zoukankan      html  css  js  c++  java
  • antd4 form表单使用

    antd4.x form使用实例:

    // AntdForm4.js class组件形式
    import React, { Component, useEffect } from "react";
    import { Form, Input, Button } from "antd";
    const FormItem = Form.Item;
    const nameRules = { required: true, message: "请输入姓名" };
    const passwordRules = { required: true, message: "请输入密码" };
    
    export default class AntdFormPage extends Component {
      formRef = React.createRef();
      componentDidMount() {
        this.formRef.current.setFieldsValue({ name: "default name" });
      }
      onReset = () => {
        this.formRef.current.resetFields();
      };
      onFinish = (val) => {
        console.log("onfinish", val);
      };
      onFinishFailed = (val) => {
        console.log("onfinishfailed", val);
      };
      render() {
        console.log("antd render", this.formRef.current);
        return (
          <div>
            <Form
              ref={this.formRef}
              onFinish={this.onFinish}
              onFinishFailed={this.onFinishFailed}
              onReset={this.onReset}
            >
              <FormItem label="姓名" name="name" rules={[nameRules]}>
                <Input placeholder="name" />
              </FormItem>
              <FormItem label="密码" name="password" rules={[passwordRules]}>
                <Input placeholder="password" />
              </FormItem>
              <FormItem>
                <Button type="primary" size="large" htmlType="submit">
                  submit
                </Button>
              </FormItem>
              <FormItem>
                <Button type="default" size="large" htmlType="reset">
                  reset
                </Button>
              </FormItem>
            </Form>
          </div>
        );
      }
    }
    
    
    

      

    // AntdForm4.js function组件形式
    import React, { Component, useEffect } from "react";
    import { Form, Input, Button } from "antd";
    const FormItem = Form.Item;
    const nameRules = { required: true, message: "姓名" };
    const passwordRules = { required: true, message: "Mim " };
    export default function AntdFormPage(props) {
      const [form] = Form.useForm();
      const onFinish = (val) => {
        console.log("onfinish", val);
      };
      const onFinishFailed = (val) => {
        console.log("onFinishFailed", val);
      };
      useEffect(() => {
        console.log("form", form);
        form.setFieldsValue({ name: "default name" });
      }, []);
      return (
        <div>
          <h3>AntdFormPage</h3>
          <Form form={form} onFinish={onFinish} onFinishFailed={onFinishFailed}>
            <FormItem label="姓名" name="name" rules={[nameRules]}>
              <Input placeholder="name" />
            </FormItem>
            <FormItem label="密码" name="password" rules={[passwordRules]}>
              <Input placeholder="password" />
            </FormItem>
            <FormItem>
              <Button type="primary" size="large" htmlType="submit">
                submit
              </Button>
            </FormItem>
          </Form>
        </div>
      );
    }
    

      

    // App.js
    import React from "react";
    import AntdFormPage from "./pages/AntdForm4.js";
    
    function App() {
      return (
        <div className="App">
          <AntdFormPage />
        </div>
      );
    }
    
    export default App;
    

      

  • 相关阅读:
    使用Code First Migrations依据代码更新数据库结构
    Engine Yard增加对Node.js的支持
    CSS3无前缀脚本prefixfree.js及Animatable介绍
    html5客户端本地存储之sessionStorage及storage事件
    cctype,string,vector
    管理朋友信息程序
    三位数的排列组合
    结构体字节对齐
    习题3.13
    OPENCV用户手册之图像处理部分(之一):梯度、边缘与角点(中文翻译)
  • 原文地址:https://www.cnblogs.com/haimengqingyuan/p/13526068.html
Copyright © 2011-2022 走看看