我有一个 FieldArray:
<FieldArray name="elements" component={renderFiles}/>
数据:
[
{name:'xxx', url:'www.xxx.com', isRepeat: true},
{name:'yyy', url:'', isRepeat: false},
{name:'zzz', url:'', isRepeat: false}
]
我需要显示 SwitchField 取决于 url 有一个值
const renderFiles = ({ fields }) => {
return (
<div>
{fields.map((element, index) =>
<div key={'preview-wrap-'+index}>
<Field
name={`${element}.name`}
type="text"
component={TextField}/>
{ element.url ?
<Field
name={`${element}.isRepeat`}
component={SwitchField}
change={() => { console.log('change')}}/> :
''
}
</div>
)}
</div>
)
}
TextField 和 SwitchField 是 react-toolbox 的自定义组件:
const TextField = ({ input, label, theme, meta: { touched, error }, ...custom }) => (
<Input type='text' label={label} error={touched ? error : ''} {...input} {...custom} theme={{error:'text-error', ...theme}} />
)
const SwitchField = ({ input, label }) => (
<Switch checked={!!input.value} label={label} onChange={(value) => {console.log('change')}} {...input} />
)