FormatConfigModal.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. import React from 'react'
  2. import { fromJS } from 'immutable'
  3. import { IFieldFormatConfig } from './types'
  4. import { getDefaultFieldFormatConfig } from './util'
  5. import { ViewModelVisualTypes } from 'containers/View/constants'
  6. import { NumericUnitList, FieldFormatTypes, FieldFormatTypesLocale, FieldFormatTypesSetting, defaultFormatConfig } from './constants'
  7. import { FormComponentProps } from 'antd/lib/form/Form'
  8. import { Form, Input, InputNumber, Radio, Checkbox, Select, Button, Modal } from 'antd'
  9. const FormItem = Form.Item
  10. const RadioGroup = Radio.Group
  11. const { Option } = Select
  12. interface IFormatConfigFormProps extends FormComponentProps {
  13. visible: boolean
  14. visualType: ViewModelVisualTypes
  15. formatConfig: IFieldFormatConfig
  16. onCancel: () => void
  17. onSave: (config: IFieldFormatConfig) => void
  18. }
  19. interface IFormatConfigFormStates {
  20. localConfig: IFieldFormatConfig
  21. }
  22. class FormatConfigForm extends React.PureComponent<IFormatConfigFormProps, IFormatConfigFormStates> {
  23. private numericUnitOptions = NumericUnitList.map((item) => (
  24. <Option key={item} value={item}>{item}</Option>
  25. ))
  26. public constructor (props: IFormatConfigFormProps) {
  27. super(props)
  28. const { formatConfig } = props
  29. this.state = {
  30. localConfig: formatConfig ? fromJS(formatConfig).toJS() : getDefaultFieldFormatConfig()
  31. }
  32. }
  33. public componentDidMount () {
  34. this.props.form.setFieldsValue(this.state.localConfig)
  35. }
  36. public componentWillReceiveProps (nextProps: IFormatConfigFormProps) {
  37. const { formatConfig, form } = nextProps
  38. if (formatConfig === this.props.formatConfig) { return }
  39. this.setState({
  40. localConfig: formatConfig ? fromJS(formatConfig).toJS() : getDefaultFieldFormatConfig()
  41. }, () => {
  42. form.setFieldsValue(this.state.localConfig)
  43. })
  44. }
  45. private onFormatTypeChange = (e) => {
  46. const { localConfig } = this.state
  47. const selectedFormatType = e.target.value as FieldFormatTypes
  48. const previousValues = this.props.form.getFieldsValue() as IFieldFormatConfig
  49. const nextLocalConfig: IFieldFormatConfig = {
  50. ...localConfig,
  51. ...previousValues,
  52. formatType: selectedFormatType
  53. }
  54. if (selectedFormatType !== FieldFormatTypes.Default && !localConfig[selectedFormatType]) {
  55. // @ts-ignore
  56. nextLocalConfig[selectedFormatType] = { ...defaultFormatConfig[selectedFormatType] }
  57. }
  58. this.setState({
  59. localConfig: nextLocalConfig
  60. })
  61. }
  62. private renderFormatTypes () {
  63. const { form, visualType } = this.props
  64. const { getFieldDecorator } = form
  65. const { localConfig } = this.state
  66. const formatTypesGroup = FieldFormatTypesSetting[visualType] && (
  67. <FormItem>
  68. {getFieldDecorator('formatType', {
  69. initialValue: localConfig.formatType
  70. })(
  71. <RadioGroup onChange={this.onFormatTypeChange}>
  72. {FieldFormatTypesSetting[visualType].map((formatType) => (
  73. <Radio key={formatType} value={formatType}>{FieldFormatTypesLocale[formatType]}</Radio>
  74. ))}
  75. </RadioGroup>
  76. )}
  77. </FormItem>
  78. )
  79. return formatTypesGroup
  80. }
  81. private formItemLayout = {
  82. labelCol: {
  83. sm: { span: 6 }
  84. },
  85. wrapperCol: {
  86. sm: { span: 14 }
  87. }
  88. }
  89. private renderNumeric () {
  90. const { form } = this.props
  91. const { localConfig } = this.state
  92. const config = localConfig[FieldFormatTypes.Numeric]
  93. const { decimalPlaces, unit, useThousandSeparator } = config
  94. const { getFieldDecorator } = form
  95. const formItems = [(
  96. <FormItem key={`${FieldFormatTypes.Numeric}.decimalPlaces`} label="小数位数:" {...this.formItemLayout}>
  97. {getFieldDecorator(`${FieldFormatTypes.Numeric}.decimalPlaces`, {
  98. initialValue: decimalPlaces,
  99. rules: [{ required: true, message: '不能为空' }]
  100. })(
  101. <InputNumber min={0} max={6} />
  102. )}
  103. </FormItem>
  104. ), (
  105. <FormItem key={`${FieldFormatTypes.Numeric}.unit`} label="单位:" {...this.formItemLayout}>
  106. {getFieldDecorator(`${FieldFormatTypes.Numeric}.unit`, {
  107. initialValue: unit
  108. })(
  109. <Select>{this.numericUnitOptions}</Select>
  110. )}
  111. </FormItem>
  112. ), (
  113. <FormItem key={`${FieldFormatTypes.Numeric}.useThousandSeparator`} label=" " colon={false} {...this.formItemLayout}>
  114. {getFieldDecorator(`${FieldFormatTypes.Numeric}.useThousandSeparator`, {
  115. initialValue: useThousandSeparator,
  116. valuePropName: 'checked'
  117. })(
  118. <Checkbox>使用千分位分隔符</Checkbox>
  119. )}
  120. </FormItem>
  121. )]
  122. return formItems
  123. }
  124. private renderCurrency () {
  125. const { form } = this.props
  126. const { localConfig } = this.state
  127. const config = localConfig[FieldFormatTypes.Currency]
  128. const { decimalPlaces, unit, useThousandSeparator, prefix, suffix } = config
  129. const { getFieldDecorator } = form
  130. const formItems = [(
  131. <FormItem key={`${FieldFormatTypes.Currency}.decimalPlaces`} label="小数位数:" {...this.formItemLayout}>
  132. {getFieldDecorator(`${FieldFormatTypes.Currency}.decimalPlaces`, {
  133. initialValue: decimalPlaces,
  134. rules: [{ required: true, message: '不能为空' }]
  135. })(
  136. <InputNumber min={0} max={6} />
  137. )}
  138. </FormItem>
  139. ), (
  140. <FormItem key={`${FieldFormatTypes.Currency}.unit`} label="单位:" {...this.formItemLayout}>
  141. {getFieldDecorator(`${FieldFormatTypes.Currency}.unit`, {
  142. initialValue: unit
  143. })(
  144. <Select>{this.numericUnitOptions}</Select>
  145. )}
  146. </FormItem>
  147. ), (
  148. <FormItem key={`${FieldFormatTypes.Currency}.useThousandSeparator`} label=" " colon={false} {...this.formItemLayout}>
  149. {getFieldDecorator(`${FieldFormatTypes.Currency}.useThousandSeparator`, {
  150. initialValue: useThousandSeparator,
  151. valuePropName: 'checked'
  152. })(
  153. <Checkbox>使用千分位分隔符</Checkbox>
  154. )}
  155. </FormItem>
  156. ), (
  157. <FormItem key={`${FieldFormatTypes.Currency}.prefix`} label="前缀" {...this.formItemLayout}>
  158. {getFieldDecorator(`${FieldFormatTypes.Currency}.prefix`, {
  159. initialValue: prefix
  160. })(
  161. <Input />
  162. )}
  163. </FormItem>
  164. ), (
  165. <FormItem key={`${FieldFormatTypes.Currency}.suffix`} label="后缀" {...this.formItemLayout}>
  166. {getFieldDecorator(`${FieldFormatTypes.Currency}.suffix`, {
  167. initialValue: suffix
  168. })(
  169. <Input />
  170. )}
  171. </FormItem>
  172. )]
  173. return formItems
  174. }
  175. private renderPercentage () {
  176. const { form } = this.props
  177. const { localConfig } = this.state
  178. const config = localConfig[FieldFormatTypes.Percentage]
  179. const { decimalPlaces } = config
  180. const { getFieldDecorator } = form
  181. const formItem = (
  182. <FormItem label="小数位数:" {...this.formItemLayout}>
  183. {getFieldDecorator(`${FieldFormatTypes.Percentage}.decimalPlaces`, {
  184. initialValue: decimalPlaces,
  185. rules: [{ required: true, message: '不能为空' }]
  186. })(
  187. <InputNumber min={0} max={6} />
  188. )}
  189. </FormItem>
  190. )
  191. return formItem
  192. }
  193. private renderScientificNotation () {
  194. const { form } = this.props
  195. const { localConfig } = this.state
  196. const config = localConfig[FieldFormatTypes.ScientificNotation]
  197. const { decimalPlaces } = config
  198. const { getFieldDecorator } = form
  199. const formItem = (
  200. <FormItem label="小数位数:" {...this.formItemLayout}>
  201. {getFieldDecorator(`${FieldFormatTypes.ScientificNotation}.decimalPlaces`, {
  202. initialValue: decimalPlaces,
  203. rules: [{ required: true, message: '不能为空' }]
  204. })(
  205. <InputNumber min={0} max={6} />
  206. )}
  207. </FormItem>
  208. )
  209. return formItem
  210. }
  211. private renderCustom () {
  212. const { form } = this.props
  213. const { localConfig } = this.state
  214. const config = localConfig[FieldFormatTypes.Custom]
  215. const { format } = config
  216. const { getFieldDecorator } = form
  217. const formItem = (
  218. <FormItem label="格式:" {...this.formItemLayout}>
  219. {getFieldDecorator(`${FieldFormatTypes.Custom}.format`, {
  220. initialValue: format
  221. })(
  222. <Input />
  223. )}
  224. </FormItem>
  225. )
  226. return formItem
  227. }
  228. private renderDate () {
  229. const { form } = this.props
  230. const { localConfig } = this.state
  231. const config = localConfig[FieldFormatTypes.Date]
  232. const { format } = config
  233. const { getFieldDecorator } = form
  234. const formItem = (
  235. <FormItem label="格式:" {...this.formItemLayout}>
  236. {getFieldDecorator(`${FieldFormatTypes.Date}.format`, {
  237. initialValue: format,
  238. rules: [{ required: true, message: '不能为空' }]
  239. })(
  240. <Input />
  241. )}
  242. </FormItem>
  243. )
  244. return formItem
  245. }
  246. private renderConfig = (formatType: FieldFormatTypes) => {
  247. switch (formatType) {
  248. case FieldFormatTypes.Numeric:
  249. return this.renderNumeric()
  250. case FieldFormatTypes.Currency:
  251. return this.renderCurrency()
  252. case FieldFormatTypes.Percentage:
  253. return this.renderPercentage()
  254. case FieldFormatTypes.ScientificNotation:
  255. return this.renderScientificNotation()
  256. case FieldFormatTypes.Custom:
  257. return this.renderCustom()
  258. case FieldFormatTypes.Date:
  259. return this.renderDate()
  260. default:
  261. return null
  262. }
  263. }
  264. private save = () => {
  265. const { form } = this.props
  266. form.validateFieldsAndScroll((err, fieldsValues) => {
  267. if (err) { return }
  268. const formatType = fieldsValues['formatType']
  269. const config: IFieldFormatConfig = {
  270. formatType
  271. }
  272. if (formatType !== FieldFormatTypes.Default) {
  273. config[formatType] = fieldsValues[formatType]
  274. }
  275. this.props.onSave(config)
  276. })
  277. }
  278. private cancel = () => {
  279. this.props.onCancel()
  280. }
  281. private modalFooter = [(
  282. <Button
  283. key="cancel"
  284. size="large"
  285. onClick={this.cancel}
  286. >
  287. 取 消
  288. </Button>
  289. ), (
  290. <Button
  291. key="submit"
  292. size="large"
  293. type="primary"
  294. onClick={this.save}
  295. >
  296. 保 存
  297. </Button>
  298. )]
  299. public render () {
  300. const { visible } = this.props
  301. const { localConfig } = this.state
  302. const { formatType } = localConfig
  303. const config = this.renderConfig(formatType)
  304. return (
  305. <Modal
  306. title="设置数值格式"
  307. wrapClassName="ant-modal-small"
  308. footer={this.modalFooter}
  309. visible={visible}
  310. onCancel={this.cancel}
  311. onOk={this.save}
  312. >
  313. <Form>
  314. {this.renderFormatTypes()}
  315. {config}
  316. </Form>
  317. </Modal>
  318. )
  319. }
  320. }
  321. export default Form.create<IFormatConfigFormProps>()(FormatConfigForm)