Skip to content

Commit 15a5108

Browse files
ThusharaJ07joshwooding
authored andcommitted
changed example to clear all for combobox
1 parent cf40a53 commit 15a5108

File tree

5 files changed

+83
-145
lines changed

5 files changed

+83
-145
lines changed

packages/core/stories/combo-box/combo-box.stories.tsx

Lines changed: 22 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Meta, StoryFn } from "@storybook/react";
22
import {
33
FormField,
4+
Button,
45
FormFieldHelperText,
56
FormFieldLabel,
67
StackLayout,
@@ -11,6 +12,7 @@ import {
1112
Text,
1213
Avatar,
1314
} from "@salt-ds/core";
15+
import { CloseIcon } from "@salt-ds/icons";
1416
import {
1517
CountryCode,
1618
countryMetaMap,
@@ -844,14 +846,11 @@ export const FreeText: StoryFn<ComboBoxProps> = (args) => {
844846
);
845847
};
846848

847-
export const SelectAll: StoryFn<ComboBoxProps> = (args) => {
849+
export const ClearAll: StoryFn<ComboBoxProps> = (args) => {
848850
const [value, setValue] = useState(getTemplateDefaultValue(args));
849851
const [selected, setSelected] = useState<string[]>([]);
850-
const allSelectedOptionValue = "all";
851-
const handleChange = (event: ChangeEvent<HTMLInputElement>) => {
852-
// React 16 backwards compatibility
853-
event.persist();
854852

853+
const handleChange = (event: ChangeEvent<HTMLInputElement>) => {
855854
const value = event.target.value;
856855
setValue(value);
857856
};
@@ -860,72 +859,41 @@ export const SelectAll: StoryFn<ComboBoxProps> = (args) => {
860859
event: SyntheticEvent,
861860
newSelected: string[]
862861
) => {
863-
let newOptionsSelected = [...newSelected];
864-
const wasAllSelected = selected.includes(allSelectedOptionValue);
865-
const isAllSelected = newOptionsSelected.includes(allSelectedOptionValue);
866-
867-
if (wasAllSelected) {
868-
if (isAllSelected) {
869-
newOptionsSelected = newOptionsSelected.filter(
870-
(el) => el !== allSelectedOptionValue
871-
);
872-
} else {
873-
newOptionsSelected = [];
874-
}
875-
} else if (
876-
isAllSelected ||
877-
(!isAllSelected && newOptionsSelected.length === usStates.length)
878-
) {
879-
newOptionsSelected = [...usStates, allSelectedOptionValue];
880-
}
881-
setSelected(newOptionsSelected);
882-
args.onSelectionChange?.(event, newOptionsSelected);
883-
862+
setSelected(newSelected);
863+
args.onSelectionChange?.(event, newSelected);
884864
setValue("");
885865
};
886866

887867
const filteredOptions = usStates.filter((state) =>
888868
state.toLowerCase().includes(value.trim().toLowerCase())
889869
);
890870

871+
const handleClear = () => {
872+
setValue("");
873+
setSelected([]);
874+
};
875+
891876
return (
892877
<ComboBox
893878
{...args}
894879
multiselect
880+
endAdornment={
881+
(value || selected.length > 0) && (
882+
<Button
883+
onClick={handleClear}
884+
aria-label="Clear value"
885+
variant="secondary"
886+
>
887+
<CloseIcon aria-hidden />
888+
</Button>
889+
)
890+
}
895891
selected={selected}
896892
onChange={handleChange}
897893
onSelectionChange={handleSelectionChange}
898894
value={value}
899895
style={{ width: "266px" }}
900896
>
901-
{filteredOptions.length > 1 && (
902-
<div
903-
style={{
904-
position: "sticky",
905-
top: 0,
906-
zIndex: 9,
907-
background: !selected.includes(allSelectedOptionValue)
908-
? "var(--salt-color-white)"
909-
: "",
910-
}}
911-
>
912-
<Option
913-
style={{
914-
borderBottom: "solid",
915-
borderWidth: "1px",
916-
borderColor:
917-
selected.includes(filteredOptions[0]) ||
918-
selected.includes(allSelectedOptionValue)
919-
? "transparent"
920-
: "var(--salt-separable-tertiary-borderColor)",
921-
}}
922-
value={"all"}
923-
key={"all"}
924-
>
925-
Select All
926-
</Option>
927-
</div>
928-
)}
929897
{filteredOptions.map((state) => (
930898
<Option value={state} key={state} />
931899
))}

site/docs/components/combo-box/examples.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,11 @@ Use the `truncate` prop when you are limited on space and want to prevent the mu
178178

179179
</LivePreview>
180180

181-
<LivePreview componentName="combo-box" exampleName="SelectAll" >
181+
<LivePreview componentName="combo-box" exampleName="ClearAll" >
182182

183-
## Select All
183+
## Clear All
184184

185-
In order to achieve the select all functionality, you can pass the first Option as Select All and pass in the logic to handleSelectionChange prop.
185+
In order to achieve the clear all functionality, you can pass a close icon in endAdornment prop.
186186

187187
</LivePreview>
188188
</LivePreviewControls>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { ChangeEvent, ReactElement, useState, SyntheticEvent } from "react";
2+
import { ComboBox, Button, Option } from "@salt-ds/core";
3+
import { shortColorData } from "./exampleData";
4+
import { CloseIcon } from "@salt-ds/icons";
5+
6+
export const ClearAll = (): ReactElement => {
7+
const [value, setValue] = useState("");
8+
const [selected, setSelected] = useState<string[]>([]);
9+
10+
const handleChange = (event: ChangeEvent<HTMLInputElement>) => {
11+
const value = event.target.value;
12+
setValue(value);
13+
};
14+
15+
const handleSelectionChange = (
16+
event: SyntheticEvent,
17+
newSelected: string[]
18+
) => {
19+
setSelected(newSelected);
20+
setValue("");
21+
};
22+
23+
const filteredOptions = shortColorData.filter((data) =>
24+
data.toLowerCase().includes(value.trim().toLowerCase())
25+
);
26+
27+
const handleClear = () => {
28+
setValue("");
29+
setSelected([]);
30+
};
31+
32+
return (
33+
<ComboBox
34+
multiselect
35+
selected={selected}
36+
onChange={handleChange}
37+
onSelectionChange={handleSelectionChange}
38+
value={value}
39+
style={{ width: "266px" }}
40+
endAdornment={
41+
(value || selected.length > 0) && (
42+
<Button
43+
onClick={handleClear}
44+
aria-label="Clear value"
45+
variant="secondary"
46+
>
47+
<CloseIcon aria-hidden />
48+
</Button>
49+
)
50+
}
51+
>
52+
{filteredOptions.map((state) => (
53+
<Option value={state} key={state} />
54+
))}
55+
</ComboBox>
56+
);
57+
};

site/src/examples/combo-box/SelectAll.tsx

Lines changed: 0 additions & 87 deletions
This file was deleted.

site/src/examples/combo-box/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ export * from "./CustomFiltering";
1414
export * from "./ServerSideData";
1515
export * from "./ObjectValues";
1616
export * from "./Truncation";
17-
export * from "./SelectAll";
17+
export * from "./ClearAll";

0 commit comments

Comments
 (0)