AliasExpressionTest.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import React from 'react'
  2. import { Input, Button, Form, Modal } from 'antd'
  3. import { FormComponentProps } from 'antd/lib/form'
  4. const FormItem = Form.Item
  5. import { IQueryVariableMap } from 'app/containers/Dashboard/types'
  6. interface IAliasExpressionTestProps extends FormComponentProps {
  7. visible: boolean
  8. queryVariableNames: string[]
  9. onClose: () => void
  10. onTest: (queryVariableMap: IQueryVariableMap) => void
  11. }
  12. export class AliasExpressionTest extends React.PureComponent<IAliasExpressionTestProps> {
  13. private labelCol = {span: 8}
  14. private wrapperCol = {span: 14}
  15. private renderQueryVarItem = (queryVariableName: string) => {
  16. const { form } = this.props
  17. const { getFieldDecorator } = form
  18. return (
  19. <FormItem key={queryVariableName} label={queryVariableName} labelCol={this.labelCol} wrapperCol={this.wrapperCol}>
  20. {getFieldDecorator(queryVariableName)(<Input />)}
  21. </FormItem>
  22. )
  23. }
  24. private ok = () => {
  25. const { form, onTest } = this.props
  26. let queryVariables = form.getFieldsValue() as { [key: string]: string }
  27. queryVariables = Object.entries(queryVariables).reduce((obj, [key, value]) => {
  28. obj[`$${key}$`] = value
  29. return obj
  30. }, {})
  31. onTest(queryVariables)
  32. }
  33. private close = () => {
  34. this.props.onClose()
  35. }
  36. private testModalFooter = [(
  37. <Button
  38. key="cancel"
  39. size="large"
  40. onClick={this.close}
  41. >
  42. 关 闭
  43. </Button>
  44. ), (
  45. <Button
  46. key="submit"
  47. size="large"
  48. type="primary"
  49. onClick={this.ok}
  50. >
  51. 确 定
  52. </Button>
  53. )]
  54. public render () {
  55. const { visible, queryVariableNames } = this.props
  56. return (
  57. <Modal
  58. title="变量值输入"
  59. wrapClassName="ant-modal-small"
  60. footer={this.testModalFooter}
  61. visible={visible}
  62. onCancel={this.close}
  63. >
  64. <Form>
  65. {queryVariableNames.map((name) => this.renderQueryVarItem(name))}
  66. </Form>
  67. </Modal>
  68. )
  69. }
  70. }
  71. export default Form.create<IAliasExpressionTestProps>()(AliasExpressionTest)