Skip to content

Commit

Permalink
update clear combobox example to prevent blur
Browse files Browse the repository at this point in the history
  • Loading branch information
Fercas123 committed Jul 31, 2024
1 parent 3913dbb commit 54e953e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
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

0 comments on commit 54e953e

Please sign in to comment.