ComboBox: Add prop to provide custom behavior on input change in ComboBox
Currently we don't have a way to provide custom behavior for what happens when user types in Combo Box. For example if you want to filter the list of options displayed based on input, we can't do this. Can you please add a prop which allows us to add a customer behavior when user types?
Also can you add a prop which controls if menu should be displayed when user focus in combo box input. Currently we have this if allowFreeform is disabled, but we need it even if free flow text is enabled.
Hi @tabansal, can you use the onPendingValueChanged property for your first question? In our example from the documentation site, we have:
<ComboBox
defaultSelectedKey="C"
label="Single-select ComboBox (uncontrolled, allowFreeform: T, autoComplete: T)"
allowFreeform
autoComplete="on"
options={INITIAL_OPTIONS}
onPendingValueChanged={(option, pendingIndex, pendingValue) => console.log(`Preview value was changed. Pending index: ${pendingIndex}. Pending value: ${pendingValue}.`)
}
/>
When you type something in the ComboBox, you should see "Preview value was changed. Pending index: undefined. Pending value: (what you have typed goes here)."
For you second question, I have a CodePen https://codepen.io/kkjeer/pen/xeVVPX that demonstrates what I think is your desired behavior: when the user focuses on the ComboBox, the menu is opened.
This type of solution is also on our documentation page https://developer.microsoft.com/en-us/fabric#/components/ComboBox (in the button the opens the ComboBox when clicked):
<PrimaryButton
text="Open ComboBox"
onClick={() => {
if (this._basicComboBox.current) {
this._basicComboBox.current.focus(true);
}
}}
/>
onPendingValueChanged is getting triggered multiple times. Apart from input text change it also getting triggered when user hovers over the menu items and also when we change the props of combobox (when we update the comboBox with filtered options). Since it is getting called multiple times & mostly with empty value it reset the filtered options in my case.
Second problem is resolved using onFocus.
Hi @tabansal, I have a CodePen https://codepen.io/kkjeer/pen/xeVVPX which demonstrates the use of onPendingValueChanged to filter the options based on what the user types. The key is to not do anything if the value is undefined, which is the case when the user hovers over the menu items. Please let me know if this does not work for you.
Hello @kkjeer, In your codePen if user deletes all the input text from comboBox it doesn't reset to display all options.
@jspurlin can onPendingValueChanged be used to detect when the user clears the pending value in the ComboBox? The issue here is that onPendingValueChanged is called in scenarios other than the user typing, e.g. when the user hovers over an item. It is continually called with undefined, undefined, undefined, but so far I can't find a way to determine if the pending value is empty.
@tabansal I updated my codepen (https://codepen.io/kkjeer/pen/xeVVPX) with a potential local fix. Please let me know if this does not solve your issue; otherwise, I can create a PR with the fix.
@kkjeer It still doesn't work as onPendingValueChanged is not called with empty value on clearing of input. Thanks for the quick work on PR. I'll wait for the merge.
:tada:This issue was addressed in #8646, which has now been successfully released as [email protected].:tada:
Handy links:
I don't think this issue was addressed. The codepen doesn't quite work (clicking on the ComboBox causes the menu to open and close.)
At least we can fix the "expand on focus" behavior. It should be consistent with Dropdown.
ComboBox already has the same behavior as Dropdown with opening on focus provided that allowFreeform is false. Currently, @joschect's PR makes neither component open on focus under any circumstances, which is a breaking behavior change. So at the very least, the PR should be updated to make openOnFocus default to true when allowFreeform is false. (We probably shouldn't make it always default to true, because that's also a pretty significant behavior change.)
I also question whether we really need a new prop--the API surface for Dropdown and CombBox is already pretty large, and it's already possible to open on focus even when allowFreeform is true using code like this: https://codepen.io/ecraig12345/pen/gJaQYo?editors=0010
Just talked to @joschect and he clarified that the issue here is opening on keyboard focus, not when clicking in it (sorry!). So now I have less objection to the prop, but if we keep it, it should definitely be renamed to reflect that it's about keyboard focus only.
@ecraig12345 In your code pen (https://codepen.io/ecraig12345/pen/gJaQYo?editors=0010) if you click on drop down icon to open menu, it closes immediately. That's the issue we are trying to fix here.
:tada:This issue was addressed in #8927, which has now been successfully released as [email protected].:tada:
Handy links: