AccountModal.tsx 1.59 KB
import React, { useEffect } from 'react';
import { Form, Button, Modal, Input } from 'antd';

const Item = Form.Item;

interface Props {
  visible?: boolean,
  onCancel: Function,
  onOk: Function,
  accounts: string[]
}

export default function Index(props: Props) {
  const { visible, onCancel, onOk, accounts } = props;
  const [form] = Form.useForm();

  useEffect(() => {
    if (!visible) {
      form.resetFields();
    }
  }, [visible]);

  const onSave = () => {
    form.validateFields().then(files => {
      onOk(files.account);
      onCancel();
    });
  };

  return (
    <Modal
      title="发单账号"
      open={visible}
      maskClosable={false}
      onCancel={() => onCancel()}
      footer={[
        <Button onClick={() => onCancel()}>取消</Button>,
        <Button type="primary" htmlType="submit" onClick={() => onSave()}>确定</Button>
      ]}
    >
      <Form
        form={form}
        labelCol={{ span: 5 }}
        wrapperCol={{ span: 12 }}
      >
        <Item 
          label="发单账号"
          name="account" 
          rules={[
            { required: true, message: "请填写发单账号" },
            () => ({
            validator(_, value) {
              if (value.length != 11) {
                return Promise.reject(new Error('发单账号不符合要求'));
              } else if (accounts.includes(value)) {
                return Promise.reject(new Error('发单账号重复'));
              }
              return Promise.resolve();
            },
          }),
          ]}
        >
          <Input />
        </Item>
      </Form>
    </Modal>
  );
}