/* * << * Davinci * == * Copyright (C) 2016 - 2017 EDP * == * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * >> */ import React, { FC, useCallback, memo } from 'react' import { Form, Row, Col, Input, InputNumber, Radio, Select, Divider } from 'antd' import { WrappedFormUtils } from 'antd/lib/form/Form' import Checkbox, { CheckboxChangeEvent } from 'antd/lib/checkbox' import { RadioChangeEvent } from 'antd/lib/radio' import Condition from './Condition' import { ControlTypesLocale, ControlTypes, DatePickerFormatsLocale, ControlVisibilityTypes, SHOULD_LOAD_OPTIONS } from '../../constants' import { IControl } from '../../types' import { getDatePickerFormatOptions } from '../../util' import utilStyles from 'assets/less/util.less' const FormItem = Form.Item const Option = Select.Option const RadioGroup = Radio.Group const RadioButton = Radio.Button interface IBaseFormProps { form: WrappedFormUtils controls: IControl[] controlBase: Omit onControlTypeChange: (value) => void onMultipleSettingChange: (e: CheckboxChangeEvent) => void onSliderPropChange: (min: number, max: number, step: number) => void onCommonPropChange: (propName: string, value) => void } const BaseForm: FC = ({ form, controls, controlBase, onControlTypeChange, onMultipleSettingChange, onSliderPropChange, onCommonPropChange }) => { const { getFieldDecorator } = form const { key, type, multiple, visibility } = controlBase const datePickerFormatOptions = getDatePickerFormatOptions(type, multiple) const colSpan = { xxl: 12, xl: 18 } const itemCols = { labelCol: { span: 8 }, wrapperCol: { span: 12 } } const radioTypeChange = useCallback( (e: RadioChangeEvent) => { onCommonPropChange('radioType', e.target.value) }, [onCommonPropChange] ) const visibilityChange = useCallback( (e: RadioChangeEvent) => { onCommonPropChange('visibility', e.target.value) }, [onCommonPropChange] ) const minChange = useCallback( (value) => { const { max, step } = form.getFieldsValue() onSliderPropChange(value, max, step) }, [form, onSliderPropChange] ) const maxChange = useCallback( (value) => { const { min, step } = form.getFieldsValue() onSliderPropChange(min, value, step) }, [form, onSliderPropChange] ) const stepChange = useCallback( (value) => { const { min, max } = form.getFieldsValue() onSliderPropChange(min, max, value) }, [form, onSliderPropChange] ) const labelChange = useCallback( (e: CheckboxChangeEvent) => { onCommonPropChange('label', e.target.checked) }, [onCommonPropChange] ) const minValidator = useCallback( (rule, value, callback) => { if (typeof value === 'number') { const max = form.getFieldValue('max') if (typeof max === 'number' && value > max) { callback('最小值不能大于最大值') } else { callback() } } else { callback() } }, [form] ) const maxValidator = useCallback( (rule, value, callback) => { form.validateFields(['min'], { force: true }, () => void 0) callback() }, [form] ) const getDateFormatComponent = useCallback( () => ( {getFieldDecorator( 'dateFormat', {} )( )} ), [datePickerFormatOptions] ) const getMultipleComponent = useCallback( () => ( {getFieldDecorator('multiple', { valuePropName: 'checked' })( )} ), [onMultipleSettingChange] ) let controlPropComponents switch (type) { case ControlTypes.Date: controlPropComponents = [getDateFormatComponent(), getMultipleComponent()] break case ControlTypes.DateRange: controlPropComponents = [getDateFormatComponent()] break case ControlTypes.Select: case ControlTypes.TreeSelect: controlPropComponents = [getMultipleComponent()] break case ControlTypes.Radio: controlPropComponents = ( {getFieldDecorator( 'radioType', {} )( 常规 按钮 )} ) break case ControlTypes.Slider: controlPropComponents = ( <> {getFieldDecorator('min', { rules: [ { required: true, message: '最小值不能为空' }, { validator: minValidator } ] })()} {getFieldDecorator('max', { rules: [ { required: true, message: '最大值不能为空' }, { validator: maxValidator } ] })()} {getFieldDecorator('step', { initialValue: 1 })( )} {getFieldDecorator('label', { valuePropName: 'checked' })( )} ) break } const selectTypeControls = controls.filter( (c) => c.type === ControlTypes.Select && c.key !== key ) return ( <> 控制器配置 {getFieldDecorator('key', {})()} {getFieldDecorator( 'type', {} )( )} {controlPropComponents} {SHOULD_LOAD_OPTIONS[type] && ( <> {getFieldDecorator( 'cache', {} )( 开启 关闭 )} {getFieldDecorator('expired', {})()} )} {getFieldDecorator( 'width', {} )( )} {getFieldDecorator( 'visibility', {} )( 显示 隐藏 条件 )} {visibility === ControlVisibilityTypes.Conditional && ( {getFieldDecorator( 'conditions[0]', {} )()} )} ) } export default memo(BaseForm)