ant-design/components/form/demo/form-context.md
vagusX 149729e524
use ant design icons 4.0 (#18217)
* feat: use @ant-design/icons@4.0

* feat: use createFromIconfontCN to make site works

* feat: update doc for Icon

* feat: use icon in component Alert

* feat: use icon in component Avatar

* feat: use icon in component Breadcrumb

* feat: use icon in component Button

* feat: use icon in component Cascader

* feat: use icon in component Collapse

* feat: use icon in component Datepicker

* feat: use icon in component Dropdown

* feat: use icon in component Form

* feat: use icon in component Input

* feat: use icon in component InputNumber

* feat: use icon in component Layout

* feat: use icon in component Mention

* feat: use icon in component Message

* feat: use icon in component Modal

* feat: use icon in component Notification

* feat: use icon in component PageHeader

* feat: use icon in component Pagination

* feat: use icon in component Popconfirm

* feat: use icon in component Progress

* feat: use icon in component Rate

* feat: use icon in component Result

* feat: use icon in component Select

* feat: use icon in component Step

* feat: use icon in component Switch

* feat: use icon in component Table

* feat: use icon in component Tab

* feat: use icon in component Tag

* feat: handle rest component which using Icon

* fix: remove unused vars

* feat: use latest alpha ant design icons

* fix: failed test in uploadlist.test.js

* test: update snapshot for icons

* doc: add Icon for site

* doc: use @ant-design/icons in site

* chore: use latest icons

* fix: tslint issue

* fix: test cases

* fix: types for react

* fix: lint rules for import orders

* fix: use @ant-design/icons@4.0.0-alpha.5 to avoid insert css in server render

* fix: eslint error in demo/**.md

* inject antd icons

* update snapshot

* fix site

* doc: update docs

* fix: code snippets icon in site

* feat: use latest @ant-design/icons

* fix: icon props in message
2019-08-13 14:07:17 +08:00

3.8 KiB
Raw Blame History

order title
8
zh-CN en-US
多表单联动 Control between forms

zh-CN

通过 Form.Provider 在表单间处理数据。本例子中Modal 的确认按钮在 Form 之外,通过 form.submit 方法调用表单提交功能。反之,则推荐使用 <Button htmlType="submit" /> 调用 web 原生提交逻辑。

en-US

Use Form.Provider to process data between forms. In this case, submit button is in the Modal which is out of Form. You can use form.submit to submit form. Besides, we recommend native <Button htmlType="submit" /> to submit a form.

import { Form, Input, InputNumber, Modal, Button, Avatar, Typography } from 'antd';
import { Smile } from '@ant-design/icons';

const layout = {
  labelCol: { span: 8 },
  wrapperCol: { span: 16 },
};
const tailLayout = {
  wrapperCol: { offset: 8, span: 16 },
};

interface ModalFormProps {
  visible: boolean;
  onCancel: () => void;
}

const ModalForm: React.FC<ModalFormProps> = ({ visible, onCancel }) => {
  const [form] = Form.useForm();

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

  const onOk = () => {
    form.submit();
  };

  return (
    <Modal title="Basic Drawer" visible={visible} onOk={onOk} onCancel={onCancel}>
      <Form form={form} layout="vertical" name="userForm">
        <Form.Item name="name" label="User Name" rules={[{ required: true }]}>
          <Input />
        </Form.Item>
        <Form.Item name="age" label="User Age" rules={[{ required: true }]}>
          <InputNumber />
        </Form.Item>
      </Form>
    </Modal>
  );
};

const Demo = () => {
  const [visible, setVisible] = React.useState(false);

  const showUserModal = () => {
    setVisible(true);
  };

  const hideUserModal = () => {
    setVisible(false);
  };

  const onFinish = values => {
    console.log('Finish:', values);
  };

  return (
    <div>
      <Form.Provider
        onFormFinish={(name, { values, forms }) => {
          if (name === 'userForm') {
            const { basicForm } = forms;
            const users = basicForm.getFieldValue('users') || [];
            basicForm.setFieldsValue({ users: [...users, values] });
            setVisible(false);
          }
        }}
      >
        <Form {...layout} name="basicForm" onFinish={onFinish}>
          <Form.Item name="group" label="Group Name" rules={[{ required: true }]}>
            <Input />
          </Form.Item>
          <Form.Item
            label="User List"
            shouldUpdate={(prevValues, curValues) => prevValues.users !== curValues.users}
          >
            {({ getFieldValue }) => {
              const users = getFieldValue('users') || [];
              return users.length ? (
                <ul>
                  {users.map((user, index) => (
                    <li key={index} className="user">
                      <Avatar icon="user" />
                      {user.name} - {user.age}
                    </li>
                  ))}
                </ul>
              ) : (
                <Typography.Text className="ant-form-text" type="secondary">
                  ( <Smile /> No user yet. )
                </Typography.Text>
              );
            }}
          </Form.Item>
          <Form.Item {...tailLayout}>
            <Button htmlType="submit" type="primary">
              Submit
            </Button>
            <Button htmlType="button" style={{ marginLeft: 8 }} onClick={showUserModal}>
              Add User
            </Button>
          </Form.Item>
        </Form>

        <ModalForm visible={visible} onCancel={hideUserModal} />
      </Form.Provider>
    </div>
  );
};

ReactDOM.render(<Demo />, mountNode);
#components-form-demo-form-context .user {
  margin-bottom: 8px;
}

#components-form-demo-form-context .user .ant-avatar {
  margin-right: 8px;
}