import React from 'react' import { Modal, Form, Input, Select, Checkbox, Button, Row, Col } from 'antd' const FormItem = Form.Item const TextArea = Input.TextArea const { Option } = Select import { CheckboxChangeEvent } from 'antd/lib/checkbox' import { FormComponentProps } from 'antd/lib/form/Form' import ConditionValuesControl, { ConditionValueTypes } from 'components/ConditionValuesControl' import { IViewVariable, IDacChannel, IDacTenant, IDacBiz } from 'containers/View/types' import OperatorTypes from 'utils/operatorTypes' import { ViewVariableTypes, ViewVariableTypesLocale, ViewVariableValueTypes, ViewVariableValueTypesLocale } from 'containers/View/constants' export interface IVariableModalProps { visible?: boolean variable?: IViewVariable channels: IDacChannel[] tenants: IDacTenant[] bizs: IDacBiz[] nameValidator?: (key: string, name: string, callback: (msg?: string) => void) => void onCancel?: () => void onSave?: (variable: IViewVariable) => void onLoadDacTenants: (channelName: string) => void onLoadDacBizs: (channelName: string, tenantId: number) => void } interface IVariableModalStates { operatorType: OperatorTypes selectedType: ViewVariableTypes selectedValueType: ViewVariableValueTypes defaultValues: ConditionValueTypes[] isUdf: boolean isFromService: boolean } const defaultVarible: IViewVariable = { key: '', name: '', alias: '', type: ViewVariableTypes.Query, valueType: ViewVariableValueTypes.String, defaultValues: [], udf: false, fromService: false } export class VariableModal extends React.Component { private formItemStyle = { labelCol: { span: 6 }, wrapperCol: { span: 18 } } private viewVariableTypeOptions = Object.entries(ViewVariableTypesLocale).map(([variableType, text]) => ( )) private viewVariableValueTypeOptions = Object.entries(ViewVariableValueTypesLocale).map(([valueType, text]) => ( )) public state: Readonly = { operatorType: OperatorTypes.In, selectedType: ViewVariableTypes.Query, selectedValueType: ViewVariableValueTypes.String, defaultValues: [], isUdf: false, isFromService: false } public componentDidUpdate (prevProps: IVariableModalProps & FormComponentProps) { const { form, variable, visible, channels } = this.props if (variable !== prevProps.variable || visible !== prevProps.visible) { const { type, valueType, defaultValues, udf, fromService, channel } = variable || defaultVarible if (channel && visible) { const { name: channelName, tenantId } = channel const { onLoadDacTenants, onLoadDacBizs } = this.props onLoadDacTenants(channelName) onLoadDacBizs(channelName, tenantId) } this.setState({ selectedType: type, selectedValueType: valueType, defaultValues: defaultValues || [], isUdf: udf, isFromService: fromService && channels.length > 0 }, () => { form.setFieldsValue(variable || defaultVarible) }) } } private typeChange = (selectedType: ViewVariableTypes) => { this.setState(({ isUdf, isFromService }) => ({ selectedType, isUdf: selectedType === ViewVariableTypes.Authorization ? false : isUdf, isFromService: selectedType === ViewVariableTypes.Query ? false : isFromService })) } private valueTypeChange = (selectedValueType: ViewVariableValueTypes) => { this.setState({ selectedValueType, operatorType: selectedValueType === ViewVariableValueTypes.Boolean ? OperatorTypes.Equal : OperatorTypes.In, defaultValues: [] }) } private singleDefaultValuesChange = (e: React.ChangeEvent) => { this.defaultValueChange([e.target.value]) } private defaultValueChange = (values: ConditionValueTypes[]) => { this.setState({ defaultValues: values }) } private udfChange = (e: CheckboxChangeEvent) => { const udf = e.target.checked this.setState({ isUdf: udf }) this.defaultValueChange([]) } private fromServiceChange = (e: CheckboxChangeEvent) => { const fromService = e.target.checked this.setState({ isFromService: fromService }) } private loadDacBizs = (tenantId: number) => { const { form, onLoadDacBizs } = this.props const channelName = form.getFieldValue('channel.name') onLoadDacBizs(channelName, tenantId) } private validateVariableName = (_: any, name: string, callback: (msg?: string) => void) => { const isValidName = /^[\w]+$/.test(name) if (!isValidName) { callback('变量名称由字母、数字及下划线组成') return } const { nameValidator } = this.props if (!nameValidator) { callback() return } const { variable } = this.props const key = variable ? variable.key : '' nameValidator(key, name, callback) } private clearFieldsValue = () => { this.props.form.resetFields() } private save = () => { const { form, variable, onSave } = this.props form.validateFieldsAndScroll((err, fieldsValue) => { if (!err) { const updatedVariable = fieldsValue as IViewVariable if (variable) { updatedVariable.key = variable.key } if (updatedVariable.type === ViewVariableTypes.Query) { updatedVariable.defaultValues = this.state.defaultValues } console.log(updatedVariable) // return onSave(updatedVariable) } }) } public render () { const { visible, variable, onCancel, form, channels, tenants, bizs, onLoadDacTenants } = this.props const { getFieldDecorator } = form const { operatorType, selectedType, selectedValueType, defaultValues, isUdf, isFromService } = this.state const modalButtons = [( ), ( )] return (
{getFieldDecorator('name', { rules: [{ required: true, validator: this.validateVariableName }] })()} {getFieldDecorator('alias')()} {getFieldDecorator('type', { rules: [{ required: true, message: '请选择类型' }] })()} {getFieldDecorator('valueType', { rules: [{ required: true, message: '请选择值类型' }] })()} {selectedType === ViewVariableTypes.Query && selectedValueType !== ViewVariableValueTypes.SqlExpression && ( <> {getFieldDecorator('udf', { valuePropName: 'checked', initialValue: isUdf })( 使用表达式 )} {!isUdf && } )} {selectedType === ViewVariableTypes.Query && (isUdf || selectedValueType === ViewVariableValueTypes.SqlExpression) && (