@salutejs/sdds-api-tests 0.5.0-next-sbcom.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@salutejs/sdds-api-tests",
3
- "version": "0.5.0-next-sbcom.0",
3
+ "version": "0.5.0",
4
4
  "description": "API tests for components",
5
5
  "author": "Salute Frontend Team <salute.developers@gmail.com>",
6
6
  "license": "MIT",
@@ -13,19 +13,19 @@
13
13
  "test": "rm -rf tests && node script.mjs && vitest run --config ./vitest.config.ts"
14
14
  },
15
15
  "devDependencies": {
16
- "@salutejs/plasma-b2c": "1.616.0-next-sbcom.0",
17
- "@salutejs/plasma-giga": "0.343.0-next-sbcom.0",
18
- "@salutejs/plasma-icons": "1.236.0-next-sbcom.0",
19
- "@salutejs/plasma-web": "1.618.0-next-sbcom.0",
20
- "@salutejs/sdds-bizcom": "0.348.0-next-sbcom.0",
21
- "@salutejs/sdds-cs": "0.352.0-next-sbcom.0",
22
- "@salutejs/sdds-dfa": "0.346.0-next-sbcom.0",
23
- "@salutejs/sdds-finai": "0.339.0-next-sbcom.0",
24
- "@salutejs/sdds-insol": "0.343.0-next-sbcom.0",
25
- "@salutejs/sdds-netology": "0.347.0-next-sbcom.0",
26
- "@salutejs/sdds-platform-ai": "0.347.0-next-sbcom.0",
27
- "@salutejs/sdds-scan": "0.346.0-next-sbcom.0",
28
- "@salutejs/sdds-serv": "0.347.0-next-sbcom.0",
16
+ "@salutejs/plasma-b2c": "1.616.0",
17
+ "@salutejs/plasma-giga": "0.343.0",
18
+ "@salutejs/plasma-icons": "1.236.0",
19
+ "@salutejs/plasma-web": "1.618.0",
20
+ "@salutejs/sdds-bizcom": "0.348.0",
21
+ "@salutejs/sdds-cs": "0.352.0",
22
+ "@salutejs/sdds-dfa": "0.346.0",
23
+ "@salutejs/sdds-finai": "0.339.0",
24
+ "@salutejs/sdds-insol": "0.343.0",
25
+ "@salutejs/sdds-netology": "0.347.0",
26
+ "@salutejs/sdds-platform-ai": "0.347.0",
27
+ "@salutejs/sdds-scan": "0.346.0",
28
+ "@salutejs/sdds-serv": "0.347.0",
29
29
  "@types/react": "18.0.28",
30
30
  "@types/react-dom": "18.0.11",
31
31
  "react": "18.2.0",
@@ -34,5 +34,5 @@
34
34
  "publishConfig": {
35
35
  "access": "public"
36
36
  },
37
- "gitHead": "412a3110ff7287ee9b0351c2e4b43afe6f99bb73"
37
+ "gitHead": "19666107f034de0f50f0109e1a24faf0d05d6fcb"
38
38
  }
@@ -86,3 +86,58 @@ describe('Complex', () => {
86
86
  expectTypeOf<ButtonProps>({ text: 'Hello', value: 'Plasma', stretching: 'filled', contentPlacing: 'default' });
87
87
  });
88
88
  });
89
+
90
+ describe('Polymorphic (as prop)', () => {
91
+ it('as="a" adds anchor props', () => {
92
+ expectTypeOf(<Button as="a" href="/link" target="_blank" />);
93
+ });
94
+
95
+ it('as="a" keeps own props', () => {
96
+ expectTypeOf(<Button as="a" text="text" disabled />);
97
+ expectTypeOf(<Button as="a" text="text" isLoading />);
98
+ });
99
+
100
+ it('as custom component adds its props', () => {
101
+ type LinkProps = { to: string; replace?: boolean; children?: React.ReactNode };
102
+ const Link = (_: LinkProps) => null;
103
+
104
+ expectTypeOf(<Button as={Link} to="/link" replace text="text" />);
105
+ });
106
+
107
+ it('as custom component does not add props absent in that component', () => {
108
+ type LinkProps = { to: string; children?: React.ReactNode };
109
+ const Link = (_: LinkProps) => null;
110
+
111
+ // @ts-expect-error
112
+ expectTypeOf(<Button as={Link} href="/link" replace text="text" />);
113
+ });
114
+
115
+ it('as="input" adds input props', () => {
116
+ expectTypeOf(<Button as="input" type="submit" />);
117
+ expectTypeOf(<Button as="input" type="reset" placeholder="..." onChange={() => {}} />);
118
+ });
119
+
120
+ it('as="input" keeps own props', () => {
121
+ expectTypeOf(<Button as="input" text="text" disabled />);
122
+ expectTypeOf(<Button as="input" text="text" isLoading />);
123
+ });
124
+
125
+ it('as="input" does not add anchor props', () => {
126
+ // @ts-expect-error
127
+ expectTypeOf(<Button as="input" href="/link" target="_blank" />);
128
+ });
129
+
130
+ it('as="div" adds div props', () => {
131
+ expectTypeOf(<Button as="div" tabIndex={0} contentEditable />);
132
+ });
133
+
134
+ it('as="div" keeps own props', () => {
135
+ expectTypeOf(<Button as="div" text="text" disabled />);
136
+ expectTypeOf(<Button as="div" text="text" isLoading />);
137
+ });
138
+
139
+ it('as="div" does not add anchor props', () => {
140
+ // @ts-expect-error
141
+ expectTypeOf(<Button as="div" href="/link" target="_blank" />);
142
+ });
143
+ });
@@ -0,0 +1,543 @@
1
+ import * as React from 'react';
2
+ import type {
3
+ AriaRole,
4
+ ChangeEventHandler,
5
+ ComponentProps,
6
+ CSSProperties,
7
+ ReactElement,
8
+ ReactNode,
9
+ RefObject,
10
+ } from 'react';
11
+ import { useRef, useState } from 'react';
12
+ import { describe, it } from 'vitest';
13
+ import { expectTypeOf } from 'expect-type';
14
+ import { Button, Cell, Select } from '@salutejs/plasma-b2c';
15
+
16
+ type SelectProps = ComponentProps<typeof Select>;
17
+
18
+ type ItemOption = {
19
+ value: string;
20
+ label: string;
21
+ placement?: 'top' | 'bottom' | 'right' | 'left' | 'auto' | Array<'top' | 'bottom' | 'right' | 'left'>;
22
+ items?: Array<ItemOption>;
23
+ disabled?: boolean;
24
+ contentLeft?: ReactNode;
25
+ contentRight?: ReactNode;
26
+ className?: string;
27
+ listMaxHeight?: CSSProperties['height'];
28
+ };
29
+
30
+ describe('Basics', () => {
31
+ it('Common', () => {
32
+ // @ts-expect-error
33
+ expectTypeOf<SelectProps>({});
34
+ expectTypeOf<SelectProps>({ items: [] });
35
+ expectTypeOf<SelectProps>().toHaveProperty('items').toEqualTypeOf<ItemOption[]>();
36
+ expectTypeOf<SelectProps>()
37
+ .toHaveProperty('placement')
38
+ .toEqualTypeOf<
39
+ 'top' | 'bottom' | 'right' | 'left' | 'auto' | Array<'top' | 'bottom' | 'right' | 'left'> | undefined
40
+ >();
41
+ expectTypeOf<SelectProps>().toHaveProperty('treeView').toEqualTypeOf<boolean | undefined>();
42
+ expectTypeOf<SelectProps>().toHaveProperty('arrowPlacement').toEqualTypeOf<'left' | 'right' | undefined>();
43
+ expectTypeOf<SelectProps>().toHaveProperty('disabled').toEqualTypeOf<boolean | undefined>();
44
+ expectTypeOf<SelectProps>().toHaveProperty('readOnly').toEqualTypeOf<boolean | undefined>();
45
+ expectTypeOf<SelectProps>().toHaveProperty('variant').toEqualTypeOf<'normal' | 'tight' | undefined>();
46
+ expectTypeOf<SelectProps>().toHaveProperty('zIndex').toEqualTypeOf<CSSProperties['zIndex'] | undefined>();
47
+ expectTypeOf<SelectProps>()
48
+ .toHaveProperty('listMaxHeight')
49
+ .toEqualTypeOf<CSSProperties['height'] | undefined>();
50
+ expectTypeOf<SelectProps>().toHaveProperty('listWidth').toEqualTypeOf<CSSProperties['width'] | undefined>();
51
+ expectTypeOf<SelectProps>()
52
+ .toHaveProperty('portal')
53
+ .toEqualTypeOf<string | RefObject<HTMLElement> | undefined>();
54
+ expectTypeOf<SelectProps>()
55
+ .toHaveProperty('renderValue')
56
+ .toEqualTypeOf<((item: ItemOption) => string) | undefined>();
57
+ expectTypeOf<SelectProps>()
58
+ .toHaveProperty('renderItem')
59
+ .toEqualTypeOf<((item: ItemOption) => ReactNode) | undefined>();
60
+ expectTypeOf<SelectProps>()
61
+ .toHaveProperty('renderSelectionIcon')
62
+ .toEqualTypeOf<((selected: boolean | 'indeterminate') => ReactNode) | undefined>();
63
+ expectTypeOf<SelectProps>().toHaveProperty('closeAfterSelect').toEqualTypeOf<boolean | undefined>();
64
+ expectTypeOf<SelectProps>().toHaveProperty('beforeList').toEqualTypeOf<ReactNode | undefined>();
65
+ expectTypeOf<SelectProps>().toHaveProperty('afterList').toEqualTypeOf<ReactNode | undefined>();
66
+ expectTypeOf<SelectProps>().toHaveProperty('virtual').toEqualTypeOf<boolean | undefined>();
67
+ expectTypeOf<SelectProps>()
68
+ .toHaveProperty('onScroll')
69
+ .toEqualTypeOf<((e: React.UIEvent<HTMLElement>) => void) | undefined>();
70
+ expectTypeOf<SelectProps>().toHaveProperty('onToggle').toEqualTypeOf<((isOpen: boolean) => void) | undefined>();
71
+ expectTypeOf<SelectProps>().toHaveProperty('mode').toEqualTypeOf<'default' | 'radio' | undefined>();
72
+ expectTypeOf<SelectProps>().toHaveProperty('flip').toEqualTypeOf<boolean | undefined>();
73
+ expectTypeOf<SelectProps>().toHaveProperty('shift').toEqualTypeOf<boolean | undefined>();
74
+ expectTypeOf<SelectProps>().toHaveProperty('singleLine').toEqualTypeOf<boolean | undefined>();
75
+ expectTypeOf<SelectProps>().toHaveProperty('emptyStateDescription').toEqualTypeOf<ReactNode | undefined>();
76
+
77
+ expectTypeOf<SelectProps>()
78
+ .toHaveProperty('target')
79
+ .toEqualTypeOf<'button-like' | 'textfield-like' | undefined>();
80
+ expectTypeOf<SelectProps>().toHaveProperty('contentLeft').toEqualTypeOf<ReactNode | undefined>();
81
+ expectTypeOf<SelectProps>().toHaveProperty('contentRight').toEqualTypeOf<ReactElement | undefined>();
82
+ expectTypeOf<SelectProps>().toHaveProperty('placeholder').toEqualTypeOf<string | undefined>();
83
+ expectTypeOf<SelectProps>().toHaveProperty('helperText').toEqualTypeOf<string | undefined>();
84
+ expectTypeOf<SelectProps>().toHaveProperty('chipType').toEqualTypeOf<'default' | 'text' | undefined>();
85
+ expectTypeOf<SelectProps>().toHaveProperty('chipClickArea').toEqualTypeOf<'full' | 'close-icon' | undefined>();
86
+ expectTypeOf<SelectProps>().toHaveProperty('label').toEqualTypeOf<string | undefined>();
87
+ expectTypeOf<SelectProps>().toHaveProperty('keepPlaceholder').toEqualTypeOf<boolean | undefined>();
88
+
89
+ expectTypeOf<SelectProps>().toHaveProperty('required').toEqualTypeOf<boolean | undefined>();
90
+ expectTypeOf<SelectProps>().toHaveProperty('requiredPlacement').toEqualTypeOf<'left' | 'right' | undefined>();
91
+ expectTypeOf<SelectProps>().toHaveProperty('optional').toEqualTypeOf<boolean | undefined>();
92
+ expectTypeOf<SelectProps>().toHaveProperty('optionalText').toEqualTypeOf<string | undefined>();
93
+ expectTypeOf<SelectProps>().toHaveProperty('hasRequiredIndicator').toEqualTypeOf<boolean | undefined>();
94
+
95
+ expectTypeOf<SelectProps>().toHaveProperty('multiselect').toEqualTypeOf<boolean | undefined>();
96
+ expectTypeOf<SelectProps>().toHaveProperty('value').toEqualTypeOf<string | string[] | undefined>();
97
+ expectTypeOf<SelectProps>().toHaveProperty('defaultValue').toEqualTypeOf<string | string[] | undefined>();
98
+ expectTypeOf<SelectProps>().toHaveProperty('name').toEqualTypeOf<string | undefined>();
99
+ expectTypeOf<SelectProps>().toHaveProperty('isTargetAmount').toEqualTypeOf<true | false | undefined>();
100
+ expectTypeOf<SelectProps>()
101
+ .toHaveProperty('renderTarget')
102
+ .toEqualTypeOf<
103
+ | ((value: ItemOption, opened?: boolean) => ReactNode)
104
+ | ((value: ItemOption[], opened?: boolean) => ReactNode)
105
+ | undefined
106
+ >();
107
+ expectTypeOf<SelectProps>().toHaveProperty('selectAllOptions').toEqualTypeOf<
108
+ | {
109
+ checked?: boolean;
110
+ indeterminate?: boolean;
111
+ label?: string;
112
+ onClick?: () => void;
113
+ sticky?: boolean;
114
+ }
115
+ | undefined
116
+ >();
117
+ expectTypeOf<SelectProps>()
118
+ .toHaveProperty('onChange')
119
+ .toEqualTypeOf<
120
+ | ((value: string, item: ItemOption | null) => void)
121
+ | ((value: string[], item: ItemOption | null) => void)
122
+ | ChangeEventHandler<HTMLSelectElement>
123
+ | undefined
124
+ >();
125
+ });
126
+
127
+ it('Variations', () => {
128
+ type View = NonNullable<SelectProps['view']>;
129
+ expectTypeOf<View>().toExtend<string>();
130
+ expectTypeOf<string>().not.toExtend<View>();
131
+
132
+ type Size = NonNullable<SelectProps['size']>;
133
+ expectTypeOf<Size>().toExtend<string>();
134
+ expectTypeOf<string>().not.toExtend<Size>();
135
+
136
+ type LabelPlacement = NonNullable<SelectProps['labelPlacement']>;
137
+ expectTypeOf<LabelPlacement>().toExtend<string>();
138
+ expectTypeOf<string>().not.toExtend<LabelPlacement>();
139
+
140
+ type ChipView = NonNullable<SelectProps['chipView']>;
141
+ expectTypeOf<ChipView>().toExtend<string>();
142
+ expectTypeOf<string>().not.toExtend<ChipView>();
143
+
144
+ type HintView = NonNullable<SelectProps['hintView']>;
145
+ expectTypeOf<HintView>().toExtend<string>();
146
+ expectTypeOf<string>().not.toExtend<HintView>();
147
+
148
+ type HintSize = NonNullable<SelectProps['hintSize']>;
149
+ expectTypeOf<HintSize>().toExtend<string>();
150
+ expectTypeOf<string>().not.toExtend<HintSize>();
151
+ });
152
+
153
+ it('HTMLButtonElement', () => {
154
+ expectTypeOf<SelectProps>().toHaveProperty('id').toEqualTypeOf<string | undefined>();
155
+ expectTypeOf<SelectProps>().toHaveProperty('className').toEqualTypeOf<string | undefined>();
156
+ expectTypeOf<SelectProps>().toHaveProperty('style').toEqualTypeOf<CSSProperties | undefined>();
157
+ expectTypeOf<SelectProps>()
158
+ .toHaveProperty('onClick')
159
+ .toEqualTypeOf<React.MouseEventHandler<HTMLButtonElement> | undefined>();
160
+ expectTypeOf<SelectProps>()
161
+ .toHaveProperty('onMouseEnter')
162
+ .toEqualTypeOf<React.MouseEventHandler<HTMLButtonElement> | undefined>();
163
+ expectTypeOf<SelectProps>()
164
+ .toHaveProperty('onMouseLeave')
165
+ .toEqualTypeOf<React.MouseEventHandler<HTMLButtonElement> | undefined>();
166
+ expectTypeOf<SelectProps>()
167
+ .toHaveProperty('onFocus')
168
+ .toEqualTypeOf<React.FocusEventHandler<HTMLButtonElement> | undefined>();
169
+ expectTypeOf<SelectProps>()
170
+ .toHaveProperty('onBlur')
171
+ .toEqualTypeOf<React.FocusEventHandler<HTMLButtonElement> | undefined>();
172
+ expectTypeOf<SelectProps>()
173
+ .toHaveProperty('onKeyDown')
174
+ .toEqualTypeOf<React.KeyboardEventHandler<HTMLButtonElement> | undefined>();
175
+ expectTypeOf<SelectProps>().toHaveProperty('aria-label').toEqualTypeOf<string | undefined>();
176
+ expectTypeOf<SelectProps>().toHaveProperty('role').toEqualTypeOf<AriaRole | undefined>();
177
+ });
178
+ });
179
+
180
+ describe('Unions', () => {
181
+ it('Target', () => {
182
+ expectTypeOf<SelectProps>({ items: [], target: 'textfield-like', contentLeft: null, helperText: 'helper' });
183
+ expectTypeOf<SelectProps>({ items: [], target: 'button-like', label: 'Label', placeholder: 'Choose value' });
184
+
185
+ // @ts-expect-error
186
+ expectTypeOf<SelectProps>({ items: [], target: 'button-like', contentLeft: null });
187
+ // @ts-expect-error
188
+ expectTypeOf<SelectProps>({ items: [], target: 'button-like', contentRight: <div /> });
189
+ // @ts-expect-error
190
+ expectTypeOf<SelectProps>({ items: [], target: 'button-like', helperText: 'helper' });
191
+ // @ts-expect-error
192
+ expectTypeOf<SelectProps>({ items: [], target: 'button-like', required: true });
193
+ // @ts-expect-error
194
+ expectTypeOf<SelectProps>({ items: [], target: 'button-like', hintText: 'hint' });
195
+ });
196
+
197
+ it('IsMultiselect', () => {
198
+ expectTypeOf<SelectProps>({ items: [], value: '' });
199
+ expectTypeOf<SelectProps>({ items: [], onChange: (value: string) => value });
200
+ expectTypeOf<SelectProps>({ items: [], multiselect: true, value: [] });
201
+ expectTypeOf<SelectProps>({ items: [], multiselect: true, value: ['value'] });
202
+ expectTypeOf<SelectProps>({ items: [], multiselect: true, isTargetAmount: true });
203
+ expectTypeOf<SelectProps>({ items: [], multiselect: true, onChange: (value: string[]) => value });
204
+ expectTypeOf<SelectProps>({ items: [], multiselect: true, selectAllOptions: { label: 'All' } });
205
+ expectTypeOf<SelectProps>({ items: [], name: 'country' });
206
+ expectTypeOf<SelectProps>({ items: [], name: 'country', defaultValue: '', multiselect: false });
207
+ expectTypeOf<SelectProps>({ items: [], name: 'country', defaultValue: ['value'], multiselect: true });
208
+ expectTypeOf<SelectProps>({
209
+ items: [],
210
+ name: 'country',
211
+ onChange: (e: React.ChangeEvent<HTMLSelectElement>) => e.target.value,
212
+ });
213
+
214
+ // @ts-expect-error
215
+ expectTypeOf<SelectProps>({ items: [], multiselect: false, value: [] });
216
+ // @ts-expect-error
217
+ expectTypeOf<SelectProps>({ items: [], multiselect: true, value: '' });
218
+ // @ts-expect-error
219
+ expectTypeOf<SelectProps>({ items: [], multiselect: false, onChange: (value: string[]) => value });
220
+ // @ts-expect-error
221
+ expectTypeOf<SelectProps>({ items: [], multiselect: true, onChange: (value: string) => value });
222
+ // @ts-expect-error
223
+ expectTypeOf<SelectProps>({ items: [], isTargetAmount: true });
224
+ // @ts-expect-error
225
+ expectTypeOf<SelectProps>({ items: [], selectAllOptions: { label: 'All' } });
226
+ // @ts-expect-error
227
+ expectTypeOf<SelectProps>({ items: [], name: 'country', value: '' });
228
+ // @ts-expect-error
229
+ expectTypeOf<SelectProps>({ items: [], name: 'country', defaultValue: '', onChange: (value: string) => value });
230
+ });
231
+
232
+ it('HintProps', () => {
233
+ expectTypeOf<SelectProps>({ items: [], hintText: 'hint' });
234
+ expectTypeOf<SelectProps>({ items: [], hintText: 'hint', hintTrigger: 'hover' });
235
+ expectTypeOf<SelectProps>({ items: [], hintText: 'hint', hintHasArrow: true });
236
+ expectTypeOf<SelectProps>({ items: [], hintText: 'hint', hintOffset: [0, 8] });
237
+
238
+ // @ts-expect-error
239
+ expectTypeOf<SelectProps>({ items: [], hintTrigger: 'hover' });
240
+ // @ts-expect-error
241
+ expectTypeOf<SelectProps>({ items: [], hintHasArrow: true });
242
+ // @ts-expect-error
243
+ expectTypeOf<SelectProps>({ items: [], hintOffset: [0, 8] });
244
+ });
245
+ });
246
+
247
+ describe('Generics', () => {
248
+ it('ItemOption', () => {
249
+ const items = [{ value: '1', label: 'One', customLabel: 'Custom', isAvailable: true }];
250
+
251
+ void (
252
+ <Select
253
+ items={items}
254
+ onChange={(_, item) => item?.customLabel}
255
+ renderItem={(item) => item?.customLabel}
256
+ renderValue={(item) => item?.customLabel}
257
+ />
258
+ );
259
+
260
+ void (
261
+ <Select
262
+ multiselect
263
+ items={items}
264
+ onChange={(_, item) => item?.customLabel}
265
+ renderItem={(item) => item?.customLabel}
266
+ renderValue={(item) => item?.customLabel}
267
+ />
268
+ );
269
+
270
+ void (
271
+ <Select
272
+ items={items}
273
+ // @ts-expect-error
274
+ renderItem={(item) => item?.nonExistedProp}
275
+ // @ts-expect-error
276
+ renderValue={(item) => item?.nonExistedProp}
277
+ // @ts-expect-error
278
+ onChange={(_, item) => item?.nonExistedProp}
279
+ />
280
+ );
281
+
282
+ void (
283
+ <Select
284
+ multiselect
285
+ items={items}
286
+ // @ts-expect-error
287
+ renderItem={(item) => item?.nonExistedProp}
288
+ // @ts-expect-error
289
+ renderValue={(item) => item?.nonExistedProp}
290
+ // @ts-expect-error
291
+ onChange={(_, item) => item?.nonExistedProp}
292
+ />
293
+ );
294
+ });
295
+ });
296
+
297
+ describe('Examples', () => {
298
+ const items = [
299
+ {
300
+ value: 'north_america',
301
+ label: 'Северная Америка',
302
+ },
303
+ ];
304
+
305
+ it('Textfield', () => {
306
+ () => {
307
+ const [singleValue, setSingleValue] = useState('');
308
+ const [multipleValue, setMultipleValue] = useState<string[]>([]);
309
+
310
+ return (
311
+ <>
312
+ <Select
313
+ items={items}
314
+ value={singleValue}
315
+ onChange={setSingleValue}
316
+ label="Single"
317
+ placeholder="Placeholder"
318
+ helperText="Helper text"
319
+ />
320
+
321
+ <Select
322
+ multiselect
323
+ items={items}
324
+ value={multipleValue}
325
+ onChange={setMultipleValue}
326
+ label="Multiselect"
327
+ placeholder="Placeholder"
328
+ helperText="Helper text"
329
+ />
330
+ </>
331
+ );
332
+ };
333
+ });
334
+
335
+ it('Button-like', () => {
336
+ () => {
337
+ const [singleValue, setSingleValue] = useState('');
338
+ const [multipleValue, setMultipleValue] = useState<string[]>([]);
339
+
340
+ return (
341
+ <>
342
+ <Select
343
+ items={items}
344
+ label="Single"
345
+ target="button-like"
346
+ value={singleValue}
347
+ onChange={setSingleValue}
348
+ />
349
+
350
+ <Select
351
+ multiselect
352
+ items={items}
353
+ label="Multiple"
354
+ target="button-like"
355
+ value={multipleValue}
356
+ onChange={setMultipleValue}
357
+ />
358
+ </>
359
+ );
360
+ };
361
+ });
362
+
363
+ it('Predefined', () => {
364
+ () => {
365
+ const [multipleValue, setMultipleValue] = useState<string[]>(['brazil', 'north_america']);
366
+
367
+ return (
368
+ <>
369
+ <Button onClick={() => setMultipleValue([])}>Очистить</Button>
370
+
371
+ <Select
372
+ multiselect
373
+ items={items}
374
+ placeholder="Placeholder"
375
+ value={multipleValue}
376
+ onChange={setMultipleValue}
377
+ />
378
+ </>
379
+ );
380
+ };
381
+ });
382
+
383
+ it('RenderValue', () => {
384
+ () => {
385
+ const [singleValue, setSingleValue] = useState('');
386
+ const renderValue = (item: ItemOption) => `${item.value} - ${item.label}`;
387
+
388
+ return (
389
+ <Select
390
+ items={items}
391
+ label="Label"
392
+ placeholder="Placeholder"
393
+ value={singleValue}
394
+ onChange={setSingleValue}
395
+ renderValue={renderValue}
396
+ />
397
+ );
398
+ };
399
+ });
400
+
401
+ it('RenderItem', () => {
402
+ () => {
403
+ const [multipleValue, setMultipleValue] = useState<string[]>([]);
404
+ const renderItem = ({ label }: ItemOption) => (
405
+ <Cell
406
+ view="default"
407
+ title={label}
408
+ label="Top left"
409
+ contentRight={<Cell view="default" title="Bottom right" label="Top right" />}
410
+ />
411
+ );
412
+
413
+ return (
414
+ <Select
415
+ placeholder="Placeholder"
416
+ items={items}
417
+ value={multipleValue}
418
+ onChange={setMultipleValue}
419
+ multiselect
420
+ renderItem={renderItem}
421
+ />
422
+ );
423
+ };
424
+ });
425
+
426
+ it('RenderSelectionIcon', () => {
427
+ () => {
428
+ const [value, setValue] = useState('brazil');
429
+
430
+ const renderSelectionIcon = (selected: boolean | 'indeterminate') => {
431
+ if (selected === true) {
432
+ return <div />;
433
+ }
434
+
435
+ if (selected === 'indeterminate') {
436
+ return <span />;
437
+ }
438
+
439
+ return null;
440
+ };
441
+
442
+ return <Select items={items} value={value} onChange={setValue} renderSelectionIcon={renderSelectionIcon} />;
443
+ };
444
+ });
445
+
446
+ it('Portal', () => {
447
+ () => {
448
+ const [value, setValue] = useState('');
449
+ const ref = useRef<HTMLDivElement>(null);
450
+
451
+ return (
452
+ <div ref={ref}>
453
+ <Select
454
+ items={items}
455
+ label="Label"
456
+ placeholder="Placeholder"
457
+ value={value}
458
+ onChange={setValue}
459
+ portal={ref}
460
+ />
461
+ </div>
462
+ );
463
+ };
464
+ });
465
+
466
+ it('Uncontrolled', () => {
467
+ () => {
468
+ return <Select items={items} label="Label" placeholder="Placeholder" />;
469
+ };
470
+ });
471
+
472
+ it('Virtual', () => {
473
+ () => {
474
+ const virtualItems = Array(5000)
475
+ .fill(1)
476
+ .map((_, i) => ({ value: i.toString(), label: i.toString() }));
477
+
478
+ return (
479
+ <Select
480
+ items={virtualItems}
481
+ virtual
482
+ listMaxHeight="200px"
483
+ placeholder="Placeholder"
484
+ label="Label"
485
+ helperText="Helper text"
486
+ />
487
+ );
488
+ };
489
+ });
490
+
491
+ it('Infinite loading', () => {
492
+ () => {
493
+ const [infiniteItems, setInfiniteItems] = useState(items);
494
+ const [isInfiniteLoading, setIsInfiniteLoading] = useState(false);
495
+
496
+ const handleScroll = async (e: React.UIEvent<HTMLElement>) => {
497
+ if (isInfiniteLoading) return;
498
+
499
+ if (e.currentTarget.scrollTop + e.currentTarget.offsetHeight + 10 > e.currentTarget.scrollHeight) {
500
+ setIsInfiniteLoading(true);
501
+ setInfiniteItems((prev) => [...prev, ...items]);
502
+ setIsInfiniteLoading(false);
503
+ }
504
+ };
505
+
506
+ return (
507
+ <Select
508
+ items={infiniteItems}
509
+ listMaxHeight="200px"
510
+ placeholder="Placeholder"
511
+ label="Label"
512
+ helperText="Helper text"
513
+ onScroll={handleScroll}
514
+ afterList={isInfiniteLoading ? <center>Загружаю...</center> : undefined}
515
+ />
516
+ );
517
+ };
518
+ });
519
+
520
+ it('SelectAll', () => {
521
+ () => {
522
+ const [value, setValue] = useState<string[]>([]);
523
+
524
+ return (
525
+ <Select
526
+ multiselect
527
+ items={items}
528
+ value={value}
529
+ onChange={setValue}
530
+ label="Multiselect"
531
+ placeholder="Placeholder"
532
+ selectAllOptions={{
533
+ checked: value.length === items.length,
534
+ indeterminate: value.length > 0 && value.length < items.length,
535
+ label: 'Выбрать все',
536
+ sticky: true,
537
+ }}
538
+ isTargetAmount
539
+ />
540
+ );
541
+ };
542
+ });
543
+ });