從網上搜到的form表單解決方案如下:
1.uform,地址:https://uformjs.org/#/MpI2Ij/dNFzFyTb
UForm 和核心特性:
- 基於標准 JSON Schema 協議,數據化構建表單
- 基於 rxjs 對表單內部的副作用做統一管理,輕松解決各種復雜聯動校驗場景
- 支持各種表單布局方案
- 支持可視化構建表單
- 支持自定義組件擴展
- 分布式狀態管理,表單性能更高
不足:基於styled-components來開發的,涉及到的自定義樣式主要是Form和FormItem層面上的樣式。
樣式很難hack,很難自定義。
2.react-jsonschema-form,地址:https://github.com/rjsf-team/react-jsonschema-form
- 強耦合 Bootstrap,不方便擴展
- JSON 描述不能很好的在 JSX 中描述
- 沒能很好的解決表單布局,表單聯動的各種復雜問題
- 性能不行,內部數據管理走的 React 的全量 rerender 機制來做數據同步
最終使用:Formik+yup組合來處理表單。github上star數目最多。
formik地址:https://github.com/jaredpalmer/formik
yup地址:https://github.com/jquense/yup
使用實例:
a. 不與yup結合,只使用formik,form表單代碼如下:
import React from 'react'; import { useFormik } from 'formik'; const validate = values => { const errors = {}; if (!values.firstName) { errors.firstName = 'Required'; } else if (values.firstName.length > 15) { errors.firstName = 'Must be 15 characters or less'; } if (!values.lastName) { errors.lastName = 'Required'; } else if (values.lastName.length > 20) { errors.lastName = 'Must be 20 characters or less'; } if (!values.email) { errors.email = 'Required'; } else if (!/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(values.email)) { errors.email = 'Invalid email address'; } return errors; }; const SignupForm = () => { const formik = useFormik({ initialValues: { firstName: '', lastName: '', email: '', }, validate, onSubmit: values => { alert(JSON.stringify(values, null, 2)); }, }); return ( <form onSubmit={formik.handleSubmit}> <label htmlFor="firstName">First Name</label> <input id="firstName" name="firstName" type="text" onChange={formik.handleChange} onBlur={formik.handleBlur} value={formik.values.firstName} /> {formik.touched.firstName && formik.errors.firstName ? ( <div>{formik.errors.firstName}</div> ) : null} <label htmlFor="lastName">Last Name</label> <input id="lastName" name="lastName" type="text" onChange={formik.handleChange} onBlur={formik.handleBlur} value={formik.values.lastName} /> {formik.touched.lastName && formik.errors.lastName ? ( <div>{formik.errors.lastName}</div> ) : null} <label htmlFor="email">Email Address</label> <input id="email" name="email" type="email" onChange={formik.handleChange} onBlur={formik.handleBlur} value={formik.values.email} /> {formik.touched.email && formik.errors.email ? ( <div>{formik.errors.email}</div> ) : null} <button type="submit">Submit</button> </form> ); };
以上代碼也是非常冗余。form表單校驗全靠手動維護,很不方便。接下來
b.與yup結合。formik暴露了配置validationSchema與yup結合。示例如下:
import React from 'react'; import { useFormik } from 'formik'; import * as Yup from 'yup'; const SignupForm = () => { const formik = useFormik({ initialValues: { firstName: '', lastName: '', email: '', }, validationSchema: Yup.object({ firstName: Yup.string() .max(15, 'Must be 15 characters or less') .required('Required'), lastName: Yup.string() .max(20, 'Must be 20 characters or less') .required('Required'), email: Yup.string() .email('Invalid email address') .required('Required'), }), onSubmit: values => { alert(JSON.stringify(values, null, 2)); }, }); return ( <form onSubmit={formik.handleSubmit}> <label htmlFor="firstName">First Name</label> <input id="firstName" name="firstName" type="text" onChange={formik.handleChange} onBlur={formik.handleBlur} value={formik.values.firstName} /> {formik.touched.firstName && formik.errors.firstName ? ( <div>{formik.errors.firstName}</div> ) : null} <label htmlFor="lastName">Last Name</label> <input id="lastName" name="lastName" type="text" onChange={formik.handleChange} onBlur={formik.handleBlur} value={formik.values.lastName} /> {formik.touched.lastName && formik.errors.lastName ? ( <div>{formik.errors.lastName}</div> ) : null} <label htmlFor="email">Email Address</label> <input id="email" name="email" type="email" onChange={formik.handleChange} onBlur={formik.handleBlur} value={formik.values.email} /> {formik.touched.email && formik.errors.email ? ( <div>{formik.errors.email}</div> ) : null} <button type="submit">Submit</button> </form> ); };
如上,dom節點處的代碼依然是比較復雜和重復的。
c.formik提供了一個Field組件
import React from 'react'; import { Formik, Field, Form, ErrorMessage } from 'formik'; import * as Yup from 'yup'; const SignupForm = () => { return ( <Formik initialValues={{ firstName: '', lastName: '', email: '' }} validationSchema={Yup.object({ firstName: Yup.string() .max(15, 'Must be 15 characters or less') .required('Required'), lastName: Yup.string() .max(20, 'Must be 20 characters or less') .required('Required'), email: Yup.string() .email('Invalid email address') .required('Required'), })} onSubmit={(values, { setSubmitting }) => { setTimeout(() => { alert(JSON.stringify(values, null, 2)); setSubmitting(false); }, 400); }} > <Form> <label htmlFor="firstName">First Name</label> <Field name="firstName" type="text" /> <ErrorMessage name="firstName" /> <label htmlFor="lastName">Last Name</label> <Field name="lastName" type="text" /> <ErrorMessage name="lastName" /> <label htmlFor="email">Email Address</label> <Field name="email" type="email" /> <ErrorMessage name="email" /> <button type="submit">Submit</button> </Form> </Formik> ); };
Field組件默認渲染為input。
<Field name="message" as="textarea" className="form-input"/> // <select className="my-select"/> <Field name="colors" as="select" className="my-select"> <option value="red">Red</option> <option value="green">Green</option> <option value="blue">Blue</option> </Field>
最后,再進一步用法:
import React from 'react'; import ReactDOM from 'react-dom'; import { Formik, Form, useField } from 'formik'; import styled from '@emotion/styled'; import * as Yup from 'yup'; const MyTextInput = ({ label, ...props }) => { // useField() returns [formik.getFieldProps(), formik.getFieldMeta()] // which we can spread on <input> and also replace ErrorMessage entirely. const [field, meta] = useField(props); return ( <> <label htmlFor={props.id || props.name}>{label}</label> <input className="text-input" {...field} {...props} /> {meta.touched && meta.error ? ( <div className="error">{meta.error}</div> ) : null} </> ); }; const MyCheckbox = ({ children, ...props }) => { // We need to tell useField what type of input this is // since React treats radios and checkboxes differently // than inputs/select/textarea. const [field, meta] = useField({ ...props, type: 'checkbox' }); return ( <> <label className="checkbox"> <input type="checkbox" {...field} {...props} /> {children} </label> {meta.touched && meta.error ? ( <div className="error">{meta.error}</div> ) : null} </> ); }; // Styled components .... const StyledSelect = styled.select` /** ... * / `; const StyledErrorMessage = styled.div` /** ... * / `; const StyledLabel = styled.label` /** ...* / `; const MySelect = ({ label, ...props }) => { const [field, meta] = useField(props); return ( <> <StyledLabel htmlFor={props.id || props.name}>{label}</StyledLabel> <StyledSelect {...field} {...props} /> {meta.touched && meta.error ? ( <StyledErrorMessage>{meta.error}</StyledErrorMessage> ) : null} </> ); }; // And now we can use these const SignupForm = () => { return ( <> <h1>Subscribe!</h1> <Formik initialValues={{ firstName: '', lastName: '', email: '', acceptedTerms: false, // added for our checkbox jobType: '', // added for our select }} validationSchema={Yup.object({ firstName: Yup.string() .max(15, 'Must be 15 characters or less') .required('Required'), lastName: Yup.string() .max(20, 'Must be 20 characters or less') .required('Required'), email: Yup.string() .email('Invalid email address') .required('Required'), acceptedTerms: Yup.boolean() .required('Required') .oneOf([true], 'You must accept the terms and conditions.'), jobType: Yup.string() .oneOf( ['designer', 'development', 'product', 'other'], 'Invalid Job Type' ) .required('Required'), })} onSubmit={(values, { setSubmitting }) => { setTimeout(() => { alert(JSON.stringify(values, null, 2)); setSubmitting(false); }, 400); }} > <Form> <MyTextInput label="First Name" name="firstName" type="text" placeholder="Jane" /> <MyTextInput label="Last Name" name="lastName" type="text" placeholder="Doe" /> <MyTextInput label="Email Address" name="email" type="email" placeholder="jane@formik.com" /> <MySelect label="Job Type" name="jobType"> <option value="">Select a job type</option> <option value="designer">Designer</option> <option value="development">Developer</option> <option value="product">Product Manager</option> <option value="other">Other</option> </MySelect> <MyCheckbox name="acceptedTerms"> I accept the terms and conditions </MyCheckbox> <button type="submit">Submit</button> </Form> </Formik> </> ); };
實際使用:
1.新建一個form表單元素的通用js代碼如下:

import { useField } from 'formik'; import React from 'react'; export const MyTextInput = ({ label, ...props }) => { // useField() returns [formik.getFieldProps(), formik.getFieldMeta()] // which we can spread on <input> and also replace ErrorMessage entirely. const [field, meta] = useField(props); return ( <> <label htmlFor={props.id || props.name}>{label}</label> <input className={meta.touched && meta.error?'text-input error-input':'text-input'} {...field} {...props} /> {meta.touched && meta.error ? ( <div className="error">{meta.error=="請輸入"?meta.error+label:label+meta.error}</div> ) : null} </> ); }; export const MyCheckbox = ({ children, ...props }) => { // We need to tell useField what type of input this is // since React treats radios and checkboxes differently // than inputs/select/textarea. const [field, meta] = useField({ ...props, type: 'checkbox' }); return ( <> <label className="checkbox"> <input type="checkbox" {...field} {...props} /> {children} </label> {meta.touched && meta.error ? ( <div className="error">{meta.error}</div> ) : null} </> ); }; export const MySelect = ({ label, ...props }) => { const [field, meta] = useField(props); return ( <> <label htmlFor={props.id || props.name}>{label}</label> <select {...field} {...props} /> {meta.touched && meta.error ? ( <div className="error">{meta.error}</div> ) : null} </> ); };
2.新建一個統一校驗規則js,代碼如下:

import * as Yup from 'yup' /* 設置提示文字 */ export function yupSetLocale(){ Yup.setLocale({ mixed: { required:'請輸入' }, string: { max: '字符最多${max}個' }, }); } export const password = {'message':'規則提示信息','reg':/^(?![a-zA-z]+$)(?!\d+$)(?![!@#$%^&*,.?></]+$)(?![a-z\d]+$)(?![A-Z\d]+$)(?![a-z!@#$%^&*,.?></]+$)(?![\d!@#$%^&*,.?></]+$)(?![A-Z!@#$%^&*,.?></]+$)[a-zA-z\d!@#$%^&*,.?></]{8,20}$/}
3.使用

import React ,{Component} from 'react' import { Formik, Field, Form, ErrorMessage } from 'formik'; import {MyTextInput,MyCheckbox,MySelect} from './../../../common/formItem' import * as Yup from 'yup'; import * as myYup from './../../../common/validator'; /* 設置提示文字 */ myYup.yupSetLocale(); const SignupForm = () => { const formik = Yup.object({ firstName: Yup.string() .required().matches(myYup.password.reg,myYup.password.message), lastName: Yup.string() .max(20) .required(), email: Yup.string().email('Invalid email address').required(), acceptedTerms: Yup.boolean() .required('請選擇') .oneOf([true], '你必須勾選'), jobType: Yup.string().required('請選擇'), }) return ( <Formik initialValues={{ firstName: '', lastName: '', email: '' }} validationSchema={formik} onSubmit={(values, { setSubmitting }) => { setTimeout(() => { alert(JSON.stringify(values, null, 2)); setSubmitting(false); }, 400); }} > <Form> <div> <MyTextInput label="姓名" name="firstName" type="text" placeholder="Jane" /> </div> <div> <MyTextInput label="用戶名" name="lastName" type="text" placeholder="Doe" /> </div> <div> <MyTextInput label="郵箱" name="email" type="email" placeholder="jane@formik.com" /> </div> <div> <MySelect label="Job Type" name="jobType"> <option value="">Select a job type</option> <option value="designer">Designer</option> <option value="development">Developer</option> <option value="product">Product Manager</option> <option value="other">Other</option> </MySelect> </div> <div> <MyCheckbox name="acceptedTerms"> I accept the terms and conditions </MyCheckbox> </div> <div> <button type="submit" >Submit</button> </div> </Form> </Formik> ); };