当前位置:网站首页>Form development mode
Form development mode
2022-06-23 04:05:00 【fjywan】
Mastered value/onChange, You have mastered the essence of form writing .
Management side components , There are only two purposes :
- Collect data from users
- Showing data to users
Collect data from users , There are three data formats :
- The value of the original data type ( such as string/number etc. )
- object
- Array
therefore , The development model for collecting any data from users is :
- Three input fields , An interface :value/onChange.
- Input fields for collecting raw data types ( Most of the antd Provide )
- Input box for collecting objects
- Collect the input box of the array
- form Is to distribute objects to the next level input Convenience tools for ;form It is an output object to the upper level input.
- Collect nested object form development , Is to descend step by step , Development can collect information about each object input. This is a recursive process , And can be automated .
Example
Collect non nested objects
Collect the following objects :
{
"name": " Reward rule name ",
"desc": " Description of reward rules "
}Direct use antd form, Distributed to... That collects raw values input that will do :
Form: Input: name Input: desc
Collect nested objects
Collect the following objects :
{
"name": " Reward rule name ",
"desc": " Description of reward rules "
"msg": {
"template_id": "323ll1w",
"app_id": "app12323"
}
}use antd form, Distribute to msg when :
Form: Input: name Input: desc An input field that can collect objects : msg
We need an input field that can collect objects , And its interface conforms to value/onChange.
Review the second article of the development model :
form Is to distribute objects to the next level input Convenience tools for ;form It is an output object to the upper level input.
form It is a component that can output objects , Just change its interface to value/onChange that will do .
Can collect msg Object's input box component :
import { Form, Input } from 'antd'
import React, { FC } from 'react'
export const MsgInput: FC<{
value?: any,
onChange?: (value: string) => void
}> = (props) => {
const { value, onChange = (() => { }) } = props
const [form] = Form.useForm()
const fields = [{
label: ' Templates ID',
key: 'template_id',
jsx: <Input />,
}, {
label: 'APPID',
key: 'app_id',
jsx: <Input />,
}]
const getFields = (fields) => {
const children = fields.map(one => (
<Form.Item
name={one.key}
label={one.label}
rules={one.rules}
>
{one.jsx ? one.jsx : <Input />}
</Form.Item>
))
return children
}
return (
<Form
form={form}
initialValues={value}
name='object_input'
onValuesChange={(_, values) => {
onChange(values)
}}
>
{getFields(fields)}
</Form>
)
}And this 【 Can collect msg Object input field 】, From becoming 【 Collect input boxes for any object 】 It's not far away , such as :
export const CreateObjectInput = (fields) => {
return (props) => {
const { value, onChange = (() => { }) } = props
const [form] = Form.useForm()
const getFields = (fields) => {
const children = fields.map(one => (
<Form.Item
name={one.key}
label={one.label}
rules={one.rules}
>
{one.jsx ? one.jsx : <Input />}
</Form.Item>
))
return children
}
return (
<Form
form={form}
initialValues={value}
name='object_input'
onValuesChange={(_, values) => {
onChange(values)
}}
>
{getFields(fields)}
</Form>
)
}
}Collect nested array objects
such as :
{
"name": " Reward rule name ",
"desc": " Description of reward rules "
"msg": {
"template_id": "323ll1w",
"app_id": "app12323"
},
"msgs": [
{
"template_id": "323ll1w",
"app_id": "app12323"
},
{
"template_id": "323ll1w",
"app_id": "app12323"
},
]
}use antd form, Distribute to msgs when :
Form: Input: name Input: desc MsgInput: msg An input field that can collect arrays : msgs
Can collect msgs The input field component of the array :
import { Form } from 'antd'
import React, { FC } from 'react'
export const MsgsInput: FC<{
value?: any,
onChange?: (value: any) => void
}> = (props) => {
const { value, onChange = (() => { }) } = props
const add = () => {
onChange([...value, {}])
}
const del = () => {
const newVal = [...value]
newVal.pop()
onChange(newVal)
}
if (!value) {
// If it is empty , Inform the outside first , Change to an empty array , Render at least one table
onChange([])
return <Button type='primary' onClick={add}>
newly added
</Button>
}
const onOneInputChange = (v, i) => {
const copy = [...value]
copy[i] = v
onChange(copy)
}
return (
<>
{value.map((one, index) => <MsgInput key={index} value={one} onChange={(value) => onOneInputChange(value, index)} />)}
<Button type='primary' onClick={add}>
newly added
</Button>
{(value?.length > 1) ? <Button type='default' onClick={del}>
Delete
</Button> : ''}
</>
)
}And this 【 Can collect msgs The input field of the array 】, From becoming 【 Collect input fields for any array 】 It's not far away , such as :
export const CreateArrayInput = (OneInput) => {
return (props) => {
const { value, onChange = (() => { }) } = props
const add = () => {
onChange([...value, {}])
}
const del = () => {
const newVal = [...value]
newVal.pop()
onChange(newVal)
}
if (!value) {
// If it is empty , Inform the outside first , Change to an empty array , Render at least one table
onChange([])
return <Button type='primary' onClick={add}>
newly added
</Button>
}
const onOneInputChange = (v, i) => {
const copy = [...value]
copy[i] = v
onChange(copy)
}
return (
<>
{value.map((one, index) => <OneInput key={index} value={one} onChange={(value) => onOneInputChange(value, index)} />)}
<Button type='primary' onClick={add}>
newly added
</Button>
{(value?.length > 1) ? <Button type='default' onClick={del}>
Delete
</Button> : ''}
</>
)
}
}follow-up
If all three input fields can be generalized , And the pattern is fixed , that , Given a data to be collected , Can you automate the output of forms ?
I'll break it down next time .
expand : Sharing and reuse mechanism
The key to reuse is : classification .
- Put your own needs , Fall into a category ,
- Then according to this category , Find the components that meet the requirements .
therefore , The essence of classification is : A technology for fast location solutions .
When providing a set of components , The most important thing is to establish a set of classification standards :
- Each category has a clear boundary , Mutual exclusion does not overlap
- Concise and limited classification steps , Be able to perform a quick classification of a requirement .
The greatest significance of this development model lies in :
The classification criteria for the data to be collected as components are established —— Clear and direct .
Clear to , Have the possibility of program execution classification and matching :
Enter a data , By inferring the type , Automatic matching can output corresponding types of components .
边栏推荐
- 【owt】owt-client-native-p2p-e2e-test vs2017构建 4 : 第三方库的构建及链接p2pmfc.exe
- 折半查找法
- 纳瓦尔宝典:不靠运气致富的原则
- Stress testing with locust on rainbow
- 【LeetCode】23. Merge K ascending linked lists
- 【机器学习】 吴恩达机器学习作业 ex2逻辑回归 Matlab实现
- d重载嵌套函数
- TDesign update weekly report (the second week of January 2022)
- SVG+JS智能家居监控网格布局
- Common events for elements
猜你喜欢
随机推荐
Static code block, code block, constructor execution order
How to implement collection sorting?
支持在 Kubernetes 运行,添加多种连接器,SeaTunnel 2.1.2 版本正式发布!
【LeetCode】翻转链表II
【owt】owt-client-native-p2p-e2e-test vs2017构建 3 : 无 测试单元对比, 手动生成vs项目
Insérer le tri directement
关于sql语句的问题
How to solve the problem that the web page fails to log in after the easycvr service is started?
粒子动画背景登录页面particles.js
New configuration of Alipay
第一批00后下场求职:不要误读他们的“不一样”
1-1 introduction to VMWare
Using jhipster to build microservice architecture
Hierarchical attention graph convolution network for interpretable recommendation based on knowledge graph
Svn local computer storage configuration
centos7 安装 MySQL 及配置 innodb_ruby
1-1VMware介绍
How to process large volume xlsx/csv/txt files?
An implementation of warning bombing
折半查找法









