<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          UnformReact 表單組件

          聯(lián)合創(chuàng)作 · 2023-09-24 16:39

          Unform是一個(gè)以性能為中心的庫,可以幫助您在React中利用不受控制的組件性能和React Hooks創(chuàng)建漂亮的表單。

          主要特性

          • 語法漂亮
          • React Hooks ;
          • 性能很好
          • 使用非受控組件
          • 集成 pickers, dropdowns 等等

          示例表單:

          Basics

          import React from "react";
          import { Form, Input } from "@rocketseat/unform";
          
          function App() {
            function handleSubmit(data) {
              console.log(data);
          
              /**
               * {
               *   email: '[email protected]',
               *   password: "123456"
               * }
               */
            };
          
            return (
              <Form onSubmit={handleSubmit}>
                <Input name="email" />
                <Input name="password" type="password" />
          
                <button type="submit">Sign in</button>
              </Form>
            );
          }

          Nested fields

          import React from "react";
          import { Form, Input, Scope } from "@rocketseat/unform";
          
          function App() {
            function handleSubmit(data) {
              console.log(data);
          
              /**
               * {
               *   name: 'Diego',
               *   address: { street: "Name of street", number: 123 }
               * }
               */
            };
          
            return (
              <Form onSubmit={handleSubmit}>
                <Input name="name" />
          
                <Scope path="address">
                  <Input name="street" />
                  <Input name="number" />
                </Scope>
          
                <button type="submit">Save</button>
              </Form>
            );
          }

          Initial data

          import React from "react";
          import { Form, Input, Scope } from "@rocketseat/unform";
          
          function App() {
            const initialData = {
              name: 'John Doe',
              address: {
                street: 'Sample Avenue',
              },
            }
          
            function handleSubmit(data) {};
          
            return (
              <Form onSubmit={handleSubmit} initialData={initialData}>
                <Input name="name" />
          
                <Scope path="address">
                  <Input name="street" />
                  <Input name="number" />
                </Scope>
          
                <button type="submit">Save</button>
              </Form>
            );
          }

          Validation

          import React from "react";
          import { Form, Input } from "@rocketseat/unform";
          import * as Yup from 'yup';
          
          const schema = Yup.object().shape({
            email: Yup.string()
              .email('Custom invalid email message')
              .required('Custom required message'),
            password: Yup.string().min(4).required(),
          })
          
          function App() {
            function handleSubmit(data) {};
          
            return (
              <Form schema={schema} onSubmit={handleSubmit}>
                <Input name="email" />
                <Input name="password" type="password" />
          
                <button type="submit">Save</button>
              </Form>
            );
          }

          Manipulate data

          import React, { useState } from "react";
          import { Form, Input } from "@rocketseat/unform";
          import * as Yup from 'yup';
          
          const schema = Yup.object().shape({
            name: Yup.string().required(),
            email: Yup.string().email().required(),
            password: Yup.string().when('$updatePassword', {
              is: true,
              then: Yup.string().min(4).required(),
              otherwise: Yup.string().strip(true)
            }),
          })
          
          function App() {
            const [updatePassword, setUpdatePassword] = useState(false);
          
            const initialData = {
              name: 'John Doe',
              email: '[email protected]',
            }
          
            function handleSubmit(data) {};
          
            return (
              <Form
                schema={schema}
                initialData={initialData}
                context={{ updatePassword }}
                onSubmit={handleSubmit}
              >
                <Input name="name" />
                <Input name="email" />
          
                <input
                  type="checkbox"
                  name="Update Password"
                  checked={updatePassword}
                  onChange={e => setUpdatePassword(e.target.checked)}
                />
          
                <Input name="password" type="password" />
          
                <button type="submit">Save</button>
              </Form>
            );
          }
          瀏覽 22
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          編輯 分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          編輯 分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  午夜网亚洲无码 | 亚洲婷色五月 | 国产无套内精一级毛片三 | 91精品国产日韩91久久久久久 | 婷婷五月天丁香 |