Formik增强表单的处理能力,简化表单的处理流程
npm i formik
import { useFormik } from 'formik'
function App() {
const formik = useFormik({
initialValues: { username: '假装文艺浪' },
onSubmit: values => { console.log(values) }
})
return <form onSubmit={formik.handleSubmit}>
<input type="text" name="username" value={formik.values.username} onChange={formik.handleChange} />
<input type="submit" />
</form>
}
export default App
import { useFormik } from 'formik'
function App() {
const formik = useFormik({
initialValues: { username: '假装文艺浪', password: '123' },
validate: values => {
console.log(values)
const errors = {}
if (!values.username) errors.username = '请输入用户名'
if (!values.password) errors.password = '请输入密码'
return errors
},
onSubmit: values => { console.log(values) }
})
return <form onSubmit={formik.handleSubmit}>
<input
type="text"
name="username"
value={formik.values.username}
onChange={formik.handleChange} />
<p>{formik.errors.username ? <div>{formik.errors.username}</div> : null}</p>
<input
type="password" name="password"
value={formik.values.password}
onChange={formik.handleChange} />
<p>{formik.errors.password ? <div>{formik.errors.password}</div> : null}</p>
<input type="submit" />
</form>
}
import { useFormik } from 'formik'
function App() {
const formik = useFormik({
initialValues: { username: '', password: '' },
validate: values => {
console.log(values)
const errors = {}
if (!values.username) errors.username = '请输入用户名'
if (!values.password) errors.password = '请输入密码'
return errors
},
onSubmit: values => { console.log(values) }
})
return <form onSubmit={formik.handleSubmit}>
<input
type="text"
name="username"
value={formik.values.username}
onChange={formik.handleChange}
onBlur={formik.handleBlur} />
<p>{formik.touched.username && formik.errors.username ? <div>{formik.errors.username}</div> : null}</p>
<input
type="password" name="password"
value={formik.values.password}
onChange={formik.handleChange}
onBlur={formik.handleBlur} />
<p>{formik.touched.password && formik.errors.password ? <div>{formik.errors.password}</div> : null}</p>
<input type="submit" />
</form>
}
export default App
npm install yup
import { useFormik } from 'formik';
import * as Yup from 'yup';
function App() {
const formik = useFormik({
initialValues: { username: '', password: '' },
validationSchema: Yup.object({
username: Yup.string()
.max(12, '用户名的长度不能大于15')
.required('请输入用户名'),
password: Yup.string()
.max(12, '密码的长度不能大于20')
.min(6,'密码的长度不能小于6')
.required('请输入密码')
}),
onSubmit: values => { console.log(values) }
})
return <form onSubmit={formik.handleSubmit}>
<input
type="text"
name="username"
value={formik.values.username}
onChange={formik.handleChange}
onBlur={formik.handleBlur} />
<p>{formik.touched.username && formik.errors.username ? formik.errors.username: null}</p>
<input
type="password" name="password"
value={formik.values.password}
onChange={formik.handleChange}
onBlur={formik.handleBlur} />
<p>{formik.touched.password && formik.errors.password ? formik.errors.password : null}</p>
<input type="submit" />
</form>
}
以上表单代码中,存在value、onChange、onBlur这些重复的代码
{...formik.getFieldProps('username')}
return <form onSubmit={formik.handleSubmit}>
<input
type="text"
name="username"
{...formik.getFieldProps('username')} />
<p>{formik.touched.username && formik.errors.username ? formik.errors.username : null}</p>
<input
type="password" name="password"
{...formik.getFieldProps('password')} />
<p>{formik.touched.password && formik.errors.password ? formik.errors.password : null}</p>
<input type="submit" />
</form>
组件内封装好的组件
import { Formik, Form, Field, ErrorMessage } from 'formik';
import * as Yup from 'yup';
function App() {
const initialValues = {
username: ''
}
const handleSubmit = (values) => {
console.log(values)
}
const Schema = Yup.object({
username: Yup.string()
.max(12, '用户名长度不能大于12')
.required('请输入用户名')
})
return <Formik
initialValues={initialValues}
onSubmit={handleSubmit}
validationSchema={Schema}
>
<Form>
<Field name="username"></Field>
<ErrorMessage name="username"></ErrorMessage>
<input type='submit' />
</Form>
</Formik>
}
export default App
<Field name="container" as="textarea" />
<Field name="select" as="select" >
<option value='React'>React</option>
<option value='Vue'>Vue</option>
<option value='Node'>Node</option>
</Field>
import { useField } from "formik";
function MyInput({label, ...props}) {
// filed 表单属性 meta存放验证信息
const [field, meta] = useField(props)
return <div>
<label htmlFor={props.id} >{label}</label>
<input {...field} {...props} />
{meta.touched && meta.error ? <span>{meta.error}</span> : null}
</div>
}
<MyInput id="mypass" label="密码" type="password" name="password" placeholder="密码" />
import React from "react";
import { useField } from "formik";
function MyCheckBox({ label, ...props }) {
// helper 对象中 存方法
const [field, meta, helper] = useField(props);
const { value } = meta;
const { setValue } = helper;
// 复选框的逻辑实现
const handleChange = () => {
const set = new Set(value)
if (set.has(props.value)) {
set.delete(props.value) // 存在,删除
} else {
set.add(props.value) // 添加
}
setValue([...set])
}
return <div>
<label htmlFor={props.id} >
<input
// 默认选中
checked={value.includes(props.value)}
type="checkbox"
{...props}
onChange={handleChange} />
{label}
</label>
</div>
}
<Checkbox value="足球" label="足球" name="hobbies" />
<Checkbox value="篮球" label="篮球" name="hobbies" />
<Checkbox value="橄榄球" label="橄榄球" name="hobbies" />