2

我有一个 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} />
)
4

1 回答 1

0

我可能没有清楚地理解您的问题,因为它相当简单,但没有:

{ element.url ? <Field component={SwitchField} .../> : "" }

你可以这样做:

{ 
element.url 
  ? <Field component={SwitchField} .../>
  : <Field component={TextField} .../>
}
于 2017-04-27T00:21:52.013 回答