Skip to content

Commit b0c9013

Browse files
authored
docs(SelectDialog): fix multi-select story (#5143)
Fixes #5104
1 parent 1c15df5 commit b0c9013

File tree

2 files changed

+120
-7
lines changed

2 files changed

+120
-7
lines changed

packages/main/src/components/SelectDialog/SelectDialog.mdx

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ControlsWithNote, DocsHeader, DomRefTable, Footer } from '@sb/component
22
import { Canvas, Meta } from '@storybook/blocks';
33
import DialogDomRef from '../../webComponents/Dialog/DialogDomRef.json';
44
import * as ComponentStories from './SelectDialog.stories';
5+
import { MessageStrip, Text } from '../..';
56

67
<Meta of={ComponentStories} />
78

@@ -77,6 +78,114 @@ const SelectDialogComponent = () => {
7778

7879
### SelectDialog in MultiSelect mode with search
7980

80-
<Canvas of={ComponentStories.MultiSelect} />
81+
<MessageStrip
82+
hideCloseButton
83+
children={
84+
<Text>
85+
This example remembers its selection by setting <code>rememberSelections</code> to <code>true</code>.
86+
</Text>
87+
}
88+
/>
89+
90+
<Canvas of={ComponentStories.MultiSelect} sourceState="none" />
91+
92+
<details>
93+
94+
<summary>Show Code</summary>
95+
96+
```tsx
97+
const MultiSelectDialog = () => {
98+
const dialogRef = useRef(null);
99+
// predefined selection
100+
const selectedProducts = { 'HT-102': true, 'HT-203': true, 'HT-1038': true };
101+
// number of selected items
102+
const [selectedItems, setSelectedItems] = useState<Record<string, boolean>>(selectedProducts);
103+
const [searchVal, setSearchVal] = useState();
104+
const [products, setProducts] = useState(Object.keys(selectedProducts));
105+
106+
const onButtonClick = () => {
107+
dialogRef.current.show();
108+
};
109+
110+
// search
111+
const handleSearch = (e) => {
112+
setSearchVal(e.detail.value);
113+
};
114+
const handleSearchReset = () => {
115+
setSearchVal(undefined);
116+
};
117+
const handleItemClick = (e) => {
118+
const itemDescription = e.detail.item.dataset.description;
119+
setSelectedItems((prev) => {
120+
if (prev[itemDescription]) {
121+
const { [itemDescription]: _omit, ...rest } = prev;
122+
return rest;
123+
} else {
124+
return { ...prev, [itemDescription]: true };
125+
}
126+
});
127+
};
128+
// clear selection
129+
const handleClear = () => {
130+
setSelectedItems({});
131+
};
132+
// confirm selection
133+
const handleConfirm = () => {
134+
setProducts(Object.keys(selectedItems));
135+
};
136+
137+
return (
138+
<>
139+
<Button onClick={onButtonClick}>Open Dialog</Button>
140+
<SelectDialog
141+
mode={ListMode.MultiSelect}
142+
ref={dialogRef}
143+
onSearchInput={handleSearch}
144+
onSearch={handleSearch}
145+
onSearchReset={handleSearchReset}
146+
numberOfSelectedItems={Object.keys(selectedItems).length}
147+
listProps={{ onItemClick: handleItemClick }}
148+
showClearButton
149+
rememberSelections
150+
onClear={handleClear}
151+
onConfirm={handleConfirm}
152+
>
153+
{new Array(40)
154+
.fill('')
155+
.map((_, index) => {
156+
const currentProduct = listItems[index % 4];
157+
const description = `${currentProduct.description}${index}`;
158+
const lowerCaseSearchVal = searchVal?.toLowerCase();
159+
if (
160+
searchVal &&
161+
!description.toLowerCase().includes(lowerCaseSearchVal) &&
162+
!currentProduct.text.toLowerCase().includes(lowerCaseSearchVal)
163+
) {
164+
return null;
165+
}
166+
return (
167+
<StandardListItem
168+
image={currentProduct.img}
169+
description={`${currentProduct.description}${index}`}
170+
data-description={`${currentProduct.description}${index}`}
171+
key={`${currentProduct.text}${index}`}
172+
selected={selectedItems[description]}
173+
>
174+
{currentProduct.text}
175+
</StandardListItem>
176+
);
177+
})
178+
.filter(Boolean)}
179+
</SelectDialog>
180+
<FlexBox>
181+
<Label>Selected: </Label>
182+
<Text>{products.join(', ')}</Text>
183+
</FlexBox>
184+
</>
185+
);
186+
};
187+
```
188+
189+
</details>
81190

82191
<Footer />

packages/main/src/components/SelectDialog/SelectDialog.stories.tsx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,21 +74,24 @@ export const Default: Story = {
7474
export const MultiSelect: Story = {
7575
render: () => {
7676
const dialogRef = useRef(null);
77+
// predefined selection
78+
const selectedProducts = { 'HT-102': true, 'HT-203': true, 'HT-1038': true };
79+
// number of selected items
80+
const [selectedItems, setSelectedItems] = useState<Record<string, boolean>>(selectedProducts);
81+
const [searchVal, setSearchVal] = useState();
82+
const [products, setProducts] = useState(Object.keys(selectedProducts));
83+
7784
const onButtonClick = () => {
7885
dialogRef.current.show();
7986
};
80-
const [searchVal, setSearchVal] = useState();
87+
8188
// search
8289
const handleSearch = (e) => {
8390
setSearchVal(e.detail.value);
8491
};
8592
const handleSearchReset = () => {
8693
setSearchVal(undefined);
8794
};
88-
// predefined selection
89-
const selectedProducts = { 'HT-102': true, 'HT-203': true, 'HT-1038': true };
90-
// number of selected items
91-
const [selectedItems, setSelectedItems] = useState<Record<string, boolean>>(selectedProducts);
9295
const handleItemClick = (e) => {
9396
const itemDescription = e.detail.item.dataset.description;
9497
setSelectedItems((prev) => {
@@ -105,10 +108,10 @@ export const MultiSelect: Story = {
105108
setSelectedItems({});
106109
};
107110
// confirm selection
108-
const [products, setProducts] = useState(Object.keys(selectedProducts));
109111
const handleConfirm = () => {
110112
setProducts(Object.keys(selectedItems));
111113
};
114+
112115
return (
113116
<>
114117
<Button onClick={onButtonClick}>Open Dialog</Button>
@@ -121,6 +124,7 @@ export const MultiSelect: Story = {
121124
numberOfSelectedItems={Object.keys(selectedItems).length}
122125
listProps={{ onItemClick: handleItemClick }}
123126
showClearButton
127+
rememberSelections
124128
onClear={handleClear}
125129
onConfirm={handleConfirm}
126130
>

0 commit comments

Comments
 (0)