npm explorer

@uiw/react-form

v4.22.3TypeScript

Form component

formreact-formdesignuiwuiw-reactreact.jsreactreact-componentcomponentcomponents
0/weekUpdated 2 years agoMITUnpacked: 76.9 KB
Published by Kenny Wong
npm install @uiw/react-form
RepositoryHomepagenpm

Form 表单
===

![Buy me a coffee](https://jaywcjlove.github.io/#/sponsor)
![Open in unpkg](https://uiwjs.github.io/npm-unpkg/#/pkg/@uiw/react-form/file/README.md)
![NPM Downloads](https://www.npmjs.com/package/@uiw/react-form)
![npm version](https://npmjs.com/@uiw/react-form)

由输入框、选择器、单选框、多选框等控件组成,用以收集、校验、提交数据。

``jsx
import { Form, FormItem } from 'uiw';
// or
import { Form, FormItem } from '@uiw/react-form';
`

$3

`jsx mdx:preview&bg=#fff
import React, { useState, useRef } from "react";
import { Form, Input, Row, Col, Slider, Button, Notify } from 'uiw';

export default function Demo() {
const form = useRef()

const onSubmit = () => {
form.current.onSubmit()
}
const resetForm = () => {
form.current.resetForm()
}
const getFieldValues = () => {
console.log('getFieldValues', form.current.getFieldValues())
}

const setFieldValue=()=>{
form.current.setFieldValue('name','UIW')
}

return (


ref={form}
onChange={({ initial, current }) => {
console.log('onChange', initial, current);
}}
onSubmit={({ initial, current }) => {
if (current.name === initial.name) {
Notify.error({
title: '提交失败!',
description:
表单提交内容为空!,
});
} else {
Notify.success({
title: '提交成功!',
description:
姓名为:${current.name},提交完成,将自动填充初始化值!,
});
}
}}
fields={{
name: {
label: '姓名',
children:
},
}}
>
{({ fields, state, canSubmit }) => {
return (


{fields.name}








{JSON.stringify(state.current)}



)
}}






)
}
`

$3

一般校验可不需引入外部包解决,如果遇到大型工程表单比较多的地方推荐使用 jquense/yup

`jsx mdx:preview&bg=#fff
import React from 'react';
import { Form, Input, Notify, Checkbox, Switch, RadioGroup, Radio, Textarea, Row, Col, Button, Select, SearchSelect } from 'uiw';

export default function Demo() {
return(
onSubmit={({initial, current}) => {
const errorObj = {};
if (current.userName.startsWith('u')) {
errorObj.userName =
姓名 ${current.userName} 不能以 ‘u’ 开头;
}
if (!current.checkboxOne) {
errorObj.checkboxOne = '一个多选条件 为必填';
}
if (!current.terms) {
errorObj.terms = '必须统一服务条款';
}
if(Object.keys(errorObj).length > 0) {
const err = new Error();
err.filed = errorObj;
throw err;
}
Notify.success({
title: '提交成功!',
description:
姓名为:${current.userName},提交完成,将自动填充初始化值!,
});
}}
onSubmitError={(error) => {
if (error.filed) {
return { ...error.filed };
}
return null;
}}
fields={{
userName: {
initialValue: 'uiw',
label: '姓名',
children: ,
help: '以“u”开头的名字将在此处显示错误信息'
},
age: {
initialValue: '9',
label: '年龄',
children:
},
select: {
children: (

)
},
searchSelect: {
initialValue:[{label: 'a7', value: 7},{label: 'a8', value: 8}],
children: (
allowClear
labelInValue={true}
showSearch={true}
mode="multiple"
disabled={false}
placeholder="请选择选项"
option={[
{ label: 'a1', value: 1 },
{ label: 'a2', value: 2 },
{ label: 'a3', value: 3 },
{ label: 'a4', value: 4 },
{ label: 'a5', value: 5 },
{ label: 'a6', value: 6 },
{ label: 'a7', value: 7 },
{ label: 'a8', value: 8 },
]}
/>
)
},
checkbox: {
initialValue: ['四川菜'],
label: '选择你想吃的菜',
children: (

菜系

四川菜
湖北菜
西北菜
新疆菜
东北菜
家常菜

红烧武昌鱼
麻婆豆腐
北京烤鸭

)
},
checkboxOne: {
inline: true,
label: '一个多选条件',
children: 四川菜
},
switch: {
inline: true,
initialValue: true,
label: '开启',
children:
},
radioGroup: {
inline: true,
initialValue: '男',
label: '单选',
children: (

男
女
人妖
未知

)
},
textarea: {
initialValue: '',
label: '多行文本输入框',
children: