@@ -2,6 +2,7 @@ import { ControlsWithNote, DocsHeader, DomRefTable, Footer } from '@sb/component
2
2
import { Canvas , Meta } from ' @storybook/blocks' ;
3
3
import DialogDomRef from ' ../../webComponents/Dialog/DialogDomRef.json' ;
4
4
import * as ComponentStories from ' ./SelectDialog.stories' ;
5
+ import { MessageStrip , Text } from ' ../..' ;
5
6
6
7
<Meta of = { ComponentStories } />
7
8
@@ -77,6 +78,114 @@ const SelectDialogComponent = () => {
77
78
78
79
### SelectDialog in MultiSelect mode with search
79
80
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 >
81
190
82
191
<Footer />
0 commit comments