OptionSettingForm.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2017 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * >>
  19. */
  20. import React, { PureComponent } from 'react'
  21. import { Form, Input, Modal, Select, Divider } from 'antd'
  22. import { FormComponentProps } from 'antd/lib/form/Form'
  23. import { IFlatRelatedView } from './ControlForm/types'
  24. import { IControlOption } from '../types'
  25. import styles from '../Control.less'
  26. const FormItem = Form.Item
  27. const Option = Select.Option
  28. interface IOptionSettingFormProps extends FormComponentProps {
  29. visible: boolean
  30. values: IControlOption
  31. customOptions: IControlOption[]
  32. optionWithVariable: boolean
  33. relatedViewList: IFlatRelatedView[]
  34. onSave: () => void
  35. onCancel: () => void
  36. afterClose: () => void
  37. }
  38. class OptionSettingForm extends PureComponent<IOptionSettingFormProps> {
  39. public componentDidUpdate(prevProps: IOptionSettingFormProps) {
  40. const { form, values, optionWithVariable } = this.props
  41. if (values !== prevProps.values) {
  42. if (!values) {
  43. form.resetFields()
  44. } else {
  45. form.setFieldsValue({
  46. ...values,
  47. ...(optionWithVariable && values.variables)
  48. })
  49. }
  50. }
  51. }
  52. public render() {
  53. const {
  54. form,
  55. visible,
  56. optionWithVariable,
  57. relatedViewList,
  58. onSave,
  59. onCancel,
  60. afterClose
  61. } = this.props
  62. const { getFieldDecorator } = form
  63. const itemCols = {
  64. labelCol: { span: 8 },
  65. wrapperCol: { span: 12 }
  66. }
  67. return (
  68. <Modal
  69. title="编辑自定义选项"
  70. visible={visible}
  71. wrapClassName={`ant-modal-small ${styles.optionsModal}`}
  72. onOk={onSave}
  73. onCancel={onCancel}
  74. afterClose={afterClose}
  75. >
  76. <Form>
  77. <FormItem label="值" {...itemCols}>
  78. {getFieldDecorator('value', {
  79. rules: [{ required: true, message: '值不能为空' }]
  80. })(<Input />)}
  81. </FormItem>
  82. <FormItem label="文本" {...itemCols}>
  83. {getFieldDecorator('text', {})(<Input />)}
  84. </FormItem>
  85. {optionWithVariable && (
  86. <>
  87. <Divider>关联变量</Divider>
  88. {relatedViewList.map(({ id, name, variables }) => (
  89. <FormItem key={id} label={name} {...itemCols}>
  90. {getFieldDecorator(
  91. `${id}`,
  92. {}
  93. )(
  94. <Select placeholder="请选择" allowClear>
  95. {variables.map((v) => (
  96. <Option key={v.name} value={v.name}>
  97. {v.name}
  98. </Option>
  99. ))}
  100. </Select>
  101. )}
  102. </FormItem>
  103. ))}
  104. </>
  105. )}
  106. </Form>
  107. </Modal>
  108. )
  109. }
  110. }
  111. export default Form.create<IOptionSettingFormProps>()(OptionSettingForm)