当前位置:网站首页>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 .
边栏推荐
- Adobe international certification 𞓜 how IIT Madras brings efficiency and accessibility to scholars through Adobe e Acrobat
- 纳瓦尔宝典:不靠运气致富的原则
- mysql常用指令
- Goframe framework: quick creation of static file download web service
- Two ways to improve the writing efficiency of hard disk storage data
- 在 KubeSphere 上部署 Apache Pulsar
- SwiftUI 组件大全之使用 ScrollView 和 GeometryReader 创建动画 3D卡片 滚动效果
- R tree of search tree
- 仿360桌面悬浮球插件
- 【曾书格激光SLAM笔记】Gmapping基于滤波器的SLAM
猜你喜欢
随机推荐
MySQL optimization, the SQL execution is very stuck, and the SQL structure will not be changed until it ends in 10 seconds
How can I realize video call and interactive live broadcast in a small program?
One of the touchdesigner uses - Download and install
How to implement collection sorting?
【机器学习】 吴恩达机器学习作业 ex2逻辑回归 Matlab实现
R tree of search tree
The tax software exits when it detects that it is a virtual machine. How to solve this problem?
How to solve the problem that the web page fails to log in after the easycvr service is started?
【owt】owt-client-native-p2p-e2e-test vs2017构建2 :测试单元构建及运行
Twitter与Shopify合作 将商家产品引入Twitter购物当中
自媒体时代的贤内助——AI 视频云
[OWT] OWT client native P2P E2E test vs2017 build 2: test unit construction and operation
Why APP But Not WebPage
Google Earth Engine(GEE)——长时间序列逐月VCI数据提取分析和面积计算(墨西哥为例)
linux下的开源数据库是什么
What is the difference between ArrayList and LinkedList?
Common events for elements
华为联机对战服务玩家快速匹配后,不同玩家收到的同一房间内玩家列表不同
1058 multiple choice questions (20 points)
innodb_ruby 视角下 MySQL 记录增删改









