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

Combobox clear example update #3914

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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 packages/core/stories/combo-box/combo-box.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,13 @@ export const ClearSelection: StoryFn<ComboBoxProps> = (args) => {
state.toLowerCase().includes(value.trim().toLowerCase()),
);

const handleClear = () => {
const handleBlur = () => {
console.log("blur was called");
};
const handleClear = (event: any) => {
if (!event.currentTarget?.contains(event.relatedTarget)) {
event.stopPropagation();
}
setValue("");
setSelected([]);
};
Expand All @@ -876,6 +882,7 @@ export const ClearSelection: StoryFn<ComboBoxProps> = (args) => {
<ComboBox
{...args}
multiselect
onBlur={handleBlur}
endAdornment={
(value || selected.length > 0) && (
<Button
Expand Down
1 change: 1 addition & 0 deletions site/docs/components/combo-box/examples.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ Use the `selectOnTab` prop to select the active option from the list on Tab key

In order to clear all the selections, you can pass a [`Button`](../button) to the `endAdornment` prop of the `ComboBox`.

**Note:** To prevent the clear selection action from triggering a blur event, you can add `event.stopPropagation()` to your event handler.
</LivePreview>
<LivePreview componentName="combo-box" exampleName="Bordered">

Expand Down
9 changes: 8 additions & 1 deletion site/src/examples/combo-box/ClearSelection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,21 @@ export const ClearSelection = (): ReactElement => {
data.toLowerCase().includes(value.trim().toLowerCase()),
);

const handleClear = () => {
const handleBlur = () => {
console.log("blur was called");
};
const handleClear = (event: any) => {
if (!event.currentTarget?.contains(event.relatedTarget)) {
event.stopPropagation();
}
setValue("");
setSelected([]);
};

return (
<ComboBox
multiselect
onBlur={handleBlur}
selected={selected}
onChange={handleChange}
onSelectionChange={handleSelectionChange}
Expand Down
Loading