LocalControlRelatedInfoForm.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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, { FC, useCallback, useMemo, memo } from 'react'
  21. import { Form, Row, Col, Radio, Select, Divider } from 'antd'
  22. import { WrappedFormUtils } from 'antd/lib/form/Form'
  23. import { RadioChangeEvent } from 'antd/lib/radio'
  24. import { IFlatRelatedView } from './types'
  25. import { ControlFieldTypes, ControlTypes, IS_RANGE_TYPE } from '../../constants'
  26. import { filterSelectOption } from 'app/utils/util'
  27. import { IViewModelProps } from 'app/containers/View/types'
  28. const FormItem = Form.Item
  29. const Option = Select.Option
  30. const RadioGroup = Radio.Group
  31. const RadioButton = Radio.Button
  32. interface ILocalControlRelatedInfoFormProps {
  33. form: WrappedFormUtils
  34. relatedView: IFlatRelatedView
  35. controlType: ControlTypes
  36. optionWithVariable: boolean
  37. onFieldTypeChange: (id: number) => (e: RadioChangeEvent) => void
  38. }
  39. const LocalControlRelatedInfoForm: FC<ILocalControlRelatedInfoFormProps> = ({
  40. form,
  41. relatedView,
  42. controlType,
  43. optionWithVariable,
  44. onFieldTypeChange
  45. }) => {
  46. const { getFieldDecorator } = form
  47. const { id, fieldType, models, variables } = relatedView
  48. const isMultiple =
  49. IS_RANGE_TYPE[controlType] && fieldType === ControlFieldTypes.Variable
  50. const fieldValues = form.getFieldValue(`relatedViews[${id}].fields`) || []
  51. const colSpan = { xxl: 12, xl: 18 }
  52. const itemCols = {
  53. labelCol: { span: 8 },
  54. wrapperCol: { span: 12 }
  55. }
  56. const columnValidator = useCallback(
  57. (rule, value, callback) => {
  58. if (
  59. (Array.isArray(value) && !!value.length) ||
  60. (!Array.isArray(value) && value !== void 0)
  61. ) {
  62. const selectedModel =
  63. fieldType === ControlFieldTypes.Column
  64. ? models.find((m) => m.name === value)
  65. : Array.isArray(value)
  66. ? value.every((v) => variables.find((vr) => vr.name === v))
  67. : variables.find((vr) => vr.name === value)
  68. if (!selectedModel) {
  69. callback('数据模型已变化,请重新选择')
  70. } else {
  71. callback()
  72. }
  73. } else {
  74. callback()
  75. }
  76. },
  77. [form, fieldType, models, variables]
  78. )
  79. const fieldTypeText = useMemo(
  80. () => (fieldType === ControlFieldTypes.Column ? '字段' : '变量'),
  81. [fieldType]
  82. )
  83. return (
  84. <>
  85. <Divider orientation="left">关联设置</Divider>
  86. <Row>
  87. <Col {...colSpan}>
  88. <FormItem label="关联类型" {...itemCols}>
  89. {getFieldDecorator(
  90. `relatedViews[${id}].fieldType`,
  91. {}
  92. )(
  93. <RadioGroup
  94. size="small"
  95. disabled={optionWithVariable}
  96. onChange={onFieldTypeChange(id)}
  97. >
  98. <RadioButton value={ControlFieldTypes.Column}>字段</RadioButton>
  99. <RadioButton value={ControlFieldTypes.Variable}>
  100. 变量
  101. </RadioButton>
  102. </RadioGroup>
  103. )}
  104. </FormItem>
  105. </Col>
  106. </Row>
  107. <Row>
  108. <Col {...colSpan}>
  109. <FormItem label={`关联${fieldTypeText}`} {...itemCols}>
  110. {getFieldDecorator(`relatedViews[${id}].fields`, {
  111. rules: [
  112. {
  113. required: true,
  114. message: `关联${fieldTypeText}不能为空`
  115. },
  116. { validator: columnValidator }
  117. ]
  118. })(
  119. <Select
  120. showSearch
  121. placeholder="请选择"
  122. filterOption={filterSelectOption}
  123. {...(isMultiple && { mode: 'multiple' })}
  124. disabled={optionWithVariable}
  125. >
  126. {fieldType === ControlFieldTypes.Column
  127. ? models.map((m: IViewModelProps) => (
  128. <Option key={m.name} value={m.name}>
  129. {m.name}
  130. </Option>
  131. ))
  132. : variables.map((v) => (
  133. <Option
  134. key={v.name}
  135. value={v.name}
  136. disabled={
  137. isMultiple &&
  138. fieldValues.length === 2 &&
  139. !fieldValues.includes(v.name)
  140. }
  141. >
  142. {v.name}
  143. </Option>
  144. ))}
  145. </Select>
  146. )}
  147. </FormItem>
  148. </Col>
  149. </Row>
  150. </>
  151. )
  152. }
  153. export default memo(LocalControlRelatedInfoForm)