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

fix: open options list on input focus [MDS-1378] #219

Merged
merged 19 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
4a18d4f
Merge branch 'main' of github.com:coingaming/moon-light
MarioGranada Oct 14, 2024
ea06077
Merge branch 'main' of github.com:coingaming/moon-light
MarioGranada Oct 14, 2024
32ecf7b
Merge branch 'main' of github.com:coingaming/moon-light
MarioGranada Nov 26, 2024
0a65aca
Merge branch 'main' of github.com:coingaming/moon-light
MarioGranada Nov 26, 2024
074a112
fix: MDS-1378 add external control for options display
MarioGranada Nov 26, 2024
4235b3c
feat: MDS-1378 add display options external control example
MarioGranada Nov 27, 2024
b89c900
feat: MDS-1378 minor update
MarioGranada Nov 27, 2024
cc054fc
faet: MDS-1378 opening options on focus - working
MarioGranada Nov 27, 2024
86b172b
feat: MDS-1378 remove unneeded example
MarioGranada Nov 27, 2024
f335602
fix: MDS-1378 use headless ui insiders - working
MarioGranada Nov 27, 2024
372e108
feat: MDS-1378 on focus working approach - working
MarioGranada Nov 28, 2024
a0f77f9
feat: MDS-1378 use on focus added feature on visual select & inset va…
MarioGranada Nov 28, 2024
a0f9d06
feat: MDS-1378 add changeset file
MarioGranada Nov 28, 2024
f00591e
feat: MDS-1378 minor udpate
MarioGranada Nov 29, 2024
9e5b008
fix: MDS-1378 avoid selecting the first active option - working
MarioGranada Dec 5, 2024
2ae148b
Prettified Code!
MarioGranada Dec 5, 2024
6653623
fix: MDS-1378 fix introduced minor bug
MarioGranada Dec 5, 2024
826851c
Merge branch 'MDS-1378' of github.com:coingaming/moon-light into MDS-…
MarioGranada Dec 5, 2024
ed3a916
fix: MDS-1378 add new fix implementation to inset variants
MarioGranada Dec 5, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/dry-years-sleep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@heathmont/moon-core-tw": minor
---

fix: open options list on input focus [MDS-1378]
68 changes: 57 additions & 11 deletions packages/core/src/combobox/Combobox.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { forwardRef, useEffect, useState } from "react";
import React, { forwardRef, useEffect } from "react";
import {
Combobox as HeadlessCombobox,
Transition as HeadlessTransition,
Expand Down Expand Up @@ -52,6 +52,16 @@ const ComboboxRoot = ({
placement: position,
});

const comboboxButtonRef = React.useRef<HTMLButtonElement | null>(null);

const handleOnFocus: React.FocusEventHandler<HTMLInputElement> = (event) => {
setIsInputFocused(true);
if (event.relatedTarget?.id?.includes("headlessui-combobox-button")) {
return;
}
comboboxButtonRef?.current?.click();
};

const states = {
value: value,
displayValue: displayValue,
Expand All @@ -72,6 +82,8 @@ const ComboboxRoot = ({
setAnchor: setAnchorEl,
setPopper: setPopperEl,
},
comboboxButtonRef,
handleOnFocus,
};

const childArray =
Expand Down Expand Up @@ -152,8 +164,16 @@ const Input = ({
label,
...rest
}: InputProps) => {
const { size, popper, disabled, isError, input, onQueryChange } =
useComboboxContext("Combobox.Input");
const {
size,
popper,
disabled,
isError,
input,
onQueryChange,
handleOnFocus,
} = useComboboxContext("Combobox.Input");

return (
<HeadlessCombobox.Input
onChange={({ target: { value } }) => {
Expand All @@ -171,7 +191,7 @@ const Input = ({
)}
disabled={disabled}
error={isError}
onFocus={() => input?.setIsFocused(true)}
onFocus={handleOnFocus}
onBlur={() => input?.setIsFocused(false)}
aria-label={rest["aria-label"]}
{...rest}
Expand All @@ -188,8 +208,15 @@ const InsetInput = ({
label,
...rest
}: InputProps) => {
const { size, popper, disabled, isError, input, onQueryChange } =
useComboboxContext("Combobox.InsetInput");
const {
size,
popper,
disabled,
isError,
input,
onQueryChange,
handleOnFocus,
} = useComboboxContext("Combobox.InsetInput");
return (
<span className={mergeClassnames("relative", "flex flex-grow w-full")}>
<HeadlessCombobox.Input
Expand All @@ -214,7 +241,7 @@ const InsetInput = ({
"leading-5",
)}
error={isError}
onFocus={() => input?.setIsFocused(true)}
onFocus={handleOnFocus}
onBlur={() => input?.setIsFocused(false)}
aria-label={rest["aria-label"]}
{...rest}
Expand All @@ -235,8 +262,15 @@ const VisualSelectInput = ({
label,
...rest
}: InputProps) => {
const { value, size, popper, disabled, isError, onQueryChange } =
useComboboxContext("Combobox.VisualSelectInput");
const {
value,
size,
popper,
disabled,
isError,
onQueryChange,
handleOnFocus,
} = useComboboxContext("Combobox.VisualSelectInput");
const selected = value as [];

return (
Expand Down Expand Up @@ -276,6 +310,7 @@ const VisualSelectInput = ({
aria-label={rest["aria-label"]}
{...rest}
ref={popper?.setAnchor}
onFocus={handleOnFocus}
/>
</span>
);
Expand All @@ -289,8 +324,10 @@ const Button = ({
["aria-label"]: ariaLabel,
...rest
}: WithChildren<ButtonProps>) => {
const { size, disabled } = useComboboxContext("Combobox.Button");
const { size, disabled, comboboxButtonRef, input } =
useComboboxContext("Combobox.Button");
const ariaLabelValue = ariaLabel ? ariaLabel : open ? "Close" : "Open";

return (
<HeadlessCombobox.Button
className={mergeClassnames(
Expand All @@ -301,6 +338,7 @@ const Button = ({
disabled && "cursor-not-allowed",
className,
)}
ref={comboboxButtonRef}
aria-label={ariaLabelValue}
{...rest}
>
Expand All @@ -313,14 +351,16 @@ const Options = ({
children,
menuWidth,
className,
open,
...rest
}: WithChildren<OptionsProps>) => {
const { popper } = useComboboxContext("Combobox.Options");
return (
const OptionsComponent = (
<HeadlessCombobox.Options
ref={popper?.setPopper}
style={popper?.styles?.popper}
{...popper?.attributes?.popper}
static={open ?? true}
className={mergeClassnames(
menuWidth
? menuWidth
Expand All @@ -333,6 +373,12 @@ const Options = ({
{children}
</HeadlessCombobox.Options>
);

if (open === undefined) {
return OptionsComponent;
}

return open && OptionsComponent;
};

const Option = ({ children, value }: OptionProps) => {
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 @@ -23,6 +23,8 @@ type ComboboxState = {
>;
};
size?: Size;
comboboxButtonRef?: React.MutableRefObject<HTMLButtonElement | null>;
handleOnFocus?: React.FocusEventHandler<HTMLInputElement>;
};

export default ComboboxState;
1 change: 1 addition & 0 deletions packages/core/src/combobox/private/types/OptionsProps.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
type OptionsProps = {
menuWidth?: string;
className?: string;
open?: boolean;
};

export default OptionsProps;
13 changes: 6 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.