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

feat: onInputValueChange #733

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 8 additions & 1 deletion src/components/input-elements/MultiSelect/MultiSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface MultiSelectProps extends React.HTMLAttributes<HTMLDivElement> {
defaultValue?: string[];
value?: string[];
onValueChange?: (value: string[]) => void;
onInputValueChange?: (value: string) => void;
placeholder?: string;
placeholderSearch?: string;
disabled?: boolean;
Expand All @@ -31,6 +32,7 @@ const MultiSelect = React.forwardRef<HTMLDivElement, MultiSelectProps>((props, r
defaultValue,
value,
onValueChange,
onInputValueChange,
placeholder = "Select...",
placeholderSearch = "Search",
disabled = false,
Expand Down Expand Up @@ -60,6 +62,11 @@ const MultiSelect = React.forwardRef<HTMLDivElement, MultiSelectProps>((props, r
onValueChange?.([]);
};

const handleInputValueChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setSearchQuery(e.target.value);
onInputValueChange?.(e.target.value);
};

return (
<Listbox
as="div"
Expand Down Expand Up @@ -278,7 +285,7 @@ const MultiSelect = React.forwardRef<HTMLDivElement, MultiSelectProps>((props, r
e.stopPropagation();
}
}}
onChange={(e) => setSearchQuery(e.target.value)}
onChange={handleInputValueChange}
/>
</div>
<SelectedValueContext.Provider value={{ selectedValue: value }}>
Expand Down
9 changes: 8 additions & 1 deletion src/components/input-elements/SearchSelect/SearchSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface SearchSelectProps extends React.HTMLAttributes<HTMLDivElement>
defaultValue?: string;
value?: string;
onValueChange?: (value: string) => void;
onInputValueChange?: (value: string) => void;
placeholder?: string;
disabled?: boolean;
icon?: React.ElementType | React.JSXElementConstructor<any>;
Expand All @@ -29,6 +30,7 @@ const SearchSelect = React.forwardRef<HTMLDivElement, SearchSelectProps>((props,
defaultValue,
value,
onValueChange,
onInputValueChange,
placeholder = "Select...",
disabled = false,
icon,
Expand All @@ -46,6 +48,11 @@ const SearchSelect = React.forwardRef<HTMLDivElement, SearchSelectProps>((props,
[searchQuery, children],
);

const handleInputValueChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setSearchQuery(e.target.value);
onInputValueChange?.(e.target.value);
};

return (
<Combobox
as="div"
Expand Down Expand Up @@ -105,7 +112,7 @@ const SearchSelect = React.forwardRef<HTMLDivElement, SearchSelectProps>((props,
getSelectButtonColors(hasValue(value), disabled),
)}
placeholder={placeholder}
onChange={(event) => setSearchQuery(event.target.value)}
onChange={handleInputValueChange}
displayValue={(value: string) => valueToNameMapping.get(value) ?? ""}
/>
<div
Expand Down
15 changes: 15 additions & 0 deletions src/stories/input-elements/MultiSelect.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,19 @@ const WithControlledStateTemplate: ComponentStory<typeof MultiSelect> = () => {
);
};

const WithOnInputValueChangeTemplate: ComponentStory<typeof MultiSelect> = () => {
const [input, setInput] = useState<string>("");
return (
<Card>
<Text>Input:{input}</Text>
<MultiSelect onInputValueChange={(value) => setInput(value)}>
<MultiSelectItem value={"1"}>One</MultiSelectItem>
<MultiSelectItem value={"2"}>Two</MultiSelectItem>
</MultiSelect>
</Card>
);
};

export const DefaultResponsive = ResponsiveTemplate.bind({});

export const WithFlexParent = FlexTemplate.bind({});
Expand Down Expand Up @@ -159,3 +172,5 @@ WithDisabled.args = {
export const SelectElementsComparison = SelectElementsFlexTemplate.bind({});

export const WithControlledState = WithControlledStateTemplate.bind({});

export const WithOnInputValueChange = WithOnInputValueChangeTemplate.bind({});
14 changes: 14 additions & 0 deletions src/stories/input-elements/SearchSelect.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ const WithControlledStateTemplate: ComponentStory<typeof SearchSelect> = () => {
);
};

const WithOnInputValueChangeTemplate: ComponentStory<typeof SearchSelect> = () => {
const [input, setInput] = useState<string>("");
return (
<Card>
<Text>Input:{input}</Text>
<SearchSelect onInputValueChange={(value) => setInput(value)}>
<SearchSelectItem value={"1"}>One</SearchSelectItem>
</SearchSelect>
</Card>
);
};

export const DefaultResponsive = ResponsiveTemplate.bind({});
DefaultResponsive.args = {
onValueChange: (v) => alert(v),
Expand Down Expand Up @@ -123,3 +135,5 @@ WithDisabled.args = {
export const SelectElementsComparison = SelectElementsFlexTemplate.bind({});

export const WithControlledState = WithControlledStateTemplate.bind({});

export const WithOnInputValueChange = WithOnInputValueChangeTemplate.bind({});
Loading