0

I'm mapping over an array of objects and using the name in dropdown selection w/ semantic-ui-react.

I have some mock data

mock.onGet("/dataschemas").reply(200, {
  data: [
    {
      id: "2147483599",
      selfUri: "/dataschemas/2147483599",
      name: "Book Catalog"
    },
    {
      id: "2147483600",
      selfUri: "/dataschemas/2147483600",
      name: "Business Articles"
    }
  ]
});

in cDM I'm updating state with the object as dataschemas

  async componentDidMount() {
    await this.getSchemas();
  }

  getSchemas = async () => {
    try {
      const { data } = await axios.get("/dataschemas");
      console.log(data);
      const dataschemas = data.data;

      this.setState({ dataschemas: dataschemas });

      console.log("This is the dataschema list:", dataschemas);
    } catch (error) {
      console.error(Error(`Error fetching results list: ${error.message}`));
    }
  };

My change handler function looks like this:

handleSchemaChange = (e, { value }) => this.setState({ name: value });

Then in render I'm using the <Dropdown> component

render() {
    const { dataschemas } = this.state;
    return (
      <div>
        <div>
          <label>Select a dataschema: </label>
          <Dropdown
            placeholder="Select data schema"
            clearable
            fluid
            selection
            multiple={true}
            options={dataschemas}
            header="PLEASE SELECT A DATASCHEMA"
            value={dataschemas.filter(({ name }) => name === this.state.name)}
            onChange={this.handleSchemaChange}
          />
        </div>
      </div>
    );
  }

I can't get the dataschema names to show in the dropdown or get label to update when a selection is made. I don't know if I'm missing a prop or not updating the state correctly, any ideas?

Codesandbox here

1 Answer 1

6

As specified in https://react.semantic-ui.com/modules/dropdown/ you should use the following structure to display an object inside a Dropdown.

{
  "key": "",
  "text": "",
  "value": ""
}

Example: use this as options value in your Dropdown

options={dataschemas.map(ds => {
              return {
                  key: ds.id,
                  text: ds.name,
                  value: ds.selfUri
              }
            })}
Sign up to request clarification or add additional context in comments.

4 Comments

Ah I missed that. Any idea how to update state after the selection is made?
Just needed to make the value value:ds.name to get the updated name in state.
I know you answered this question a year ago but what's the difference between value and text?
@KMC I think that text means the text to be displayed and value the actual form value that will be sent via http in a form for the dropdown. I suggest you to check the code, the project is open-source.

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.