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

[base-ui][useSelect] onChange not working when activated through keyboard (Enter or Space key press) #41234

Closed
smellai opened this issue Feb 22, 2024 · 5 comments
Assignees
Labels
component: select This is the name of the generic UI component, not the React module! package: base-ui Specific to @mui/base support: Stack Overflow Please ask the community on Stack Overflow

Comments

@smellai
Copy link

smellai commented Feb 22, 2024

Steps to reproduce

Link to live example: https://stackblitz.com/edit/stackblitz-starters-gnadul?file=src%2FApp.tsx

Steps:

  1. focus combobox with keyboard
  2. activate it with spacebar or Enter
  3. navigate the list with arrow-up arrow-down
  4. activate different option with enter or space
  5. combobox value not changing

Current behavior

it works as expected with keyboard interaction on version 5.0.0-beta.25, buggy in version 5.0.0-beta.37

it works as expected with mouse click on latest.

Expected behavior

both mouse and keyboard interactions work as they were working in previous versions

Context

might be related to what discussed already in another issue #40486 (comment)

Your environment

npx @mui/envinfo
  @mui/base: 5.0.0-beta.37 => 5.0.0-beta.37 
    @mui/core-downloads-tracker:  5.15.10 
    @mui/material: 5.15.10 => 5.15.10 
    @mui/private-theming:  5.15.9 
    @mui/styled-engine:  5.15.9 
    @mui/system:  5.15.9 
    @mui/types:  7.2.13 
    @mui/utils:  5.15.11 
    @types/react: 17.0.2 => 17.0.2 
    react: 17.0.2 => 17.0.2 
    react-dom: 17.0.2 => 17.0.2 
    typescript: 4.9.5 => 4.9.5 

browser: Chrome

Search keywords: keyboard select combobox useSelect base

@smellai smellai added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Feb 22, 2024
@danilo-leal danilo-leal added component: select This is the name of the generic UI component, not the React module! package: base-ui Specific to @mui/base labels Feb 22, 2024
@mj12albert
Copy link
Member

mj12albert commented Feb 23, 2024

@smellai I can reproduce the issue in your sandbox, but I don't think it's an issue with Base UI

I tested this keyboard interaction using the Controlled Select demo in the docs, and also the hook demo but converted to controlled mode: https://stackblitz.com/edit/mcve-react-base-ui-fd26fm?file=src%2FApp.tsx - both work as expected!

@mj12albert mj12albert added status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Feb 23, 2024
@smellai
Copy link
Author

smellai commented Feb 23, 2024

so the issue is with the ref (line 50). If I remove that, the selection works. but I will loose the related functionality that I had implemented with that ref and that worked fine with previous versions.

@github-actions github-actions bot added status: waiting for maintainer These issues haven't been looked at yet by a maintainer and removed status: waiting for author Issue with insufficient information labels Feb 23, 2024
@mj12albert
Copy link
Member

mj12albert commented Feb 23, 2024

@smellai You're right ~ refs should be passed to the option through the hook instead of directly:

const CountryOption = memo(({ option }: CountryOptionProps) => {
  const optionRef = useRef<HTMLLIElement>(null);

  const { getRootProps, highlighted } = useOption({
    value: option.value,
    label: option.label,
    disabled: false,
    rootRef: optionRef, // <------------------
  });

  const countryCallingCode = useMemo(
    () => getCountryCallingCode(option.value),
    [option.value]
  );
  return (
    <li
      {...getRootProps()} // <--- custom ref is here
      className={classNames('option', { ['highlighted']: highlighted })}
    >
      <span>{option.label}</span>
      <span>+{countryCallingCode}</span>
    </li>
  );
});

Demo: https://stackblitz.com/edit/stackblitz-starters-2pd8ej?file=src%2FApp.tsx

@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 Feb 23, 2024
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.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Feb 23, 2024
@smellai
Copy link
Author

smellai commented Feb 23, 2024

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: select This is the name of the generic UI component, not the React module! package: base-ui Specific to @mui/base support: Stack Overflow Please ask the community on Stack Overflow
Projects
None yet
Development

No branches or pull requests

3 participants