Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AutoComplete] Different label and value #45089

Closed
callmeaf opened this issue Jan 22, 2025 · 2 comments
Closed

[AutoComplete] Different label and value #45089

callmeaf opened this issue Jan 22, 2025 · 2 comments
Assignees
Labels
component: autocomplete This is the name of the generic UI component, not the React module!

Comments

@callmeaf
Copy link

callmeaf commented Jan 22, 2025

Steps to reproduce

Steps:

  1. Open this link to live example: (required) https://codesandbox.io/p/sandbox/reverent-cori-sk85rc?file=%2Fsrc%2FDemo.js%3A17%2C51

Current behavior

load countries data and get label of country to display in TextField and set it as TextField value

Expected behavior

I need to get country code instead label of it
Example:
when i select "Australia" country, i want when submit form, get code of country "AU"

Context

I understand TextField act like input html and input html only display the value of it
for fixe this issue can use <input type="hidden" name="country" value="AU" />
but i only open this issue if mui handle this situation, i use easy way instead hard way

Your environment

{
"dependencies": {
"@emotion/react": "latest",
"@emotion/styled": "latest",
"@mui/material": "latest",
"react": "latest",
"react-dom": "latest"
},
"description": "https://github.com/mui/material-ui/blob/v6.4.1/docs/data/material/components/autocomplete/CountrySelect.js",
"devDependencies": {
"react-scripts": "latest"
},
"scripts": {
"build": "react-scripts build",
"eject": "react-scripts eject",
"start": "react-scripts start",
"test": "react-scripts test"
}
}

Search keywords: MUI AutoComplete,Different Label And Value

@callmeaf callmeaf added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Jan 22, 2025
@zannager zannager added the component: autocomplete This is the name of the generic UI component, not the React module! label Jan 23, 2025
@aarongarciah aarongarciah changed the title MUI AutoComplete Different Label And Value [AutoComplete] Different Label And Value Jan 23, 2025
@aarongarciah aarongarciah changed the title [AutoComplete] Different Label And Value [AutoComplete] Different label And value Jan 23, 2025
@aarongarciah aarongarciah changed the title [AutoComplete] Different label And value [AutoComplete] Different label and value Jan 23, 2025
@aarongarciah
Copy link
Member

@callmeaf one way of achieving this is by controlling the Autocomplete component (using the value and onChange props as explained in the docs) and using the country object as the value. Here's an example: https://codesandbox.io/p/sandbox/reverent-cori-sk85rc

Another option, probably simpler depending on your use case, is to just find the country by name from the list (assuming all names are unique). Here's an example: https://codesandbox.io/p/sandbox/festive-worker-zfhmyq

Some existing issues already cover the problems of not being able to have different formats between options and values: #23708 and #31192

@aarongarciah aarongarciah removed the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Jan 23, 2025
Copy link

This issue has been closed. If you have a similar problem but not exactly the same, please open a new issue.
Now, if you have additional information related to this issue or things that could help future readers, feel free to leave a comment.

Note

@callmeaf How did we do? Your experience with our support team matters to us. If you have a moment, please share your thoughts in this short Support Satisfaction survey.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: autocomplete This is the name of the generic UI component, not the React module!
Projects
None yet
Development

No branches or pull requests

4 participants