DefaultValue.tsx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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, Suspense } from 'react'
  21. import classnames from 'classnames'
  22. import { Form, Row, Col, Radio as AntRadio, Icon } from 'antd'
  23. import RelativeDatePicker from 'components/RelativeDatePicker'
  24. import Select from '../../Control/Select'
  25. import Radio from '../../Control/Radio'
  26. import Date from '../../Control/Date'
  27. import DateRange from '../../Control/DateRange'
  28. import InputText from '../../Control/InputText'
  29. import TreeSelect from '../../Control/TreeSelect'
  30. import Slider from '../../Control/Slider'
  31. import NumberRange from 'components/NumberRange'
  32. import { WrappedFormUtils } from 'antd/lib/form/Form'
  33. import { RadioChangeEvent } from 'antd/lib/radio'
  34. import { TreeNode } from 'antd/lib/tree-select'
  35. import { IControl, IControlOption } from '../../types'
  36. import {
  37. ControlTypes,
  38. ControlDefaultValueTypes,
  39. ControlOptionTypes,
  40. IS_DATE_TYPE
  41. } from '../../constants'
  42. import utilStyles from 'assets/less/util.less'
  43. const FormItem = Form.Item
  44. const RadioGroup = AntRadio.Group
  45. const RadioButton = AntRadio.Button
  46. interface IDefaultValueProps {
  47. form: WrappedFormUtils
  48. controlBase: Omit<IControl, 'relatedItems' | 'relatedViews'>
  49. defaultValueOptions: Array<IControlOption | object>
  50. defaultValueLoading: boolean
  51. onDefaultValueTypeChange: (e: RadioChangeEvent) => void
  52. onGetDefaultValueOptions: () => void
  53. }
  54. const DefaultValue: FC<IDefaultValueProps> = ({
  55. form,
  56. controlBase,
  57. defaultValueOptions,
  58. defaultValueLoading,
  59. onDefaultValueTypeChange,
  60. onGetDefaultValueOptions
  61. }) => {
  62. const { getFieldDecorator } = form
  63. const {
  64. type,
  65. multiple,
  66. optionType,
  67. defaultValueType,
  68. customOptions
  69. } = controlBase
  70. const colSpan = { xxl: 12, xl: 18 }
  71. const itemCols = {
  72. labelCol: { span: 8 },
  73. wrapperCol: { span: 12 }
  74. }
  75. let component
  76. switch (type) {
  77. case ControlTypes.Select:
  78. component = (
  79. <Row>
  80. <Col {...colSpan}>
  81. <FormItem label="默认值" {...itemCols}>
  82. {[ControlOptionTypes.Auto, ControlOptionTypes.Manual].includes(
  83. optionType
  84. ) && (
  85. <a onClick={onGetDefaultValueOptions}>
  86. {`点击加载 `}
  87. {defaultValueLoading && <Icon type="loading" />}
  88. </a>
  89. )}
  90. {getFieldDecorator(
  91. 'defaultValue',
  92. {}
  93. )(
  94. <Select
  95. control={controlBase}
  96. options={
  97. optionType === ControlOptionTypes.Custom
  98. ? customOptions || []
  99. : (defaultValueOptions as IControlOption[])
  100. }
  101. />
  102. )}
  103. </FormItem>
  104. </Col>
  105. </Row>
  106. )
  107. break
  108. case ControlTypes.Radio:
  109. component = (
  110. <Row>
  111. <Col {...colSpan}>
  112. <FormItem label="默认值" {...itemCols}>
  113. {[ControlOptionTypes.Auto, ControlOptionTypes.Manual].includes(
  114. optionType
  115. ) && (
  116. <a onClick={onGetDefaultValueOptions}>
  117. {`点击加载 `}
  118. {defaultValueLoading && <Icon type="loading" />}
  119. </a>
  120. )}
  121. {getFieldDecorator('defaultValue', {
  122. rules: [{ required: true, message: '单选按钮默认值不能为空' }]
  123. })(
  124. <Radio
  125. control={controlBase}
  126. options={
  127. optionType === ControlOptionTypes.Custom
  128. ? customOptions || []
  129. : (defaultValueOptions as IControlOption[])
  130. }
  131. />
  132. )}
  133. </FormItem>
  134. </Col>
  135. </Row>
  136. )
  137. break
  138. case ControlTypes.Date:
  139. component = (
  140. <Row>
  141. <Col {...colSpan}>
  142. <FormItem label="默认值" {...itemCols}>
  143. {defaultValueType === ControlDefaultValueTypes.Fixed ? (
  144. <Suspense fallback={null}>
  145. {getFieldDecorator(
  146. 'defaultValue',
  147. {}
  148. )(<Date control={controlBase} size="default" />)}
  149. </Suspense>
  150. ) : (
  151. getFieldDecorator('defaultValue', {})(<RelativeDatePicker />)
  152. )}
  153. </FormItem>
  154. </Col>
  155. </Row>
  156. )
  157. break
  158. case ControlTypes.DateRange:
  159. component =
  160. defaultValueType === ControlDefaultValueTypes.Fixed ? (
  161. <Row>
  162. <Col {...colSpan}>
  163. <FormItem label="默认值" {...itemCols}>
  164. {getFieldDecorator(
  165. 'defaultValue',
  166. {}
  167. )(<DateRange control={controlBase} />)}
  168. </FormItem>
  169. </Col>
  170. </Row>
  171. ) : (
  172. <>
  173. <Row>
  174. <Col {...colSpan}>
  175. <FormItem label="默认值-起始" {...itemCols}>
  176. {getFieldDecorator(
  177. 'defaultValueStart',
  178. {}
  179. )(<RelativeDatePicker />)}
  180. </FormItem>
  181. </Col>
  182. </Row>
  183. <Row>
  184. <Col {...colSpan}>
  185. <FormItem label="默认值-结束" {...itemCols}>
  186. {getFieldDecorator(
  187. 'defaultValueEnd',
  188. {}
  189. )(<RelativeDatePicker />)}
  190. </FormItem>
  191. </Col>
  192. </Row>
  193. </>
  194. )
  195. break
  196. case ControlTypes.InputText:
  197. component = (
  198. <Row>
  199. <Col {...colSpan}>
  200. <FormItem label="默认值" {...itemCols}>
  201. {getFieldDecorator('defaultValue', {})(<InputText />)}
  202. </FormItem>
  203. </Col>
  204. </Row>
  205. )
  206. break
  207. case ControlTypes.NumberRange:
  208. component = (
  209. <Row>
  210. <Col {...colSpan}>
  211. <FormItem label="默认值" {...itemCols}>
  212. {getFieldDecorator('defaultValue', {})(<NumberRange />)}
  213. </FormItem>
  214. </Col>
  215. </Row>
  216. )
  217. break
  218. case ControlTypes.Slider:
  219. component = (
  220. <Row>
  221. <Col {...colSpan}>
  222. <FormItem label="默认值" {...itemCols}>
  223. {getFieldDecorator('defaultValue', {
  224. rules: [{ required: true, message: '数字滑块默认值不能为空' }]
  225. })(<Slider control={controlBase} />)}
  226. </FormItem>
  227. </Col>
  228. </Row>
  229. )
  230. break
  231. case ControlTypes.TreeSelect:
  232. component = (
  233. <Row>
  234. <Col {...colSpan}>
  235. <FormItem label="默认值" {...itemCols}>
  236. <a onClick={onGetDefaultValueOptions}>
  237. {`点击加载 `}
  238. {defaultValueLoading && <Icon type="loading" />}
  239. </a>
  240. {getFieldDecorator(
  241. 'defaultValue',
  242. {}
  243. )(
  244. <TreeSelect
  245. control={controlBase}
  246. options={defaultValueOptions as TreeNode[]}
  247. />
  248. )}
  249. </FormItem>
  250. </Col>
  251. </Row>
  252. )
  253. break
  254. }
  255. return (
  256. <>
  257. <Row>
  258. <Col
  259. className={classnames({
  260. [utilStyles.hide]: !(IS_DATE_TYPE[type] && !multiple)
  261. })}
  262. {...colSpan}
  263. >
  264. <FormItem label="默认值类型" {...itemCols}>
  265. {getFieldDecorator(
  266. 'defaultValueType',
  267. {}
  268. )(
  269. <RadioGroup onChange={onDefaultValueTypeChange}>
  270. <RadioButton value={ControlDefaultValueTypes.Fixed}>
  271. 固定值
  272. </RadioButton>
  273. <RadioButton value={ControlDefaultValueTypes.Dynamic}>
  274. 动态值
  275. </RadioButton>
  276. </RadioGroup>
  277. )}
  278. </FormItem>
  279. </Col>
  280. </Row>
  281. {component}
  282. </>
  283. )
  284. }
  285. export default DefaultValue