|
2019-05-17
{ "name": "奖励规则名", "desc": "奖励规则说明" }
直接用 antd form,分发到搜集原始值的 input 即可:
Form: Input: name Input: desc
Form: Input: name Input: desc 一个能搜集对象的输入框: msg
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: '模板 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> ) }
而这个【能搜集 msg 对象的输入框】,离成为【搜集任何对象的输入框】已经不远,比如:
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> ) } }
{ "name": "奖励规则名", "desc": "奖励规则说明" "msg": { "template_id": "323ll1w", "app_id": "app12323" }, "msgs": [ { "template_id": "323ll1w", "app_id": "app12323" }, { "template_id": "323ll1w", "app_id": "app12323" }, ] }
用 antd form,分发到 msgs 时:
Form: Input: name Input: desc MsgInput: msg 一个能搜集数组的输入框: msgs
能搜集 msgs 数组的输入框组件:
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) { // 如果为空,先通知外部,改为空数组,至少渲染一个表格 onChange([]) return <Button type='primary' onClick={add}> 新增 </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}> 新增 </Button> {(value?.length > 1) ? <Button type='default' onClick={del}> 删除 </Button> : ''} </> ) }
而这个【能搜集 msgs 数组的输入框】,离成为【搜集任何数组的输入框】已经不远,比如:
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) { // 如果为空,先通知外部,改为空数组,至少渲染一个表格 onChange([]) return <Button type='primary' onClick={add}> 新增 </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}> 新增 </Button> {(value?.length > 1) ? <Button type='default' onClick={del}> 删除 </Button> : ''} </> ) } }
编辑:航网科技 来源:腾讯云 本文版权归原作者所有 转载请注明出处
微信扫一扫咨询客服
全国免费服务热线
0755-36300002