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

Autocomplete don't get disabled value from FormControl #34418

Open
2 tasks done
FlorianBurgevin opened this issue Sep 21, 2022 · 5 comments
Open
2 tasks done

Autocomplete don't get disabled value from FormControl #34418

FlorianBurgevin opened this issue Sep 21, 2022 · 5 comments
Labels
component: autocomplete This is the name of the generic UI component, not the React module! docs Improvements or additions to the documentation

Comments

@FlorianBurgevin
Copy link

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Steps to reproduce 馃暪

Hello,

I'm trying to customize the Autocomplete component.

I want to use it with a FormControl as I use it with my other custom components.
The FormControl should control the "disabled" and "error" state of the input.

But, with the Autocomplete, the disable prop is not passed to the Autocomplete renderInput.

Here is an example : https://codesandbox.io/s/brave-bird-kv5jcc?file=/demo.tsx

As you can see in the console, the disable prop is equal to false, but true in the FormControl.

Current behavior 馃槸

With the Autocomplete, the disable prop is not passed to the Autocomplete renderInput.

Expected behavior 馃

The FormControl should control the "disabled" state of the input.

Context 馃敠

I want to customize the Autocomplete.
I already customized input

Your environment 馃寧

npx @mui/envinfo
  Don't forget to mention which browser you used.
  Output from `npx @mui/envinfo` goes here.
@FlorianBurgevin FlorianBurgevin added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Sep 21, 2022
@siriwatknp siriwatknp added the component: autocomplete This is the name of the generic UI component, not the React module! label Sep 22, 2022
@mnajdova
Copy link
Member

Thanks for the feedback. I agree, it is surprising that the other input elements are compatible with the FormControl component, but the Autocomplete is not. We should fix this by using the useFormControl hook in the Autocomplete component too. @FlorianBurgevin would you be up to creating a pull request for this and pinging me for review?

@mnajdova mnajdova added new feature New feature or request ready to take Help wanted. Guidance available. There is a high chance the change will be accepted and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Sep 22, 2022
@mnajdova mnajdova removed their assignment Sep 22, 2022
@FlorianBurgevin
Copy link
Author

Hi @mnajdova !

Pull Request is done !

@oliviertassinari
Copy link
Member

oliviertassinari commented Sep 24, 2022

The example in the codesandbox is:

<FormControl disabled={true}>
  <FormLabel>Custom autocomplete with Input</FormLabel>
  <Autocomplete
    disablePortal
    id="combo-box-demo"
    options={top100Films}
    sx={{ width: 300 }}
    renderInput={(params) => {
      console.log(params);
      return <Input {...params} />;
    }}
  />
</FormControl>

The disabled prop is not the only issue, there are many more:

  • the listbox of the autocomplete doesn't link the label
  • the label doesn't link the textbox
  • the size isn't inherited from the FormControl
  • the InputLabel is not used to inherit more states from the FormControl
  • there are wrong props are spread to the DOM element
  • wrong CSS spacing between elements

The best fix might be to add documentation on how to use Autocomplete when breaking down the TextField into sub-components. As far as I know, it needs to be:

<Autocomplete
  disabled={true}
  id="combo-box-demo"
  options={top100Films}
  sx={{ width: 300 }}
  renderInput={({ InputProps, inputProps, InputLabelProps, ...params}) => {
    return (
      <FormControl variant="standard" {...params}>
        <InputLabel {...InputLabelProps}>Custom autocomplete with Input</InputLabel>
        <Input {...InputProps} inputProps={inputProps} />
      </FormControl>
    );
  }}
/>

for the props propagation to be correct. The autocomplete needs to be rendered as a parent of the input elements for the wiring to be correct.

@oliviertassinari oliviertassinari removed new feature New feature or request ready to take Help wanted. Guidance available. There is a high chance the change will be accepted labels Sep 24, 2022
@FlorianBurgevin
Copy link
Author

@oliviertassinari it's a bit confusing to use FormControl inside the Autocomplete no?

<FormControl>
  <FormLabel>Classic Input</FormLabel>
  <Input />
</FormControl>

The FormControl is a wrapper that should contain any type of field. And take care of the label and other stuff around a field.

So, i should be able to use an Autocomplete as a field inside my FormControl ?

<FormControl>
  <FormLabel>Autocomplete Input</FormLabel>
  <Autocomplete />
</FormControl>

@myYensid
Copy link

myYensid commented Dec 9, 2022

馃檹 This is what I am looking for. Thanks oliviertassinari! 馃檹

      <Autocomplete
        id="combo-box-demo"
        options={top100Films}
        sx={{ width: 300 }}
        renderInput={({
          InputProps,
          inputProps,
          InputLabelProps,
          ...params
        }) => {
          return (
            <FormControl variant="standard" {...params}>
              <FormLabel {...InputLabelProps}>Label Outside</FormLabel>
              <OutlinedInput {...InputProps} inputProps={inputProps} />
            </FormControl>
          );
        }}
      />

@ZeeshanTamboli ZeeshanTamboli added the docs Improvements or additions to the documentation label Apr 13, 2023
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! docs Improvements or additions to the documentation
Projects
None yet
Development

No branches or pull requests

6 participants