Semantic React - Form.Select如何使用自定义数组选项

问题描述 投票:1回答:1

关于From.Select的语义反应文档是为它提供一个需要具有如下特定数组的prop选项:

const options = [
 { key: 'm', text: 'Male', value: 'male' },
 { key: 'f', text: 'Female', value: 'female' },
]

并使用它:

<Form.Field control={Select} label='Gender' options={options} placeholder='Gender' />

如果我想使用其他数组与自定义键和值,例如:

const options = [
 { date: 'somedate', title: 'sometitle', },
 { date: 'somedate', title: 'sometitle', },
]

我收到有关使用错误道具的错误

我的问题是如何使用我自己的数组与此选择组件谢谢!

forms reactjs web semantic-ui semantic-ui-react
1个回答
0
投票

您可以在将选项发送到语义组件之前重新格式化您的选项:

import { get, map } from 'lodash'

const reformatOptions = options =>
  map(options, e => ({
    key: get(e, 'date'),
    value: get(e, 'date'),
    text: get(e, 'title'), // (or whatever other format you wish to use)
  }))

然后将options=reformatOptions(options)传递给您的语义组件

© www.soinside.com 2019 - 2024. All rights reserved.