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] useAutocomplete hook disabled option can be clicked #44

Open
2 tasks done
phiter opened this issue Aug 23, 2023 · 4 comments
Open
2 tasks done

[Autocomplete] useAutocomplete hook disabled option can be clicked #44

phiter opened this issue Aug 23, 2023 · 4 comments
Assignees
Labels
bug 🐛 Something doesn't work component: autocomplete This is the name of the generic UI component, not the React module!

Comments

@phiter
Copy link

phiter commented Aug 23, 2023

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Steps to reproduce 🕹

Link to live example: https://codesandbox.io/s/compassionate-chatelet-qz92q4?file=/Demo.tsx

Steps:

  1. Have an autocomplete hook with the getOptionDisabled option working
  2. Pass some disabled option
  3. Click the disabled option

Current behavior 😯

The disabled option can be clicked, even though if inspected, it has aria-disabled="true" attribute.

Expected behavior 🤔

The option should not be clickable.

Context 🔦

I'm creating a custom Autocomplete input with the useAutocomplete hook, but this is failing on my tests.

Your environment 🌎

npx @mui/envinfo
  System:
    OS: macOS 13.5.1
  Binaries:
    Node: 18.12.1 - /usr/local/bin/node
    Yarn: 1.22.19 - ~/.npm-global/bin/yarn
    npm: 8.19.2 - /usr/local/bin/npm
  Browsers:
    Chrome: 116.0.5845.96 <-- tested here
    Edge: 114.0.1823.55
    Safari: 16.6
@phiter phiter added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Aug 23, 2023
@mj12albert mj12albert changed the title [@mui/base] useAutocomplete hook disabled option can be clicked [base-ui][Autocomplete] useAutocomplete hook disabled option can be clicked Aug 24, 2023
@mj12albert mj12albert removed the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Aug 24, 2023
@mj12albert
Copy link
Member

@phiter Thanks for reporting this ~ I agree it's a little unintuitive 😅

When we use this hook to make the Material UI and Joy UI autocomplete components, we use pointer-events: none to prevent the click (here)

Here's an example with just Base UI: https://codesandbox.io/s/https-github-com-mui-material-ui-issues-38620-77j4gk?file=/Demo.tsx

@michaldudak Do you think preventing selecting a disabled option could be provided out of the box?

@phiter
Copy link
Author

phiter commented Aug 24, 2023

@mj12albert I tried pointer-events: none at first but during unit testing, since I use @testing-library/user-events, it doesn't immediately perform clicks on elements with pointer-events: none. When I disabled this check, the click event triggered. I don't know if simply trusting CSS is a good option, because the user can disable pointer-events: none in the devtools and the function will execute if it isn't handled.

I ended up having to do this in my code 😬

{(groupedOptions as T[]).map((option, index) => {
    const optionProps = getOptionProps({ option, index });
    // Workaround until https://github.com/mui/base-ui/issues/44 is fixed
    const isDisabled = autoCompleteOptions.getOptionDisabled(option);

    return (
        <AutocompleteOption
            key={index}
            {...optionProps}
            onClick={isDisabled ? undefined : optionProps.onClick}
        >
            {option.name}
        </AutocompleteOption>
    );
})}

I suppose it could be an option for the hook, like enableDisabledOptionClickHandler or something like that.

There is no handling in the code:

https://github.com/mui/material-ui/blob/8f5ff618ba8ec5d25c82fb8c5f67f9325a41bb3b/packages/mui-base/src/useAutocomplete/useAutocomplete.js#L998-L1003

https://github.com/mui/material-ui/blob/8f5ff618ba8ec5d25c82fb8c5f67f9325a41bb3b/packages/mui-base/src/useAutocomplete/useAutocomplete.js#L1157-L1175

@aelfannir
Copy link

Same

@michaldudak
Copy link
Member

Do you think preventing selecting a disabled option could be provided out of the box?

Yes, I think it should work similarly to how Base UI's Select works (likely using the same primitives).

@michaldudak michaldudak transferred this issue from mui/material-ui Feb 27, 2024
@michaldudak michaldudak changed the title [base-ui][Autocomplete] useAutocomplete hook disabled option can be clicked [Autocomplete] useAutocomplete hook disabled option can be clicked Feb 27, 2024
@michaldudak michaldudak added component: autocomplete This is the name of the generic UI component, not the React module! bug 🐛 Something doesn't work labels Feb 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work 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