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

[ComboBox] Pressing enter when no options are selected does not fire onChange callback #6613

Closed
1 of 2 tasks
nertzy opened this issue Aug 4, 2020 · 4 comments
Closed
1 of 2 tasks

Comments

@nertzy
Copy link

nertzy commented Aug 4, 2020

What package(s) are you using?

  • carbon-components
  • carbon-components-react

Detailed description

In a ComboBox, when I press enter after typing text that matches none of the options, the onChange callback never fires, which means that the drop-down is closed, and the text does not match the selected item. Therefore, the user has no way of knowing that the input represents a different value than is displayed.

Steps to reproduce the issue

See my code sandbox: https://codesandbox.io/s/distracted-keldysh-pwhii?file=/src/index.js

  1. Select an option
  2. Type text that matches none of the options
  3. Press enter
  4. Notice that onChange never fired, so now the input is in an inconsistent state.
@tw15egan
Copy link
Member

tw15egan commented Aug 17, 2020

Wondering if using the data-invalid attribute could be a workaround to this problem?

data-invalid={textValue !== item ? true : null}

https://codesandbox.io/s/misty-butterfly-inyun?file=/src/index.js

@nertzy
Copy link
Author

nertzy commented Aug 19, 2020

That workaround doesn't fit our use case particularly well.

We are already using the invalid state to show invalid values upon saving our form. It would be inconsistent UX for us to also introduce an invalid state dynamically before the user submits.

I'm also not sure if we could show the user a message explaining what is wrong in this case without the message hiding and showing as the user types.

The best thing for our use case would be that some callback fires, so that we can programmatically return the ComboBox to a consistent state. I would prefer one of these options:

  • Hitting "Enter" without selecting a value does nothing (instead of closing the drop-down)
  • Or, hitting "Enter" or has the same effect as hitting "Escape", returning the textbox back to the current selected item's text

Alternatively, if there was some callback that fired, we could handle it in our own code.

@tay1orjones
Copy link
Member

The only way we found around this was to use event delegation. Wrap the ComboBox in a div with a keypress handler and call our onChange handler with the value in the textbox via a ref.

  const comboRef = React.createRef();

  const handleOnKeypress = evt => {
    // Current value of input
    const currentValue = comboRef.current.textInput.current.value.trim();
    if (evt.key === 'Enter' && currentValue) {
      const newItem = {
        id: `sampleNewItemID`,
        text: currentValue,
      };
      handleOnChange({ selectedItem: newItem });
    }
  };

  return (
    <div onKeyDown={evt => handleOnKeypress(evt)}>
      <ComboBox 
        ref={comboRef}
        onChange={handleOnChange}
      />
    </div>
);

@nertzy
Copy link
Author

nertzy commented May 3, 2021

Closed by #8476.

@nertzy nertzy closed this as completed May 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants