From 9e5b00837b3005b4b559fd5bcd99e2f4b80e9386 Mon Sep 17 00:00:00 2001 From: MarioGranada Date: Thu, 5 Dec 2024 16:06:01 +0200 Subject: [PATCH] fix: MDS-1378 avoid selecting the first active option - working --- docs/app/client/combobox/examples/Default.tsx | 2 +- docs/app/client/combobox/examples/Select.tsx | 1 + packages/core/src/combobox/Combobox.tsx | 47 ++++++++++++++----- .../combobox/private/types/ComboboxState.ts | 2 + 4 files changed, 40 insertions(+), 12 deletions(-) diff --git a/docs/app/client/combobox/examples/Default.tsx b/docs/app/client/combobox/examples/Default.tsx index 789c8424..57ac73ca 100644 --- a/docs/app/client/combobox/examples/Default.tsx +++ b/docs/app/client/combobox/examples/Default.tsx @@ -51,7 +51,7 @@ const Example = () => { label} + displayValue={(item) => item?.label} /> diff --git a/docs/app/client/combobox/examples/Select.tsx b/docs/app/client/combobox/examples/Select.tsx index 56bbcdeb..6446d1cf 100644 --- a/docs/app/client/combobox/examples/Select.tsx +++ b/docs/app/client/combobox/examples/Select.tsx @@ -49,6 +49,7 @@ const Example = () => { onQueryChange={setQuery0} className="w-full max-w-xs" size="sm" + nullable={true} > {({ open }) => ( <> diff --git a/packages/core/src/combobox/Combobox.tsx b/packages/core/src/combobox/Combobox.tsx index 7eaf9028..df5eac2a 100644 --- a/packages/core/src/combobox/Combobox.tsx +++ b/packages/core/src/combobox/Combobox.tsx @@ -53,28 +53,49 @@ const ComboboxRoot = ({ }); const comboboxButtonRef = React.useRef(null); + const blurredRef = React.useRef(false); + const prevSelected = React.useRef({}); const handleOnFocus: React.FocusEventHandler = (event) => { setIsInputFocused(true); + if (event.relatedTarget?.id?.includes("headlessui-combobox-button")) { return; } + comboboxButtonRef?.current?.click(); }; + const handleOnKeyDown: React.KeyboardEventHandler = ( + event, + ) => { + if (event.key === "Tab") { + blurredRef.current = true; + prevSelected.current = value; + console.log("in here oe key down tab"); + } + }; + + const handleOnBlur: React.FocusEventHandler = () => { + setIsInputFocused(false); + if (blurredRef.current) { + onChange(prevSelected.current); + } + }; + const states = { - value: value, - displayValue: displayValue, - isError: isError, - size: size, - disabled: disabled, + value, + displayValue, + isError, + size, + disabled, input: { isFocused: isInputFocused, setIsFocused: setIsInputFocused, }, - multiple: multiple, - onClear: onClear, - onQueryChange: onQueryChange, + multiple, + onClear, + onQueryChange, popper: { forceUpdate, styles, @@ -84,6 +105,8 @@ const ComboboxRoot = ({ }, comboboxButtonRef, handleOnFocus, + handleOnBlur, + handleOnKeyDown, }; const childArray = @@ -100,7 +123,7 @@ const ComboboxRoot = ({ // https://codesandbox.io/s/festive-curran-ic7y9n?file=/src/ComboboxMultiple.tsx:527-565 value={value as {}[]} multiple={multiple as true} - nullable={nullable as true} + nullable onChange={onChange} disabled={disabled} ref={ref} @@ -169,9 +192,10 @@ const Input = ({ popper, disabled, isError, - input, onQueryChange, handleOnFocus, + handleOnKeyDown, + handleOnBlur, } = useComboboxContext("Combobox.Input"); return ( @@ -192,7 +216,8 @@ const Input = ({ disabled={disabled} error={isError} onFocus={handleOnFocus} - onBlur={() => input?.setIsFocused(false)} + onKeyDown={handleOnKeyDown} + onBlur={handleOnBlur} aria-label={rest["aria-label"]} {...rest} ref={popper?.setAnchor} diff --git a/packages/core/src/combobox/private/types/ComboboxState.ts b/packages/core/src/combobox/private/types/ComboboxState.ts index 4d1517fe..303bccba 100644 --- a/packages/core/src/combobox/private/types/ComboboxState.ts +++ b/packages/core/src/combobox/private/types/ComboboxState.ts @@ -25,6 +25,8 @@ type ComboboxState = { size?: Size; comboboxButtonRef?: React.MutableRefObject; handleOnFocus?: React.FocusEventHandler; + handleOnBlur?: React.FocusEventHandler; + handleOnKeyDown?: React.KeyboardEventHandler; }; export default ComboboxState;