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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[question] Help with a feature in Autocomplete #38624

Closed
2 tasks done
pedro-areal-torres opened this issue Aug 23, 2023 · 2 comments
Closed
2 tasks done

[question] Help with a feature in Autocomplete #38624

pedro-areal-torres opened this issue Aug 23, 2023 · 2 comments
Labels
component: autocomplete This is the name of the generic UI component, not the React module! support: Stack Overflow Please ask the community on Stack Overflow

Comments

@pedro-areal-torres
Copy link

pedro-areal-torres commented Aug 23, 2023

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Steps to reproduce 馃暪

The issue is, that I have this list of options:
[{id: 1, value: 1, label: 'Company1'},{id: 2, value: 2, label: 'Company2'}]

I'm using the Autocomplete component and I want to set as a value only the ID, but display the label.
Store as a value ONLY the ID.

I already tried with getOptionLabel, with getOptionSelected (deprecated).
The issue is that I'm getting undefined on the getOptionLabel, because the value is being changed only for the ID.
Currently my code is:

<Controller
      name={name}
      control={control}
      render={({ field, fieldState: { error } }) => (
        <Autocomplete
          {...field}
          getOptionLabel={(option) => option.label} // Display the label
          onChange={(event, newValue) => {
            setValue(name, extractId ? newValue.id : newValue, { shouldValidate: true });
          }}
          renderInput={(params) => (
            <TextField
              label={label}
              error={!!error}
              helperText={error ? error?.message : helperText}
              {...params}
            />
          )}
          {...other}
        />
      )}
    />

Current behavior 馃槸

Displaying the label, but I

Expected behavior 馃

No response

Context 馃敠

No response

Your environment 馃寧

npx @mui/envinfo
  Don't forget to mention which browser you used.
  Output from `npx @mui/envinfo` goes here.
@pedro-areal-torres pedro-areal-torres added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Aug 23, 2023
@mj12albert mj12albert added the component: autocomplete This is the name of the generic UI component, not the React module! label Aug 24, 2023
@mj12albert
Copy link
Member

@pedro-areal-torres You'll need to use isOptionEqualToValue and getOptionLabel props to use a custom options structure.

If getOptionLabel is not enough, renderOption is a more customizable alternative.

Here's a minimal example: https://codesandbox.io/s/https-github-com-mui-material-ui-issues-38624-m5g85w

@mj12albert mj12albert added support: Stack Overflow Please ask the community on Stack Overflow and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Aug 24, 2023
@github-actions
Copy link

馃憢 Thanks for using MUI Core!

We use GitHub issues exclusively as a bug and feature requests tracker, however,
this issue appears to be a support request.

For support, please check out https://mui.com/getting-started/support/. Thanks!

If you have a question on Stack Overflow, you are welcome to link to it here, it might help others.
If your issue is subsequently confirmed as a bug, and the report follows the issue template, it can be reopened.

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! support: Stack Overflow Please ask the community on Stack Overflow
Projects
None yet
Development

No branches or pull requests

2 participants