jpmorganchase / salt-ds

React UI components built with a focus on accessibility, customisation and ease-of-use
https://www.saltdesignsystem.com
Apache License 2.0
111 stars 78 forks source link

[Combobox] "Input props" support #3263

Open nathanmmiller opened 2 months ago

nathanmmiller commented 2 months ago

Area

UI Components

The problem

The use case is basically as follows: We have a multi-select, free-form-allowed combobox that we want to support the following behavior:

So for example, there could be the following scenario. Items in Combobox: Red, Blue, Baby Blue, Green, Mauve User types: "b" Combobox shows Blue, Baby Blue User arrows down to Blue and selects it "Blue" is selected, input still shows "b" and Combobox still shows Blue, Baby Blue User hits [Esc] Combobox shows all options, input is blank User types M Combobox shows "Mauve" User clicks outside the combobox Combobox input is cleared, only Blue is selected.

The solution

Currently, in the older design system we currently use, there's a concept of "InputProps" that control the Input of the combobox. We use something like this (not real code):

InputProps={{

    disableAddOnBlur: true, //This allows us to ensure that we don't add the typed text as an item when we lose focus

    onInputChange: a function that allows us to control the input text (we use a useState, call it [input, setInput] for this),

    onInputBlur: we both control the popper and reset the input text on this (since we override this we need to control the open state of the combobox) (to reset the input text, we would do for example setInput("").),

    onInputFocus: we open the combobox with this, only necessary because of the note in onInputBlur,

    onKeyDown: we hijack the Esc key to clear the input text here (by doing, for example, setInput("") on event.key = Esc)

}}

Additionally, the combobox itself also has an "inputValue" prop that we then can feed with the "input" state from above. And there's also an "allowFreeForm" that this all ties into, allowing us to actually "select" options that aren't there when the text controlled by "input" is [Enter]'d.

Alternatives and examples

I can provide examples internal to JPMC if someone pings me.

Are you a JPMorgan Chase & Co. employee?

origami-z commented 2 months ago

allowFreeForm is in #2409

Currently Combobox has inputProps which supports some of these

nathanmmiller commented 2 months ago

allowFreeForm is in #2409

Currently Combobox has inputProps which supports some of these

Thank you @origami-z - I've subscribed to #2409 and will patiently await that. The rest of these - are inputProps documented, or rather, is there a list of which of the "some" are supported and which of the others should be part of this feature request?

nathanmmiller commented 2 months ago

OK I've played around with inputProps and also with some of the events and I'm able to do most of this, though there are still some missing pieces. I think this is the list of what's missing to be able to support this use case: