Material UI react autocomplete set different label and different value

we can see the example here at https://material-ui.com/components/autocomplete/

I wanted to set option label and value different.

Like here is example used

    const defaultProps = {
       options: top5Films,
       getOptionLabel: (option) => option.title,
    };

    <Autocomplete
      {...defaultProps}
      id="auto-complete"
      value={value}
      onChange={(event, newValue) => {
        setValue(newValue);
      }}
      autoComplete
      includeInputInList
      renderInput={(params) => <TextField {...params} label="clearOnEscape" margin="normal"/>}
    />
    const top5Films= [
    { title: 'The Shawshank Redemption', year: 1994 },
    { title: 'The Godfather', year: 1972 },
    { title: 'The Godfather: Part II', year: 1974 },
    { title: 'The Dark Knight', year: 2008 },
    { title: '12 Angry Men', year: 1957 }
    ]

But I have data like:

const top5Films= [
    { id: 1, title: 'The Shawshank Redemption', year: 1994 },
    { id: 2, title: 'The Godfather', year: 1972 },
    { id: 3, title: 'The Godfather: Part II', year: 1974 },
    { id: 4, title: 'The Dark Knight', year: 2008 },
    { id: 5, title: '12 Angry Men', year: 1957 }
    ]

I want to set id as value and show title as label.

Answer

Looks like the object is assigned to the value.

So setting id to value crashed the options.

I used the id from the object in following way for further operation.

/* eslint-disable no-use-before-define */
import React from "react";
import TextField from "@material-ui/core/TextField";
import Autocomplete from "@material-ui/lab/Autocomplete";

export default function Playground() {
  const [value, setValue] = React.useState(null);
  const [id, setId] = React.useState(null);
  const [title, setTitle] = React.useState(null);

  return (
    <div>
      <div>{`value: ${value}`}</div>
      <div>{`id: ${id}`}</div>
      <div>{`title: ${title}`}</div>

      <br />
      <div style={{ width: 300 }}>
        <Autocomplete
          options={top5Films}
          getOptionLabel={option => option.title}
          id="movies"
          value={value}
          onChange={(event, newValue) => {
            console.log(newValue);
            if (newValue) {
              setValue(newValue);
              setId(newValue.id);
              setTitle(newValue.title);
            }
          }}
          renderInput={params => (
            <TextField {...params} label="Movies" margin="normal" />
          )}
        />
      </div>
    </div>
  );
}

// Top 5 films as rated by IMDb users. http://www.imdb.com/chart/top
const top5Films = [
  { id: 1, title: "The Shawshank Redemption", year: 1994 },
  { id: 2, title: "The Godfather", year: 1972 },
  { id: 3, title: "The Godfather: Part II", year: 1974 },
  { id: 4, title: "The Dark Knight", year: 2008 },
  { id: 5, title: "12 Angry Men", year: 1957 }
];

This works for now but best answer is always welcome.

Leave a Reply

Your email address will not be published. Required fields are marked *