Skip to content

Commit

Permalink
fix: MDS-1378 avoid selecting the first active option - working
Browse files Browse the repository at this point in the history
  • Loading branch information
MarioGranada committed Dec 5, 2024
1 parent f00591e commit 9e5b008
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 12 deletions.
2 changes: 1 addition & 1 deletion docs/app/client/combobox/examples/Default.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const Example = () => {
<Combobox.Input
open={open}
placeholder={"Choose a name..."}
displayValue={({ label }) => label}
displayValue={(item) => item?.label}
/>
<Combobox.Button open={open}>
<ControlsChevronDownSmall />
Expand Down
1 change: 1 addition & 0 deletions docs/app/client/combobox/examples/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const Example = () => {
onQueryChange={setQuery0}
className="w-full max-w-xs"
size="sm"
nullable={true}
>
{({ open }) => (
<>
Expand Down
47 changes: 36 additions & 11 deletions packages/core/src/combobox/Combobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,28 +53,49 @@ const ComboboxRoot = ({
});

const comboboxButtonRef = React.useRef<HTMLButtonElement | null>(null);
const blurredRef = React.useRef<boolean>(false);
const prevSelected = React.useRef<unknown | undefined>({});

const handleOnFocus: React.FocusEventHandler<HTMLInputElement> = (event) => {
setIsInputFocused(true);

if (event.relatedTarget?.id?.includes("headlessui-combobox-button")) {
return;
}

comboboxButtonRef?.current?.click();
};

const handleOnKeyDown: React.KeyboardEventHandler<HTMLInputElement> = (
event,
) => {
if (event.key === "Tab") {
blurredRef.current = true;
prevSelected.current = value;
console.log("in here oe key down tab");
}
};

const handleOnBlur: React.FocusEventHandler<HTMLInputElement> = () => {
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,
Expand All @@ -84,6 +105,8 @@ const ComboboxRoot = ({
},
comboboxButtonRef,
handleOnFocus,
handleOnBlur,
handleOnKeyDown,
};

const childArray =
Expand All @@ -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}
Expand Down Expand Up @@ -169,9 +192,10 @@ const Input = ({
popper,
disabled,
isError,
input,
onQueryChange,
handleOnFocus,
handleOnKeyDown,
handleOnBlur,
} = useComboboxContext("Combobox.Input");

return (
Expand All @@ -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}
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/combobox/private/types/ComboboxState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type ComboboxState = {
size?: Size;
comboboxButtonRef?: React.MutableRefObject<HTMLButtonElement | null>;
handleOnFocus?: React.FocusEventHandler<HTMLInputElement>;
handleOnBlur?: React.FocusEventHandler<HTMLInputElement>;
handleOnKeyDown?: React.KeyboardEventHandler<HTMLInputElement>;
};

export default ComboboxState;

0 comments on commit 9e5b008

Please sign in to comment.