简体   繁体   English

语义UI React Form Select-设置所选值

[英]Semantic UI React Form Select - set selected value

In Semantic UI React, I can't figure out how to set the selected value of a dropdown (on an edit form, for example). 在语义UI React中,我不知道如何设置下拉列表的选定值(例如,在编辑表单上)。 Here's how the options come in: 选项的输入方式如下:

const options = users.map(user => ({
  key: user.id,
  text: user.name,
  value: user,
}))

And the form looks like this. 表单看起来像这样。 I've tried setting the defaultValue , value , etc, but nothing has worked for me yet. 我试过设置defaultValuevalue等,但是还没有任何效果。

<Form.Field>
  <Form.Select
    fluid
    selection
    label="Users"
    name="users"
    options={options}
    defaultValue={user}
    onChange={this.handleSelectChange}
  />
</Form.Field>

I would assume the defaultValue needs to be something like: 我认为defaultValue必须是这样的:

{ key: 1, text: 'Tania', value: tania }

But according to this post , the default value of a Dropdown (which is apparently the underlying code of Select "sugar") can't be an object. 但是根据这篇文章Dropdown的默认值(显然是Select “糖”的基础代码)不能是对象。

So, my solution was to do something like this: 因此,我的解决方案是执行以下操作:

const options = users.map(user => {
  if (user.name === selectedUser.name) {
    return {
      key: user.id,
      text: user.name,
      value: user,
      active: true,
      selected: true,
    }
  }
  return {
    key: user.id,
    text: user.name,
    value: user,
  }
})

I also set text={user.name} to the Select component and it works well enough now, though the first option still appears selected-but-not-active. 我还将text={user.name}设置为Select组件,并且尽管第一个选项仍然显示为selected-but-not-active,但它现在已经可以很好地工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM