@true-engineering/true-react-common-ui-kit 2.5.0 → 2.7.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.
Files changed (233) hide show
  1. package/README.md +26 -0
  2. package/dist/components/Icon/icons-list.d.ts +1 -1
  3. package/dist/components/Input/Input.d.ts +2 -2
  4. package/dist/components/List/List.d.ts +1 -1
  5. package/dist/components/List/List.styles.d.ts +0 -28
  6. package/dist/components/List/index.d.ts +0 -1
  7. package/dist/components/ListItem/ListItem.d.ts +6 -0
  8. package/dist/components/ListItem/ListItem.styles.d.ts +35 -0
  9. package/dist/components/ListItem/constants.d.ts +1 -0
  10. package/dist/components/ListItem/index.d.ts +3 -0
  11. package/dist/components/{List → ListItem}/types.d.ts +5 -0
  12. package/dist/components/MoreMenu/MoreMenu.d.ts +2 -2
  13. package/dist/components/index.d.ts +1 -0
  14. package/dist/helpers/index.d.ts +1 -0
  15. package/dist/true-react-common-ui-kit.js +175 -75
  16. package/dist/true-react-common-ui-kit.js.map +1 -1
  17. package/dist/true-react-common-ui-kit.umd.cjs +176 -76
  18. package/dist/true-react-common-ui-kit.umd.cjs.map +1 -1
  19. package/dist/types.d.ts +1 -1
  20. package/package.json +93 -93
  21. package/src/components/AccountInfo/AccountInfo.stories.tsx +32 -32
  22. package/src/components/AccountInfo/AccountInfo.tsx +76 -76
  23. package/src/components/AccountInfo/constants.ts +1 -1
  24. package/src/components/AddButton/AddButton.tsx +48 -48
  25. package/src/components/Button/Button.stories.tsx +56 -56
  26. package/src/components/Button/Button.tsx +153 -154
  27. package/src/components/Button/constants.ts +9 -9
  28. package/src/components/Button/index.ts +3 -3
  29. package/src/components/Button/types.ts +5 -5
  30. package/src/components/Checkbox/Checkbox.stories.tsx +32 -32
  31. package/src/components/Checkbox/Checkbox.tsx +105 -105
  32. package/src/components/CloseButton/CloseButton.styles.ts +34 -34
  33. package/src/components/CloseButton/CloseButton.tsx +35 -35
  34. package/src/components/Colors/Colors.tsx +26 -26
  35. package/src/components/CssBaseline/CssBaseline.tsx +15 -15
  36. package/src/components/DateInput/DateInput.stories.tsx +61 -61
  37. package/src/components/DateInput/DateInput.tsx +82 -82
  38. package/src/components/DatePicker/DatePicker.stories.tsx +87 -87
  39. package/src/components/DatePicker/DatePicker.styles.ts +44 -44
  40. package/src/components/DatePicker/DatePicker.tsx +309 -309
  41. package/src/components/DatePicker/components/DatePickerHeader/DatePickerHeader.styles.ts +84 -84
  42. package/src/components/DatePicker/components/DatePickerHeader/DatePickerHeader.tsx +79 -79
  43. package/src/components/DatePicker/components/DatePickerHeader/index.ts +2 -2
  44. package/src/components/DatePicker/components/PopperContainer/PopperContainer.tsx +6 -6
  45. package/src/components/DatePicker/components/PopperContainer/index.ts +1 -1
  46. package/src/components/DatePicker/components/index.ts +2 -2
  47. package/src/components/DatePicker/constants.ts +6 -6
  48. package/src/components/DatePicker/helpers.ts +23 -23
  49. package/src/components/DatePicker/index.ts +4 -4
  50. package/src/components/DatePicker/types.ts +45 -45
  51. package/src/components/Description/Description.stories.tsx +27 -27
  52. package/src/components/Description/Description.tsx +59 -59
  53. package/src/components/Description/constants.ts +1 -1
  54. package/src/components/FiltersPane/FiltersPane.stories.tsx +295 -295
  55. package/src/components/FiltersPane/FiltersPane.tsx +150 -150
  56. package/src/components/FiltersPane/components/Filter/Filter.tsx +203 -203
  57. package/src/components/FiltersPane/components/Filter/index.ts +1 -1
  58. package/src/components/FiltersPane/components/FilterInterval/FilterInterval.styles.ts +64 -64
  59. package/src/components/FiltersPane/components/FilterInterval/FilterInterval.tsx +141 -141
  60. package/src/components/FiltersPane/components/FilterInterval/index.ts +2 -2
  61. package/src/components/FiltersPane/components/FilterMultiSelect/FilterMultiSelect.tsx +10 -10
  62. package/src/components/FiltersPane/components/FilterMultiSelect/index.ts +1 -1
  63. package/src/components/FiltersPane/components/FilterSelect/FilterSelect.styles.ts +143 -143
  64. package/src/components/FiltersPane/components/FilterSelect/FilterSelect.tsx +346 -346
  65. package/src/components/FiltersPane/components/FilterSelect/index.ts +2 -2
  66. package/src/components/FiltersPane/components/FilterValueView/FilterValueView.styles.tsx +15 -15
  67. package/src/components/FiltersPane/components/FilterValueView/FilterValueView.tsx +163 -163
  68. package/src/components/FiltersPane/components/FilterValueView/index.tsx +2 -2
  69. package/src/components/FiltersPane/components/FilterWithDates/FilterWithDates.styles.ts +60 -60
  70. package/src/components/FiltersPane/components/FilterWithDates/FilterWithDates.tsx +181 -181
  71. package/src/components/FiltersPane/components/FilterWithDates/index.ts +2 -2
  72. package/src/components/FiltersPane/components/FilterWithPeriod/FilterWithPeriod.styles.ts +17 -17
  73. package/src/components/FiltersPane/components/FilterWithPeriod/FilterWithPeriod.tsx +174 -174
  74. package/src/components/FiltersPane/components/FilterWithPeriod/index.ts +2 -2
  75. package/src/components/FiltersPane/components/FilterWrapper/FilterWrapper.styles.ts +110 -110
  76. package/src/components/FiltersPane/components/FilterWrapper/FilterWrapper.tsx +149 -149
  77. package/src/components/FiltersPane/components/FilterWrapper/index.ts +2 -2
  78. package/src/components/FiltersPane/components/FiltersPaneSearch/FiltersPaneSearch.styles.ts +109 -109
  79. package/src/components/FiltersPane/components/FiltersPaneSearch/FiltersPaneSearch.tsx +155 -155
  80. package/src/components/FiltersPane/components/FiltersPaneSearch/index.ts +2 -2
  81. package/src/components/FiltersPane/components/index.ts +9 -9
  82. package/src/components/FiltersPane/constants.ts +137 -137
  83. package/src/components/FiltersPane/helpers.ts +26 -26
  84. package/src/components/FiltersPane/index.ts +5 -5
  85. package/src/components/FiltersPane/types.ts +156 -156
  86. package/src/components/Flag/Flag.stories.tsx +29 -29
  87. package/src/components/Flag/Flag.tsx +27 -27
  88. package/src/components/FlexibleTable/FlexibleTable.stories.tsx +338 -338
  89. package/src/components/FlexibleTable/FlexibleTable.styles.ts +131 -131
  90. package/src/components/FlexibleTable/FlexibleTable.tsx +205 -205
  91. package/src/components/FlexibleTable/components/TableRow/TableRow.tsx +152 -152
  92. package/src/components/FlexibleTable/components/TableRow/index.ts +1 -1
  93. package/src/components/FlexibleTable/components/TableValue/TableValue.tsx +74 -74
  94. package/src/components/FlexibleTable/components/TableValue/index.ts +1 -1
  95. package/src/components/FlexibleTable/components/index.ts +2 -2
  96. package/src/components/FlexibleTable/constants.ts +1 -1
  97. package/src/components/FlexibleTable/types.ts +52 -52
  98. package/src/components/Icon/Icon.stories.tsx +86 -86
  99. package/src/components/Icon/Icon.tsx +27 -27
  100. package/src/components/Icon/complexIcons/icons.ts +5 -5
  101. package/src/components/Icon/components/ComplexIconBoilerplate/ComplexIconBoilerplate.tsx +16 -16
  102. package/src/components/Icon/components/ComplexIconBoilerplate/index.ts +1 -1
  103. package/src/components/Icon/components/IconBolerplate/IconBoilerplate.tsx +43 -43
  104. package/src/components/Icon/components/IconBolerplate/index.ts +1 -1
  105. package/src/components/Icon/components/index.ts +2 -2
  106. package/src/components/Icon/helpers.ts +9 -9
  107. package/src/components/Icon/icons-list.ts +856 -826
  108. package/src/components/Icon/index.ts +4 -4
  109. package/src/components/Icon/types.ts +16 -16
  110. package/src/components/IncrementInput/IncrementInput.stories.tsx +31 -31
  111. package/src/components/IncrementInput/IncrementInput.tsx +80 -80
  112. package/src/components/IncrementInput/components/ChangeButton/ChangeButton.tsx +33 -33
  113. package/src/components/IncrementInput/components/ChangeButton/index.ts +1 -1
  114. package/src/components/IncrementInput/components/index.ts +1 -1
  115. package/src/components/Input/Input.stories.tsx +86 -86
  116. package/src/components/Input/Input.tsx +308 -308
  117. package/src/components/Input/constants.ts +1 -1
  118. package/src/components/Input/index.ts +3 -3
  119. package/src/components/Input/types.ts +6 -6
  120. package/src/components/List/List.stories.tsx +63 -63
  121. package/src/components/List/List.styles.ts +0 -38
  122. package/src/components/List/List.tsx +36 -54
  123. package/src/components/List/index.ts +2 -3
  124. package/src/components/ListItem/ListItem.stories.tsx +67 -0
  125. package/src/components/ListItem/ListItem.styles.ts +48 -0
  126. package/src/components/ListItem/ListItem.tsx +44 -0
  127. package/src/components/ListItem/constants.ts +5 -0
  128. package/src/components/ListItem/index.ts +3 -0
  129. package/src/components/{List → ListItem}/types.ts +19 -13
  130. package/src/components/Modal/Modal.stories.tsx +105 -105
  131. package/src/components/Modal/Modal.styles.ts +305 -305
  132. package/src/components/Modal/Modal.tsx +184 -184
  133. package/src/components/Modal/index.ts +3 -3
  134. package/src/components/Modal/types.ts +17 -17
  135. package/src/components/MoreMenu/MoreMenu.styles.ts +70 -70
  136. package/src/components/MoreMenu/MoreMenu.tsx +90 -90
  137. package/src/components/MultiSelect/MultiSelect.stories.tsx +46 -46
  138. package/src/components/MultiSelect/MultiSelect.tsx +92 -92
  139. package/src/components/MultiSelect/components/MultiSelectInput/MultiSelectInput.styles.ts +73 -73
  140. package/src/components/MultiSelect/components/MultiSelectInput/MultiSelectInput.tsx +51 -51
  141. package/src/components/MultiSelect/components/MultiSelectInput/index.ts +2 -2
  142. package/src/components/MultiSelect/components/index.ts +1 -1
  143. package/src/components/MultiSelect/index.ts +4 -4
  144. package/src/components/MultiSelect/types.ts +1 -1
  145. package/src/components/MultiSelectList/MultiSelectList.styles.ts +124 -124
  146. package/src/components/MultiSelectList/MultiSelectList.tsx +441 -441
  147. package/src/components/MultiSelectList/constants.ts +21 -21
  148. package/src/components/MultiSelectList/helpers.ts +11 -11
  149. package/src/components/MultiSelectList/index.ts +3 -3
  150. package/src/components/MultiSelectList/types.ts +15 -15
  151. package/src/components/Notification/Notification.stories.tsx +46 -46
  152. package/src/components/Notification/Notification.styles.ts +50 -50
  153. package/src/components/Notification/Notification.tsx +78 -78
  154. package/src/components/Notification/index.ts +3 -3
  155. package/src/components/Notification/types.ts +1 -1
  156. package/src/components/NumberInput/NumberInput.stories.tsx +35 -35
  157. package/src/components/NumberInput/NumberInput.tsx +133 -133
  158. package/src/components/NumberInput/helpers.ts +86 -86
  159. package/src/components/PhoneInput/PhoneInput.stories.tsx +70 -70
  160. package/src/components/PhoneInput/PhoneInput.tsx +193 -193
  161. package/src/components/PhoneInput/components/PhoneInputCountryList/PhoneInputCountryList.stories.tsx +21 -21
  162. package/src/components/PhoneInput/components/PhoneInputCountryList/PhoneInputCountryList.styles.ts +100 -100
  163. package/src/components/PhoneInput/components/PhoneInputCountryList/PhoneInputCountryList.tsx +145 -145
  164. package/src/components/PhoneInput/components/PhoneInputCountryList/index.ts +2 -2
  165. package/src/components/PhoneInput/components/index.ts +1 -1
  166. package/src/components/PhoneInput/constants.ts +3 -3
  167. package/src/components/PhoneInput/index.ts +5 -5
  168. package/src/components/PhoneInput/phone-info.ts +2147 -2147
  169. package/src/components/RadioButton/RadioButton.stories.tsx +46 -46
  170. package/src/components/RadioButton/RadioButton.tsx +55 -55
  171. package/src/components/ScrollIntoViewIfNeeded/ScrollIntoViewIfNeeded.ts +54 -54
  172. package/src/components/ScrollIntoViewIfNeeded/constants.ts +12 -12
  173. package/src/components/SearchInput/SearchInput.stories.tsx +23 -23
  174. package/src/components/SearchInput/SearchInput.styles.ts +50 -50
  175. package/src/components/SearchInput/SearchInput.tsx +51 -51
  176. package/src/components/Select/MultiSelect.stories.tsx +240 -240
  177. package/src/components/Select/Select.stories.tsx +235 -235
  178. package/src/components/Select/Select.tsx +574 -575
  179. package/src/components/Select/components/SelectList/SelectList.styles.ts +72 -72
  180. package/src/components/Select/components/SelectList/SelectList.tsx +158 -158
  181. package/src/components/Select/components/SelectList/index.ts +2 -2
  182. package/src/components/Select/components/SelectListItem/SelectListItem.styles.ts +14 -14
  183. package/src/components/Select/components/SelectListItem/SelectListItem.tsx +68 -68
  184. package/src/components/Select/components/SelectListItem/index.ts +1 -1
  185. package/src/components/Select/components/index.ts +2 -2
  186. package/src/components/Select/helpers.ts +26 -26
  187. package/src/components/Select/index.ts +4 -4
  188. package/src/components/SmartInput/SmartInput.stories.tsx +51 -51
  189. package/src/components/SmartInput/SmartInput.tsx +124 -124
  190. package/src/components/SmartInput/constants.ts +84 -84
  191. package/src/components/SmartInput/helpers.ts +13 -13
  192. package/src/components/SmartInput/index.ts +2 -2
  193. package/src/components/SmartInput/types.ts +11 -11
  194. package/src/components/Switch/Switch.stories.tsx +40 -40
  195. package/src/components/Switch/Switch.tsx +79 -79
  196. package/src/components/Switch/index.ts +3 -3
  197. package/src/components/Switch/types.ts +4 -4
  198. package/src/components/TextArea/TextArea.stories.tsx +35 -35
  199. package/src/components/TextArea/TextArea.tsx +174 -174
  200. package/src/components/TextWithInfo/TextWithInfo.stories.tsx +53 -53
  201. package/src/components/TextWithInfo/TextWithInfo.tsx +60 -60
  202. package/src/components/TextWithTooltip/TextWithTooltip.tsx +143 -143
  203. package/src/components/ThemedPreloader/ThemedPreloader.stories.tsx +41 -41
  204. package/src/components/ThemedPreloader/ThemedPreloader.tsx +47 -47
  205. package/src/components/ThemedPreloader/components/DefaultPreloader/DefaultPreloader.tsx +29 -29
  206. package/src/components/ThemedPreloader/components/DotsPreloader/DotsPreloader.tsx +18 -18
  207. package/src/components/ThemedPreloader/components/SvgPreloader/SvgPreloader.tsx +25 -25
  208. package/src/components/ThemedPreloader/components/index.ts +3 -3
  209. package/src/components/ThemedPreloader/constants.ts +1 -1
  210. package/src/components/ThemedPreloader/index.ts +4 -4
  211. package/src/components/ThemedPreloader/types.ts +3 -3
  212. package/src/components/Toaster/Toaster.stories.tsx +30 -30
  213. package/src/components/Toaster/Toaster.tsx +108 -108
  214. package/src/components/Toaster/constants.ts +1 -1
  215. package/src/components/Toaster/index.ts +3 -3
  216. package/src/components/Toaster/types.ts +1 -1
  217. package/src/components/Tooltip/Tooltip.stories.tsx +19 -19
  218. package/src/components/Tooltip/Tooltip.styles.ts +45 -45
  219. package/src/components/Tooltip/Tooltip.tsx +35 -35
  220. package/src/components/Tooltip/index.ts +3 -3
  221. package/src/components/index.ts +1 -0
  222. package/src/helpers/deprecated.ts +23 -23
  223. package/src/helpers/index.ts +5 -4
  224. package/src/helpers/misc.ts +158 -158
  225. package/src/helpers/phone.ts +87 -87
  226. package/src/helpers/snippets.tsx +6 -6
  227. package/src/hooks/use-did-mount-effect.ts +18 -18
  228. package/src/hooks/use-dropdown.ts +82 -82
  229. package/src/hooks/use-on-click-outside.ts +77 -77
  230. package/src/hooks/use-theme.ts +32 -32
  231. package/src/hooks/use-tweak-styles.ts +13 -13
  232. package/src/theme.ts +149 -149
  233. package/src/types.ts +109 -108
@@ -1,441 +1,441 @@
1
- import { useEffect, useState, useMemo, useRef, useCallback, ReactNode } from 'react';
2
- import clsx from 'clsx';
3
- import { debounce } from 'ts-debounce';
4
- import { addDataAttributes } from '../../helpers';
5
- import { useIsMounted, useTheme, useTweakStyles } from '../../hooks';
6
- import { ICommonProps } from '../../types';
7
- import { Button } from '../Button';
8
- import { Checkbox, ICheckboxProps } from '../Checkbox';
9
- import { SearchInput } from '../SearchInput';
10
- import { ThemedPreloader } from '../ThemedPreloader';
11
- import { DEFAULT_LOCALE, MultiSelectLocales } from './constants';
12
- import { defaultConvertFunction, getLocale } from './helpers';
13
- import { IMultiSelectListValues, IMultiSelectLocale, IMultiSelectLocaleKey } from './types';
14
- import { MultiSelectListStyles, styles } from './MultiSelectList.styles';
15
-
16
- export interface IMultiSelectListProps<Value, Option> extends ICommonProps {
17
- tweakStyles?: MultiSelectListStyles;
18
- value?: Value;
19
- isSearchEnabled?: boolean;
20
- width?: number | string;
21
- localeKey?: IMultiSelectLocaleKey;
22
- locale?: Partial<IMultiSelectLocale>;
23
- onClose?: () => void;
24
- onChange: (val?: Value) => void;
25
- options?: Option[];
26
- fetchOptions?: (val?: string, page?: number) => Promise<Option[]>;
27
- getValueView?: (val: Option) => ReactNode;
28
- getValueId?: (val: Option) => string;
29
- getValueString?: (val: Option) => string;
30
- isGroupingEnabled?: boolean;
31
- checkboxPosition?: 'left' | 'right';
32
- pageSize?: number;
33
- searchMaxLength?: number;
34
- testId?: string;
35
- }
36
-
37
- export function MultiSelectList<Value extends IMultiSelectListValues<Option>, Option = string>({
38
- value,
39
- tweakStyles,
40
- data,
41
- isSearchEnabled = false,
42
- width,
43
- localeKey = DEFAULT_LOCALE,
44
- locale = MultiSelectLocales[localeKey],
45
- onChange,
46
- onClose,
47
- options,
48
- fetchOptions,
49
- isGroupingEnabled = isSearchEnabled,
50
- getValueView = defaultConvertFunction,
51
- getValueId = defaultConvertFunction,
52
- getValueString = defaultConvertFunction,
53
- checkboxPosition = 'left',
54
- pageSize,
55
- searchMaxLength,
56
- testId,
57
- }: IMultiSelectListProps<Value, Option>): JSX.Element {
58
- const isMounted = useIsMounted();
59
- const { classes, componentStyles } = useTheme('MultiSelectList', styles, tweakStyles, { width });
60
-
61
- const tweakCheckboxStyles = useTweakStyles(componentStyles, tweakStyles, 'tweakCheckbox');
62
-
63
- const tweakSearchInputStyles = useTweakStyles(componentStyles, tweakStyles, 'tweakSearchInput');
64
-
65
- const tweakClearButtonStyles = useTweakStyles(componentStyles, tweakStyles, 'tweakClearButton');
66
-
67
- const translates = useMemo(() => getLocale(localeKey, locale), [localeKey, locale]);
68
-
69
- const chosenValues = value?.include;
70
- const chosenValuesIds = useMemo(() => chosenValues?.map(getValueId), [getValueId, chosenValues]);
71
-
72
- const [keyCursorOn, setKeyCursorOn] = useState(0);
73
- const [searchValue, setSearchValue] = useState('');
74
- // Опции, которые получаем с бека при изменении строки поиска
75
- const [allOptions, setAllOptions] = useState<Option[]>([]);
76
-
77
- const [isLoading, setIsLoading] = useState(false);
78
- const [isLoadingOptionsOnScroll, setLoadingOptionsOnScroll] = useState(false);
79
-
80
- const [activePage, setActivePage] = useState(0);
81
- const [isMaxPage, setIsMaxPage] = useState(false);
82
-
83
- const observer = useRef<IntersectionObserver>();
84
-
85
- const unchosenOptions = useMemo(
86
- () => allOptions.filter((v) => !chosenValuesIds?.includes(getValueId(v))),
87
- [getValueId, allOptions, chosenValuesIds],
88
- );
89
-
90
- const handleSearchInputChange = useCallback(
91
- async (inputValue?: string) => {
92
- if (fetchOptions === undefined) {
93
- return;
94
- }
95
- setIsLoading(true);
96
-
97
- try {
98
- const response = await fetchOptions(inputValue?.trim() ?? '');
99
- if (isMounted() && response !== undefined) {
100
- setAllOptions(response);
101
- setActivePage(0);
102
- setIsMaxPage(
103
- response.length === 0 || (pageSize !== undefined && response.length < pageSize),
104
- );
105
- }
106
- } finally {
107
- if (isMounted()) {
108
- setIsLoading(false);
109
- }
110
- }
111
- },
112
- [fetchOptions, pageSize],
113
- );
114
-
115
- const changeSearchValue = useCallback(debounce(handleSearchInputChange), [
116
- handleSearchInputChange,
117
- ]);
118
-
119
- function handleOnChange(inputValue: string) {
120
- setSearchValue(inputValue);
121
-
122
- if (fetchOptions === undefined) {
123
- if (options !== undefined) {
124
- const lowerSearchValue = inputValue.toLowerCase();
125
- setAllOptions(
126
- options.filter((o) => getValueString(o).toLowerCase().includes(lowerSearchValue)),
127
- );
128
- }
129
- } else {
130
- setIsLoading(true);
131
- changeSearchValue(inputValue);
132
- }
133
- }
134
-
135
- const handleSelectValue = (val: Option[]) => {
136
- if (val.length === 0) {
137
- onChange(undefined);
138
- return;
139
- }
140
-
141
- onChange({
142
- include: val,
143
- } as Value);
144
- };
145
-
146
- const handleClear = () => {
147
- onChange(undefined);
148
- };
149
-
150
- const onSelect: ICheckboxProps<Option>['onSelect'] = (val) => {
151
- handleSelectValue(
152
- val.isSelected
153
- ? [...(chosenValues ?? []), val.value]
154
- : chosenValues?.filter((v) => getValueId(v) !== getValueId(val.value)) ?? [],
155
- );
156
- };
157
-
158
- const handleKeyDown = (event: KeyboardEvent) => {
159
- // TODO: это все очень плохо работает
160
- if (event.code === 'Escape') {
161
- onChange(value);
162
- if (onClose) {
163
- onClose();
164
- }
165
- }
166
- if (event.code === 'ArrowDown') {
167
- if (keyCursorOn < unchosenOptions.length + (value?.include.length ?? 0) - 1) {
168
- setKeyCursorOn(keyCursorOn + 1);
169
- }
170
- }
171
- if (event.code === 'ArrowUp') {
172
- if (keyCursorOn >= 1) {
173
- setKeyCursorOn(keyCursorOn - 1);
174
- }
175
- }
176
- if (event.code === 'Enter') {
177
- if (
178
- unchosenOptions[keyCursorOn] === undefined &&
179
- (value === undefined || value.include.length === 0)
180
- ) {
181
- return;
182
- }
183
- if (value !== undefined) {
184
- if (keyCursorOn < value.include.length) {
185
- onChange({
186
- ...value,
187
- include:
188
- value.include.filter(
189
- (val) => getValueId(val) !== getValueId(value.include[keyCursorOn]),
190
- ) ?? [],
191
- });
192
- } else {
193
- onChange({
194
- ...value,
195
- include: [...value.include, unchosenOptions[keyCursorOn - value.include.length]],
196
- });
197
- }
198
- } else {
199
- onChange({
200
- include: [unchosenOptions[keyCursorOn]],
201
- } as Value);
202
- }
203
- }
204
- };
205
-
206
- const getIsValueChosen = (id: string) => chosenValues?.some((v) => getValueId(v) === id);
207
-
208
- const handleLoadItemsOnScroll = useCallback(
209
- async (inputValue?: string, page?: number) => {
210
- if (fetchOptions === undefined) {
211
- return;
212
- }
213
-
214
- setLoadingOptionsOnScroll(true);
215
-
216
- try {
217
- const response = await fetchOptions(inputValue ?? '', page ?? 0);
218
- if (isMounted()) {
219
- if (response.length > 0) {
220
- setAllOptions((prevState) => [...prevState, ...response]);
221
- }
222
- if (response.length === 0 || (pageSize !== undefined && response.length < pageSize)) {
223
- setIsMaxPage(true);
224
- }
225
- }
226
- } finally {
227
- if (isMounted()) {
228
- setLoadingOptionsOnScroll(false);
229
- }
230
- }
231
- },
232
- [fetchOptions, getValueId],
233
- );
234
-
235
- const initIntersectionObserver = useCallback(
236
- (node: HTMLDivElement) => {
237
- if (isLoadingOptionsOnScroll || isMaxPage) {
238
- return;
239
- }
240
-
241
- if (observer.current) {
242
- observer.current.disconnect();
243
- }
244
-
245
- observer.current = new IntersectionObserver(async (entries) => {
246
- if (entries[0].isIntersecting) {
247
- await handleLoadItemsOnScroll(searchValue, activePage + 1);
248
- if (isMounted()) {
249
- setActivePage((prevState) => prevState + 1);
250
- }
251
- }
252
- });
253
-
254
- if (node) {
255
- observer.current.observe(node);
256
- }
257
- },
258
- [activePage, handleLoadItemsOnScroll, isLoadingOptionsOnScroll, isMaxPage, searchValue],
259
- );
260
-
261
- useEffect(() => {
262
- if (fetchOptions === undefined) {
263
- setAllOptions(options ?? []);
264
- return;
265
- }
266
-
267
- const doFetchOptions = async () => {
268
- setIsLoading(true);
269
-
270
- try {
271
- const response = await fetchOptions('');
272
- if (response !== undefined && isMounted()) {
273
- if (response.length === 0 || (pageSize !== undefined && response.length < pageSize)) {
274
- setIsMaxPage(true);
275
- }
276
- setAllOptions(response);
277
- }
278
- } finally {
279
- if (isMounted()) {
280
- setIsLoading(false);
281
- }
282
- }
283
- };
284
- doFetchOptions();
285
- }, []);
286
-
287
- useEffect(() => {
288
- document.addEventListener('keydown', handleKeyDown, false);
289
-
290
- return () => {
291
- document.removeEventListener('keydown', handleKeyDown, false);
292
- };
293
- });
294
-
295
- const mainOptionsList = isGroupingEnabled ? unchosenOptions : allOptions;
296
-
297
- const hasSelectedOptionsGroup =
298
- isGroupingEnabled && chosenValues !== undefined && chosenValues.length > 0;
299
-
300
- const shouldShowNothingFoundMessage = !isLoading && allOptions.length === 0;
301
-
302
- const shouldShowAllOptionsLabel =
303
- isGroupingEnabled &&
304
- unchosenOptions.length > 0 &&
305
- chosenValues !== undefined &&
306
- chosenValues.length > 0;
307
-
308
- const shouldShowPreloader = isLoading || isLoadingOptionsOnScroll;
309
-
310
- const shouldShowOptionsList = !isLoading && allOptions.length !== 0;
311
-
312
- return (
313
- <div className={classes.root} {...addDataAttributes(data)}>
314
- {isSearchEnabled && (
315
- <div className={classes.dropdownInput}>
316
- <SearchInput
317
- value={searchValue}
318
- placeholder={translates.searchPlaceholder}
319
- onChange={handleOnChange}
320
- tweakStyles={tweakSearchInputStyles}
321
- maxLength={searchMaxLength}
322
- testId={testId !== undefined ? `${testId}-search` : undefined}
323
- shouldFocusOnMount
324
- />
325
- </div>
326
- )}
327
- {shouldShowOptionsList && (
328
- <div
329
- className={classes.list}
330
- data-testid={testId !== undefined ? `${testId}-list` : undefined}
331
- >
332
- {/* Выбранные */}
333
- {hasSelectedOptionsGroup && (
334
- <>
335
- <div
336
- className={clsx(
337
- classes.label,
338
- classes.labelChosen,
339
- !isSearchEnabled && classes.withoutTopGap,
340
- )}
341
- >
342
- {translates.chosen}
343
- </div>
344
- {chosenValues?.map((val) => {
345
- const id = getValueId(val);
346
- const view = getValueView(val);
347
-
348
- return (
349
- <div
350
- className={clsx(
351
- classes.item,
352
- // keyCursorOn === index && classes.selectedItem,
353
- )}
354
- key={id}
355
- >
356
- <Checkbox
357
- onSelect={onSelect}
358
- isChecked
359
- value={val}
360
- tweakStyles={tweakCheckboxStyles}
361
- labelPosition={checkboxPosition === 'left' ? 'right' : 'left'}
362
- >
363
- <div className={classes.option} data-option={id}>
364
- {view}
365
- </div>
366
- </Checkbox>
367
- </div>
368
- );
369
- })}
370
- </>
371
- )}
372
-
373
- {!isLoading && (
374
- <>
375
- {/* Не выбранные (или все если нет группировки) */}
376
- {shouldShowAllOptionsLabel && <div className={classes.label}>{translates.all}</div>}
377
-
378
- {mainOptionsList.map((val, index) => {
379
- const id = getValueId(val);
380
- const view = getValueView(val);
381
-
382
- return (
383
- <div
384
- className={clsx(
385
- classes.item,
386
- // keyCursorOn === index + (value?.include.length ?? 0) &&
387
- // classes.selectedItem,
388
- )}
389
- ref={
390
- mainOptionsList.length - 1 === index ? initIntersectionObserver : undefined
391
- }
392
- key={id}
393
- >
394
- <Checkbox
395
- onSelect={onSelect}
396
- isChecked={getIsValueChosen(id)}
397
- value={val}
398
- tweakStyles={tweakCheckboxStyles}
399
- labelPosition={checkboxPosition === 'left' ? 'right' : 'left'}
400
- >
401
- <div className={classes.option} data-option={id}>
402
- {view}
403
- </div>
404
- </Checkbox>
405
- </div>
406
- );
407
- })}
408
- </>
409
- )}
410
- </div>
411
- )}
412
-
413
- {/* Preloader */}
414
- {shouldShowPreloader && (
415
- <div className={classes.preloader}>
416
- <ThemedPreloader type="dots" />
417
- </div>
418
- )}
419
-
420
- {/* Nothing found */}
421
- {shouldShowNothingFoundMessage && (
422
- <div className={classes.nothingFound}>{translates.nothingFound}</div>
423
- )}
424
-
425
- {/* Controls */}
426
- <div className={classes.panel}>
427
- <div className={classes.clear}>
428
- <Button
429
- onClick={handleClear}
430
- size="s"
431
- view="text"
432
- testId={testId !== undefined ? `${testId}-clear-button` : undefined}
433
- tweakStyles={tweakClearButtonStyles}
434
- >
435
- {translates.clear}
436
- </Button>
437
- </div>
438
- </div>
439
- </div>
440
- );
441
- }
1
+ import { useEffect, useState, useMemo, useRef, useCallback, ReactNode } from 'react';
2
+ import clsx from 'clsx';
3
+ import { debounce } from 'ts-debounce';
4
+ import { addDataAttributes } from '../../helpers';
5
+ import { useIsMounted, useTheme, useTweakStyles } from '../../hooks';
6
+ import { ICommonProps } from '../../types';
7
+ import { Button } from '../Button';
8
+ import { Checkbox, ICheckboxProps } from '../Checkbox';
9
+ import { SearchInput } from '../SearchInput';
10
+ import { ThemedPreloader } from '../ThemedPreloader';
11
+ import { DEFAULT_LOCALE, MultiSelectLocales } from './constants';
12
+ import { defaultConvertFunction, getLocale } from './helpers';
13
+ import { IMultiSelectListValues, IMultiSelectLocale, IMultiSelectLocaleKey } from './types';
14
+ import { MultiSelectListStyles, styles } from './MultiSelectList.styles';
15
+
16
+ export interface IMultiSelectListProps<Value, Option> extends ICommonProps {
17
+ tweakStyles?: MultiSelectListStyles;
18
+ value?: Value;
19
+ isSearchEnabled?: boolean;
20
+ width?: number | string;
21
+ localeKey?: IMultiSelectLocaleKey;
22
+ locale?: Partial<IMultiSelectLocale>;
23
+ onClose?: () => void;
24
+ onChange: (val?: Value) => void;
25
+ options?: Option[];
26
+ fetchOptions?: (val?: string, page?: number) => Promise<Option[]>;
27
+ getValueView?: (val: Option) => ReactNode;
28
+ getValueId?: (val: Option) => string;
29
+ getValueString?: (val: Option) => string;
30
+ isGroupingEnabled?: boolean;
31
+ checkboxPosition?: 'left' | 'right';
32
+ pageSize?: number;
33
+ searchMaxLength?: number;
34
+ testId?: string;
35
+ }
36
+
37
+ export function MultiSelectList<Value extends IMultiSelectListValues<Option>, Option = string>({
38
+ value,
39
+ tweakStyles,
40
+ data,
41
+ isSearchEnabled = false,
42
+ width,
43
+ localeKey = DEFAULT_LOCALE,
44
+ locale = MultiSelectLocales[localeKey],
45
+ onChange,
46
+ onClose,
47
+ options,
48
+ fetchOptions,
49
+ isGroupingEnabled = isSearchEnabled,
50
+ getValueView = defaultConvertFunction,
51
+ getValueId = defaultConvertFunction,
52
+ getValueString = defaultConvertFunction,
53
+ checkboxPosition = 'left',
54
+ pageSize,
55
+ searchMaxLength,
56
+ testId,
57
+ }: IMultiSelectListProps<Value, Option>): JSX.Element {
58
+ const isMounted = useIsMounted();
59
+ const { classes, componentStyles } = useTheme('MultiSelectList', styles, tweakStyles, { width });
60
+
61
+ const tweakCheckboxStyles = useTweakStyles(componentStyles, tweakStyles, 'tweakCheckbox');
62
+
63
+ const tweakSearchInputStyles = useTweakStyles(componentStyles, tweakStyles, 'tweakSearchInput');
64
+
65
+ const tweakClearButtonStyles = useTweakStyles(componentStyles, tweakStyles, 'tweakClearButton');
66
+
67
+ const translates = useMemo(() => getLocale(localeKey, locale), [localeKey, locale]);
68
+
69
+ const chosenValues = value?.include;
70
+ const chosenValuesIds = useMemo(() => chosenValues?.map(getValueId), [getValueId, chosenValues]);
71
+
72
+ const [keyCursorOn, setKeyCursorOn] = useState(0);
73
+ const [searchValue, setSearchValue] = useState('');
74
+ // Опции, которые получаем с бека при изменении строки поиска
75
+ const [allOptions, setAllOptions] = useState<Option[]>([]);
76
+
77
+ const [isLoading, setIsLoading] = useState(false);
78
+ const [isLoadingOptionsOnScroll, setLoadingOptionsOnScroll] = useState(false);
79
+
80
+ const [activePage, setActivePage] = useState(0);
81
+ const [isMaxPage, setIsMaxPage] = useState(false);
82
+
83
+ const observer = useRef<IntersectionObserver>();
84
+
85
+ const unchosenOptions = useMemo(
86
+ () => allOptions.filter((v) => !chosenValuesIds?.includes(getValueId(v))),
87
+ [getValueId, allOptions, chosenValuesIds],
88
+ );
89
+
90
+ const handleSearchInputChange = useCallback(
91
+ async (inputValue?: string) => {
92
+ if (fetchOptions === undefined) {
93
+ return;
94
+ }
95
+ setIsLoading(true);
96
+
97
+ try {
98
+ const response = await fetchOptions(inputValue?.trim() ?? '');
99
+ if (isMounted() && response !== undefined) {
100
+ setAllOptions(response);
101
+ setActivePage(0);
102
+ setIsMaxPage(
103
+ response.length === 0 || (pageSize !== undefined && response.length < pageSize),
104
+ );
105
+ }
106
+ } finally {
107
+ if (isMounted()) {
108
+ setIsLoading(false);
109
+ }
110
+ }
111
+ },
112
+ [fetchOptions, pageSize],
113
+ );
114
+
115
+ const changeSearchValue = useCallback(debounce(handleSearchInputChange), [
116
+ handleSearchInputChange,
117
+ ]);
118
+
119
+ function handleOnChange(inputValue: string) {
120
+ setSearchValue(inputValue);
121
+
122
+ if (fetchOptions === undefined) {
123
+ if (options !== undefined) {
124
+ const lowerSearchValue = inputValue.toLowerCase();
125
+ setAllOptions(
126
+ options.filter((o) => getValueString(o).toLowerCase().includes(lowerSearchValue)),
127
+ );
128
+ }
129
+ } else {
130
+ setIsLoading(true);
131
+ changeSearchValue(inputValue);
132
+ }
133
+ }
134
+
135
+ const handleSelectValue = (val: Option[]) => {
136
+ if (val.length === 0) {
137
+ onChange(undefined);
138
+ return;
139
+ }
140
+
141
+ onChange({
142
+ include: val,
143
+ } as Value);
144
+ };
145
+
146
+ const handleClear = () => {
147
+ onChange(undefined);
148
+ };
149
+
150
+ const onSelect: ICheckboxProps<Option>['onSelect'] = (val) => {
151
+ handleSelectValue(
152
+ val.isSelected
153
+ ? [...(chosenValues ?? []), val.value]
154
+ : chosenValues?.filter((v) => getValueId(v) !== getValueId(val.value)) ?? [],
155
+ );
156
+ };
157
+
158
+ const handleKeyDown = (event: KeyboardEvent) => {
159
+ // TODO: это все очень плохо работает
160
+ if (event.code === 'Escape') {
161
+ onChange(value);
162
+ if (onClose) {
163
+ onClose();
164
+ }
165
+ }
166
+ if (event.code === 'ArrowDown') {
167
+ if (keyCursorOn < unchosenOptions.length + (value?.include.length ?? 0) - 1) {
168
+ setKeyCursorOn(keyCursorOn + 1);
169
+ }
170
+ }
171
+ if (event.code === 'ArrowUp') {
172
+ if (keyCursorOn >= 1) {
173
+ setKeyCursorOn(keyCursorOn - 1);
174
+ }
175
+ }
176
+ if (event.code === 'Enter') {
177
+ if (
178
+ unchosenOptions[keyCursorOn] === undefined &&
179
+ (value === undefined || value.include.length === 0)
180
+ ) {
181
+ return;
182
+ }
183
+ if (value !== undefined) {
184
+ if (keyCursorOn < value.include.length) {
185
+ onChange({
186
+ ...value,
187
+ include:
188
+ value.include.filter(
189
+ (val) => getValueId(val) !== getValueId(value.include[keyCursorOn]),
190
+ ) ?? [],
191
+ });
192
+ } else {
193
+ onChange({
194
+ ...value,
195
+ include: [...value.include, unchosenOptions[keyCursorOn - value.include.length]],
196
+ });
197
+ }
198
+ } else {
199
+ onChange({
200
+ include: [unchosenOptions[keyCursorOn]],
201
+ } as Value);
202
+ }
203
+ }
204
+ };
205
+
206
+ const getIsValueChosen = (id: string) => chosenValues?.some((v) => getValueId(v) === id);
207
+
208
+ const handleLoadItemsOnScroll = useCallback(
209
+ async (inputValue?: string, page?: number) => {
210
+ if (fetchOptions === undefined) {
211
+ return;
212
+ }
213
+
214
+ setLoadingOptionsOnScroll(true);
215
+
216
+ try {
217
+ const response = await fetchOptions(inputValue ?? '', page ?? 0);
218
+ if (isMounted()) {
219
+ if (response.length > 0) {
220
+ setAllOptions((prevState) => [...prevState, ...response]);
221
+ }
222
+ if (response.length === 0 || (pageSize !== undefined && response.length < pageSize)) {
223
+ setIsMaxPage(true);
224
+ }
225
+ }
226
+ } finally {
227
+ if (isMounted()) {
228
+ setLoadingOptionsOnScroll(false);
229
+ }
230
+ }
231
+ },
232
+ [fetchOptions, getValueId],
233
+ );
234
+
235
+ const initIntersectionObserver = useCallback(
236
+ (node: HTMLDivElement) => {
237
+ if (isLoadingOptionsOnScroll || isMaxPage) {
238
+ return;
239
+ }
240
+
241
+ if (observer.current) {
242
+ observer.current.disconnect();
243
+ }
244
+
245
+ observer.current = new IntersectionObserver(async (entries) => {
246
+ if (entries[0].isIntersecting) {
247
+ await handleLoadItemsOnScroll(searchValue, activePage + 1);
248
+ if (isMounted()) {
249
+ setActivePage((prevState) => prevState + 1);
250
+ }
251
+ }
252
+ });
253
+
254
+ if (node) {
255
+ observer.current.observe(node);
256
+ }
257
+ },
258
+ [activePage, handleLoadItemsOnScroll, isLoadingOptionsOnScroll, isMaxPage, searchValue],
259
+ );
260
+
261
+ useEffect(() => {
262
+ if (fetchOptions === undefined) {
263
+ setAllOptions(options ?? []);
264
+ return;
265
+ }
266
+
267
+ const doFetchOptions = async () => {
268
+ setIsLoading(true);
269
+
270
+ try {
271
+ const response = await fetchOptions('');
272
+ if (response !== undefined && isMounted()) {
273
+ if (response.length === 0 || (pageSize !== undefined && response.length < pageSize)) {
274
+ setIsMaxPage(true);
275
+ }
276
+ setAllOptions(response);
277
+ }
278
+ } finally {
279
+ if (isMounted()) {
280
+ setIsLoading(false);
281
+ }
282
+ }
283
+ };
284
+ doFetchOptions();
285
+ }, []);
286
+
287
+ useEffect(() => {
288
+ document.addEventListener('keydown', handleKeyDown, false);
289
+
290
+ return () => {
291
+ document.removeEventListener('keydown', handleKeyDown, false);
292
+ };
293
+ });
294
+
295
+ const mainOptionsList = isGroupingEnabled ? unchosenOptions : allOptions;
296
+
297
+ const hasSelectedOptionsGroup =
298
+ isGroupingEnabled && chosenValues !== undefined && chosenValues.length > 0;
299
+
300
+ const shouldShowNothingFoundMessage = !isLoading && allOptions.length === 0;
301
+
302
+ const shouldShowAllOptionsLabel =
303
+ isGroupingEnabled &&
304
+ unchosenOptions.length > 0 &&
305
+ chosenValues !== undefined &&
306
+ chosenValues.length > 0;
307
+
308
+ const shouldShowPreloader = isLoading || isLoadingOptionsOnScroll;
309
+
310
+ const shouldShowOptionsList = !isLoading && allOptions.length !== 0;
311
+
312
+ return (
313
+ <div className={classes.root} {...addDataAttributes(data)}>
314
+ {isSearchEnabled && (
315
+ <div className={classes.dropdownInput}>
316
+ <SearchInput
317
+ value={searchValue}
318
+ placeholder={translates.searchPlaceholder}
319
+ onChange={handleOnChange}
320
+ tweakStyles={tweakSearchInputStyles}
321
+ maxLength={searchMaxLength}
322
+ testId={testId !== undefined ? `${testId}-search` : undefined}
323
+ shouldFocusOnMount
324
+ />
325
+ </div>
326
+ )}
327
+ {shouldShowOptionsList && (
328
+ <div
329
+ className={classes.list}
330
+ data-testid={testId !== undefined ? `${testId}-list` : undefined}
331
+ >
332
+ {/* Выбранные */}
333
+ {hasSelectedOptionsGroup && (
334
+ <>
335
+ <div
336
+ className={clsx(
337
+ classes.label,
338
+ classes.labelChosen,
339
+ !isSearchEnabled && classes.withoutTopGap,
340
+ )}
341
+ >
342
+ {translates.chosen}
343
+ </div>
344
+ {chosenValues?.map((val) => {
345
+ const id = getValueId(val);
346
+ const view = getValueView(val);
347
+
348
+ return (
349
+ <div
350
+ className={clsx(
351
+ classes.item,
352
+ // keyCursorOn === index && classes.selectedItem,
353
+ )}
354
+ key={id}
355
+ >
356
+ <Checkbox
357
+ onSelect={onSelect}
358
+ isChecked
359
+ value={val}
360
+ tweakStyles={tweakCheckboxStyles}
361
+ labelPosition={checkboxPosition === 'left' ? 'right' : 'left'}
362
+ >
363
+ <div className={classes.option} data-option={id}>
364
+ {view}
365
+ </div>
366
+ </Checkbox>
367
+ </div>
368
+ );
369
+ })}
370
+ </>
371
+ )}
372
+
373
+ {!isLoading && (
374
+ <>
375
+ {/* Не выбранные (или все если нет группировки) */}
376
+ {shouldShowAllOptionsLabel && <div className={classes.label}>{translates.all}</div>}
377
+
378
+ {mainOptionsList.map((val, index) => {
379
+ const id = getValueId(val);
380
+ const view = getValueView(val);
381
+
382
+ return (
383
+ <div
384
+ className={clsx(
385
+ classes.item,
386
+ // keyCursorOn === index + (value?.include.length ?? 0) &&
387
+ // classes.selectedItem,
388
+ )}
389
+ ref={
390
+ mainOptionsList.length - 1 === index ? initIntersectionObserver : undefined
391
+ }
392
+ key={id}
393
+ >
394
+ <Checkbox
395
+ onSelect={onSelect}
396
+ isChecked={getIsValueChosen(id)}
397
+ value={val}
398
+ tweakStyles={tweakCheckboxStyles}
399
+ labelPosition={checkboxPosition === 'left' ? 'right' : 'left'}
400
+ >
401
+ <div className={classes.option} data-option={id}>
402
+ {view}
403
+ </div>
404
+ </Checkbox>
405
+ </div>
406
+ );
407
+ })}
408
+ </>
409
+ )}
410
+ </div>
411
+ )}
412
+
413
+ {/* Preloader */}
414
+ {shouldShowPreloader && (
415
+ <div className={classes.preloader}>
416
+ <ThemedPreloader type="dots" />
417
+ </div>
418
+ )}
419
+
420
+ {/* Nothing found */}
421
+ {shouldShowNothingFoundMessage && (
422
+ <div className={classes.nothingFound}>{translates.nothingFound}</div>
423
+ )}
424
+
425
+ {/* Controls */}
426
+ <div className={classes.panel}>
427
+ <div className={classes.clear}>
428
+ <Button
429
+ onClick={handleClear}
430
+ size="s"
431
+ view="text"
432
+ testId={testId !== undefined ? `${testId}-clear-button` : undefined}
433
+ tweakStyles={tweakClearButtonStyles}
434
+ >
435
+ {translates.clear}
436
+ </Button>
437
+ </div>
438
+ </div>
439
+ </div>
440
+ );
441
+ }