0

In Semantic UI React, I can't figure out how to set the selected value of a dropdown (on an edit form, for example). 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.

<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:

{ 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.

1
  • actually value is also described as "{bool|string|number|arrayOf}". Is not it better to change code so value for each option would be user.id? Commented Sep 4, 2018 at 19:26

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.