/* * << * 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, { useCallback, useState } from 'react' import { Modal, Form, Input, Radio, Popover, Upload, Icon, Button, message } from 'antd' const FormItem = Form.Item const RadioGroup = Radio.Group import { FormItemProps, FormComponentProps } from 'antd/lib/form' import { UploadProps } from 'antd/lib/upload' import { ICSVMetaInfo } from '../types' const formItemLayout: Partial = { labelCol: { span: 6 }, wrapperCol: { span: 16 } } interface IUploadCsvModalProps extends FormComponentProps { visible: boolean sourceId: number uploading: boolean onValidate: ( values: Pick, callback: (errMsg?: string) => void ) => void onOk: (value: ICSVMetaInfo) => void onCancel: () => void } const UploadCsvModal: React.FC = (props) => { const { visible, sourceId, uploading, form, onValidate, onOk, onCancel } = props const { getFieldDecorator } = form const [files, setFiles] = useState([]) const preventUpload: UploadProps['beforeUpload'] = useCallback((file) => { setFiles([file]) return false }, []) const save = useCallback(() => { if (!files.length) { message.error('请上传 csv 文件!') return } form.validateFields((err, values) => { if (err) { return } values.file = files[0] values.sourceId = sourceId onOk(values) }) }, [sourceId, files, onOk]) const validateTableName = useCallback( (_, tableName: string, callback) => { onValidate( { sourceId, tableName, mode: form.getFieldValue('mode') }, callback ) }, [sourceId, onValidate] ) const resetFields = useCallback(() => { form.resetFields() setFiles([]) }, []) return (
{getFieldDecorator('tableName', { rules: [ { required: true, message: '表名不能为空' }, { validator: validateTableName } ], validateFirst: true, validateTrigger: '' })()} {getFieldDecorator('primaryKeys', {})()} {getFieldDecorator('indexKeys', {})()} {getFieldDecorator('mode', { initialValue: 0 })( 新增 替换 追加 覆盖 )}

新增:首次上传文件到新表

替换:保持原有表结构不变,清空原有表数据后上传

追加:保持原有表结构不变,保持原有表数据并追加

覆盖:重建表结构并替换数据

} >
) } export default Form.create()(UploadCsvModal)