React - 在填写所有必填字段之前禁用 Ant Design 表单中的按钮

React - Disable button in Ant Design form until all required fields are filled

我正在使用 React 和 Ant Design 表单,我有 6 个输入字段,其中 3 个是必需的。在填写所有 3 个必填字段之前,如何禁用该按钮?

我找到了一个解决方案 ,但它仅在没有错误且所有必填字段都已填写时才有效。因此,当我在第一个输入中输入内容时,它会启用按钮。

Form.js

const handleFormChange = () => {
    const hasErrors = form
      .getFieldsError()
      .some((field) => field.errors.length > 0);

    setDisabledSave(hasErrors);
  };


 return (
    <Drawer
      onClose={props.onClose}
      visible={props.isVisible}
      bodyStyle={{ paddingBottom: 80 }}
      extra={
        <Space>
          <Button onClick={props.onClose}>Cancel</Button>
          <Button
            type="primary"
            loading={props.isAdding}
            disabled={disabledSave}
            onClick={onSubmitHandler}
            htmlType="submit"
          >
            Submit
          </Button>
        </Space>
      }
    >
      <Form
        onFinish={onFinish}
        form={form}
        layout="vertical"
        onFieldsChange={handleFormChange}
      >
        <Row gutter={16}>
          <Col xs={24} sm={12}>
            <Form.Item
              name="name"
              label="Name"
              rules={[{ required: true, message: 'Name is required.' }]}
            >
              <Input placeholder="Enter name..." />
            </Form.Item>
          </Col>
          <Col xs={24} sm={12}>
            <Form.Item
              name="email"
              label="E-mail"
              rules={[
                { required: true, message: 'E-mail is required.' },
                { type: 'email', message: 'E-mail must be valid.' },
              ]}
            >
              <Input placeholder="Enter E-mail address..." />
            </Form.Item>
          </Col>
        </Row>
        {/*  other fields  */}
      </Form>
    </Drawer>
  );

使用 Form 组件的 onValuesChange 方法检查以下示例

import React, { useState } from 'react';
import 'antd/dist/antd.css';
import './index.css';
import { Form, Input, Button, Checkbox } from 'antd';

const Demo = () => {

  const [btndisabled, setbtndisabled] = useState(true);

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

  const onFinishFailed = (errorInfo) => {
    console.log('Failed:', errorInfo);
  };

  const onValuesChange = (changedValues, allValues) => {

    if ( allValues.username != undefined && allValues.password != undefined && allValues.username != '' && allValues.password != '') {
      setbtndisabled(false);
    } else {
      setbtndisabled(true);
    }
    console.log(allValues);
  };

  return (
    <Form
      name="basic"
      labelCol={{
        span: 8,
      }}
      wrapperCol={{
        span: 16,
      }}
      initialValues={{
        remember: true,
      }}
      onFinish={onFinish}
      onValuesChange={onValuesChange}
      onFinishFailed={onFinishFailed}
    >
      <Form.Item
        label="Username"
        name="username"
        rules={[
          {
            required: true,
            message: 'Please input your username!',
          },
        ]}
      >
        <Input />
      </Form.Item>

      <Form.Item
        label="Password"
        name="password"
        rules={[
          {
            required: true,
            message: 'Please input your password!',
          },
        ]}
      >
        <Input.Password />
      </Form.Item>

      <Form.Item
        wrapperCol={{
          offset: 8,
          span: 16,
        }}
      >
        <Button disabled={btndisabled} type="primary" htmlType="submit">
          Submit
        </Button>
      </Form.Item>
    </Form>
  );
};

截图:

输入所有值之前

输入所有值后