Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 259
yathomasi Avatar asked Feb 12 '26 04:02

yathomasi


2 Answers

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.

like image 164
zb22 Avatar answered Feb 13 '26 18:02

zb22


okk you can try in this way -

  const options = [
    {
      id: 212,
      label: 'First Title'
    },
    {
      id: 321,
      label: 'Second Title'
    },
    {
      id: 543,
      label: 'Third Title'
    }
  ]
  
  <Autocomplete
      placeholder="Search…"
      options={options}
      isOptionEqualToValue={(option, value) => option.id === value.id}
      renderInput={(params) => <TextField {...params} label={'label'} />}
      renderOption={(props, option) => {
          return (
              <li {...props}>
                {option.label}
              </li>
             )
           }}
      onChange={(_event, value) => {
          setValue(value.id)
      }}
  />
like image 32
Md Muaz Ahmed Avatar answered Feb 13 '26 17:02

Md Muaz Ahmed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!