23

I would like to display default value to my Autocomplete TextField component from Material UI in React.js. A pre-populated value that is automatically loaded from the users' profile and that can be changed with another one from the list.

Here is my code:

import React from 'react';
import TextField from '@material-ui/core/TextField';
import Autocomplete from '@material-ui/lab/Autocomplete';

export const ComboBox =() => {
  return (
    <Autocomplete
      id="combo-box-demo"
      options={top100Films}
      getOptionLabel={(option) => option.title}
      style={{ width: 300 }}
      renderInput={(params) => <TextField {...params} label="Combo box" defaultValue="The Godfather" variant="outlined" />}
    />
  );
}

// Top 100 films as rated by IMDb users. http://www.imdb.com/chart/top
const top100Films = [
  { title: 'The Shawshank Redemption', year: 1994 },
  { title: 'The Godfather', year: 1972 },
  { title: 'The Godfather: Part II', year: 1974 },
  { title: 'The Dark Knight', year: 2008 } 
]

I only see the input field with the label on it. defaultValue is listed as an API of both TextField and Autocomplete and I also tried to move it directly under Autocomplete. Still not working.

3 Answers 3

36

your defaultValue of <AutoComplete /> should have the same format as your options, the getOptionLabel() is used to form the label even from your defaultValue.

in your code title property of the string is undefined, so nothing is shown.

this code should work fine:

<Autocomplete
    id="combo-box-demo"
    options={top100Films}
    defaultValue={{ title: "The Godfather", year: 1972 }}
    getOptionLabel={(option) => option.title}
    style={{ width: 300 }}
    renderInput={(params) => <TextField {...params} label="Combo box" defaultValue="The Godfather" variant="outlined" />}
/>
Sign up to request clarification or add additional context in comments.

5 Comments

OK. I understand what you mean. Thank you. This works as intended!
and ho do i get the default value?
well, here you should make your own choice
what if I don't have the options yet? because the options will come asynchronously from the API request made when typing in autocomplete
6

You can use the defaultValue option like this:

<Autocomplete
    id="combo-box-demo"
    options={top100Films}
    defaultValue={ top100Films[0] }
    getOptionLabel={(option) => option.title}
    style={{ width: 300 }}
    renderInput={(params) => <TextField {...params} label="Combo box" defaultValue="The Godfather" variant="outlined" />}
/>

7 Comments

This does assign the value but it is still not rendering it on the screen. It still initially displaying the label.
What do you mean by "still displaying the label"? Please create a working example in codesandbox.io
I want it to look like this initially: imgur.com/a/0v15c0C . This is what it defaults to : imgur.com/D6TINi4
If you want the value of 12 Angry Men to be available you should have it in the options, and it's not there currently. You can add it inside, and than use it inside the defaultValue.
Not sure if you even checked my solution, it's actually exactly the same code.
|
1

If you use multiple so you should set defaultValue in list like blow

<Autocomplete multiple id="tags-standard" options={categoryList}
    defaultValue={[{ title: "cat1", id: 1 }, { title: "cat2", id: 2 }]}

    getOptionLabel={(option) => option.title}
    renderInput={(params) => (
    <TextField {...params} variant="standard" label="Category" />
    )}
    onChange={handleCategory}
                            
    />

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.