import React from 'react' import { fromJS } from 'immutable' import { IFieldFormatConfig } from './types' import { getDefaultFieldFormatConfig } from './util' import { ViewModelVisualTypes } from 'containers/View/constants' import { NumericUnitList, FieldFormatTypes, FieldFormatTypesLocale, FieldFormatTypesSetting, defaultFormatConfig } from './constants' import { FormComponentProps } from 'antd/lib/form/Form' import { Form, Input, InputNumber, Radio, Checkbox, Select, Button, Modal } from 'antd' const FormItem = Form.Item const RadioGroup = Radio.Group const { Option } = Select interface IFormatConfigFormProps extends FormComponentProps { visible: boolean visualType: ViewModelVisualTypes formatConfig: IFieldFormatConfig onCancel: () => void onSave: (config: IFieldFormatConfig) => void } interface IFormatConfigFormStates { localConfig: IFieldFormatConfig } class FormatConfigForm extends React.PureComponent { private numericUnitOptions = NumericUnitList.map((item) => ( )) public constructor (props: IFormatConfigFormProps) { super(props) const { formatConfig } = props this.state = { localConfig: formatConfig ? fromJS(formatConfig).toJS() : getDefaultFieldFormatConfig() } } public componentDidMount () { this.props.form.setFieldsValue(this.state.localConfig) } public componentWillReceiveProps (nextProps: IFormatConfigFormProps) { const { formatConfig, form } = nextProps if (formatConfig === this.props.formatConfig) { return } this.setState({ localConfig: formatConfig ? fromJS(formatConfig).toJS() : getDefaultFieldFormatConfig() }, () => { form.setFieldsValue(this.state.localConfig) }) } private onFormatTypeChange = (e) => { const { localConfig } = this.state const selectedFormatType = e.target.value as FieldFormatTypes const previousValues = this.props.form.getFieldsValue() as IFieldFormatConfig const nextLocalConfig: IFieldFormatConfig = { ...localConfig, ...previousValues, formatType: selectedFormatType } if (selectedFormatType !== FieldFormatTypes.Default && !localConfig[selectedFormatType]) { // @ts-ignore nextLocalConfig[selectedFormatType] = { ...defaultFormatConfig[selectedFormatType] } } this.setState({ localConfig: nextLocalConfig }) } private renderFormatTypes () { const { form, visualType } = this.props const { getFieldDecorator } = form const { localConfig } = this.state const formatTypesGroup = FieldFormatTypesSetting[visualType] && ( {getFieldDecorator('formatType', { initialValue: localConfig.formatType })( {FieldFormatTypesSetting[visualType].map((formatType) => ( {FieldFormatTypesLocale[formatType]} ))} )} ) return formatTypesGroup } private formItemLayout = { labelCol: { sm: { span: 6 } }, wrapperCol: { sm: { span: 14 } } } private renderNumeric () { const { form } = this.props const { localConfig } = this.state const config = localConfig[FieldFormatTypes.Numeric] const { decimalPlaces, unit, useThousandSeparator } = config const { getFieldDecorator } = form const formItems = [( {getFieldDecorator(`${FieldFormatTypes.Numeric}.decimalPlaces`, { initialValue: decimalPlaces, rules: [{ required: true, message: '不能为空' }] })( )} ), ( {getFieldDecorator(`${FieldFormatTypes.Numeric}.unit`, { initialValue: unit })( )} ), ( {getFieldDecorator(`${FieldFormatTypes.Numeric}.useThousandSeparator`, { initialValue: useThousandSeparator, valuePropName: 'checked' })( 使用千分位分隔符 )} )] return formItems } private renderCurrency () { const { form } = this.props const { localConfig } = this.state const config = localConfig[FieldFormatTypes.Currency] const { decimalPlaces, unit, useThousandSeparator, prefix, suffix } = config const { getFieldDecorator } = form const formItems = [( {getFieldDecorator(`${FieldFormatTypes.Currency}.decimalPlaces`, { initialValue: decimalPlaces, rules: [{ required: true, message: '不能为空' }] })( )} ), ( {getFieldDecorator(`${FieldFormatTypes.Currency}.unit`, { initialValue: unit })( )} ), ( {getFieldDecorator(`${FieldFormatTypes.Currency}.useThousandSeparator`, { initialValue: useThousandSeparator, valuePropName: 'checked' })( 使用千分位分隔符 )} ), ( {getFieldDecorator(`${FieldFormatTypes.Currency}.prefix`, { initialValue: prefix })( )} ), ( {getFieldDecorator(`${FieldFormatTypes.Currency}.suffix`, { initialValue: suffix })( )} )] return formItems } private renderPercentage () { const { form } = this.props const { localConfig } = this.state const config = localConfig[FieldFormatTypes.Percentage] const { decimalPlaces } = config const { getFieldDecorator } = form const formItem = ( {getFieldDecorator(`${FieldFormatTypes.Percentage}.decimalPlaces`, { initialValue: decimalPlaces, rules: [{ required: true, message: '不能为空' }] })( )} ) return formItem } private renderScientificNotation () { const { form } = this.props const { localConfig } = this.state const config = localConfig[FieldFormatTypes.ScientificNotation] const { decimalPlaces } = config const { getFieldDecorator } = form const formItem = ( {getFieldDecorator(`${FieldFormatTypes.ScientificNotation}.decimalPlaces`, { initialValue: decimalPlaces, rules: [{ required: true, message: '不能为空' }] })( )} ) return formItem } private renderCustom () { const { form } = this.props const { localConfig } = this.state const config = localConfig[FieldFormatTypes.Custom] const { format } = config const { getFieldDecorator } = form const formItem = ( {getFieldDecorator(`${FieldFormatTypes.Custom}.format`, { initialValue: format })( )} ) return formItem } private renderDate () { const { form } = this.props const { localConfig } = this.state const config = localConfig[FieldFormatTypes.Date] const { format } = config const { getFieldDecorator } = form const formItem = ( {getFieldDecorator(`${FieldFormatTypes.Date}.format`, { initialValue: format, rules: [{ required: true, message: '不能为空' }] })( )} ) return formItem } private renderConfig = (formatType: FieldFormatTypes) => { switch (formatType) { case FieldFormatTypes.Numeric: return this.renderNumeric() case FieldFormatTypes.Currency: return this.renderCurrency() case FieldFormatTypes.Percentage: return this.renderPercentage() case FieldFormatTypes.ScientificNotation: return this.renderScientificNotation() case FieldFormatTypes.Custom: return this.renderCustom() case FieldFormatTypes.Date: return this.renderDate() default: return null } } private save = () => { const { form } = this.props form.validateFieldsAndScroll((err, fieldsValues) => { if (err) { return } const formatType = fieldsValues['formatType'] const config: IFieldFormatConfig = { formatType } if (formatType !== FieldFormatTypes.Default) { config[formatType] = fieldsValues[formatType] } this.props.onSave(config) }) } private cancel = () => { this.props.onCancel() } private modalFooter = [( ), ( )] public render () { const { visible } = this.props const { localConfig } = this.state const { formatType } = localConfig const config = this.renderConfig(formatType) return (
{this.renderFormatTypes()} {config}
) } } export default Form.create()(FormatConfigForm)