@true-engineering/true-react-common-ui-kit 1.5.3 → 1.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 (244) hide show
  1. package/LICENSE +201 -201
  2. package/dist/components/Checkbox/Checkbox.d.ts +2 -1
  3. package/dist/components/Checkbox/Checkbox.styles.d.ts +4 -1
  4. package/dist/components/Flag/augment.d.ts +1 -1
  5. package/dist/components/FlexibleTable/types.d.ts +2 -2
  6. package/dist/components/Icon/complexIcons/augment.d.ts +1 -1
  7. package/dist/helpers/index.d.ts +0 -1
  8. package/dist/helpers/utils.d.ts +8 -0
  9. package/dist/true-react-common-ui-kit.js +183 -154
  10. package/dist/true-react-common-ui-kit.js.map +1 -1
  11. package/dist/true-react-common-ui-kit.umd.cjs +183 -154
  12. package/dist/true-react-common-ui-kit.umd.cjs.map +1 -1
  13. package/dist/vite-env.d.ts +1 -1
  14. package/package.json +91 -91
  15. package/src/components/AccountInfo/AccountInfo.stories.tsx +35 -35
  16. package/src/components/AccountInfo/AccountInfo.styles.ts +55 -55
  17. package/src/components/AccountInfo/AccountInfo.tsx +106 -106
  18. package/src/components/AccountInfo/index.ts +2 -2
  19. package/src/components/AddButton/AddButton.stories.tsx +21 -21
  20. package/src/components/AddButton/AddButton.styles.ts +34 -34
  21. package/src/components/AddButton/AddButton.tsx +49 -49
  22. package/src/components/AddButton/index.ts +2 -2
  23. package/src/components/Button/Button.stories.tsx +61 -61
  24. package/src/components/Button/Button.styles.ts +196 -196
  25. package/src/components/Button/Button.tsx +195 -195
  26. package/src/components/Button/index.ts +2 -2
  27. package/src/components/Checkbox/Checkbox.stories.tsx +35 -35
  28. package/src/components/Checkbox/Checkbox.styles.ts +62 -59
  29. package/src/components/Checkbox/Checkbox.tsx +106 -93
  30. package/src/components/Checkbox/index.ts +2 -2
  31. package/src/components/CloseButton/CloseButton.styles.ts +34 -34
  32. package/src/components/CloseButton/CloseButton.tsx +37 -37
  33. package/src/components/CloseButton/index.ts +2 -2
  34. package/src/components/Colors/Colors.stories.tsx +7 -7
  35. package/src/components/Colors/Colors.styles.ts +38 -38
  36. package/src/components/Colors/Colors.tsx +34 -34
  37. package/src/components/Colors/index.ts +2 -2
  38. package/src/components/CssBaseline/CssBaseline.styles.ts +15 -15
  39. package/src/components/CssBaseline/CssBaseline.tsx +17 -17
  40. package/src/components/CssBaseline/index.ts +2 -2
  41. package/src/components/DateInput/DateInput.stories.tsx +63 -63
  42. package/src/components/DateInput/DateInput.styles.ts +14 -14
  43. package/src/components/DateInput/DateInput.tsx +60 -60
  44. package/src/components/DateInput/index.ts +2 -2
  45. package/src/components/DatePicker/DatePicker.stories.tsx +96 -96
  46. package/src/components/DatePicker/DatePicker.styles.ts +54 -54
  47. package/src/components/DatePicker/DatePicker.tsx +358 -358
  48. package/src/components/DatePicker/DatePickerHeader/DatePickerHeader.styles.ts +84 -84
  49. package/src/components/DatePicker/DatePickerHeader/DatePickerHeader.tsx +94 -94
  50. package/src/components/DatePicker/DatePickerHeader/index.ts +1 -1
  51. package/src/components/DatePicker/DatePickerInput/DatePickerInput.styles.ts +25 -25
  52. package/src/components/DatePicker/DatePickerInput/DatePickerInput.tsx +31 -31
  53. package/src/components/DatePicker/DatePickerInput/index.ts +1 -1
  54. package/src/components/DatePicker/index.ts +4 -4
  55. package/src/components/Description/Description.stories.tsx +29 -29
  56. package/src/components/Description/Description.styles.ts +31 -31
  57. package/src/components/Description/Description.tsx +69 -69
  58. package/src/components/Description/index.ts +2 -2
  59. package/src/components/FiltersPane/FilterInterval/FilterInterval.styles.ts +64 -64
  60. package/src/components/FiltersPane/FilterInterval/FilterInterval.tsx +162 -162
  61. package/src/components/FiltersPane/FilterInterval/index.ts +1 -1
  62. package/src/components/FiltersPane/FilterMultiSelect/FilterMultiSelect.tsx +14 -14
  63. package/src/components/FiltersPane/FilterMultiSelect/index.ts +1 -1
  64. package/src/components/FiltersPane/FilterSelect/FilterSelect.styles.ts +144 -144
  65. package/src/components/FiltersPane/FilterSelect/FilterSelect.tsx +397 -397
  66. package/src/components/FiltersPane/FilterSelect/index.ts +1 -1
  67. package/src/components/FiltersPane/FilterSelect/locales.ts +37 -37
  68. package/src/components/FiltersPane/FilterValueView/FilterValueView.styles.tsx +15 -15
  69. package/src/components/FiltersPane/FilterValueView/FilterValueView.tsx +186 -186
  70. package/src/components/FiltersPane/FilterValueView/index.tsx +1 -1
  71. package/src/components/FiltersPane/FilterWithDates/FilterWithDates.styles.ts +60 -60
  72. package/src/components/FiltersPane/FilterWithDates/FilterWithDates.tsx +222 -222
  73. package/src/components/FiltersPane/FilterWithDates/index.ts +1 -1
  74. package/src/components/FiltersPane/FilterWithPeriod/FilterWithPeriod.styles.ts +17 -17
  75. package/src/components/FiltersPane/FilterWithPeriod/FilterWithPeriod.tsx +231 -231
  76. package/src/components/FiltersPane/FilterWithPeriod/index.ts +1 -1
  77. package/src/components/FiltersPane/FilterWrapper/FilterWrapper.styles.ts +110 -110
  78. package/src/components/FiltersPane/FilterWrapper/FilterWrapper.tsx +360 -360
  79. package/src/components/FiltersPane/FilterWrapper/index.ts +1 -1
  80. package/src/components/FiltersPane/FiltersPane.stories.tsx +308 -308
  81. package/src/components/FiltersPane/FiltersPane.styles.ts +71 -71
  82. package/src/components/FiltersPane/FiltersPane.tsx +193 -193
  83. package/src/components/FiltersPane/FiltersPaneSearch/FiltersPaneSearch.styles.ts +109 -109
  84. package/src/components/FiltersPane/FiltersPaneSearch/FiltersPaneSearch.tsx +175 -175
  85. package/src/components/FiltersPane/FiltersPaneSearch/index.ts +1 -1
  86. package/src/components/FiltersPane/index.ts +20 -20
  87. package/src/components/FiltersPane/locales.ts +107 -107
  88. package/src/components/FiltersPane/types.ts +126 -126
  89. package/src/components/Flag/Flag.stories.tsx +29 -29
  90. package/src/components/Flag/Flag.styles.ts +18 -18
  91. package/src/components/Flag/Flag.tsx +28 -28
  92. package/src/components/Flag/augment.d.ts +1 -1
  93. package/src/components/Flag/index.ts +2 -2
  94. package/src/components/FlexibleTable/FlexibleTable.stories.tsx +86 -80
  95. package/src/components/FlexibleTable/FlexibleTable.styles.ts +131 -131
  96. package/src/components/FlexibleTable/FlexibleTable.tsx +243 -243
  97. package/src/components/FlexibleTable/TableRow.tsx +171 -171
  98. package/src/components/FlexibleTable/TableValue.tsx +83 -83
  99. package/src/components/FlexibleTable/fixture-test.ts +254 -254
  100. package/src/components/FlexibleTable/index.ts +3 -3
  101. package/src/components/FlexibleTable/types.ts +58 -58
  102. package/src/components/Icon/ComplexIconBoilerplate.tsx +17 -17
  103. package/src/components/Icon/Icon.stories.tsx +88 -88
  104. package/src/components/Icon/Icon.styles.ts +10 -10
  105. package/src/components/Icon/Icon.tsx +34 -34
  106. package/src/components/Icon/IconBoilerplate.tsx +42 -42
  107. package/src/components/Icon/complexIcons/augment.d.ts +1 -1
  108. package/src/components/Icon/complexIcons/avatarGreen.svg +57 -57
  109. package/src/components/Icon/complexIcons/icons.ts +7 -7
  110. package/src/components/Icon/complexIcons/index.ts +1 -1
  111. package/src/components/Icon/icons/icons.ts +838 -838
  112. package/src/components/Icon/icons/index.ts +1 -1
  113. package/src/components/Icon/index.ts +4 -4
  114. package/src/components/IncrementInput/ChangeButton.tsx +34 -34
  115. package/src/components/IncrementInput/IncrementInput.stories.tsx +34 -34
  116. package/src/components/IncrementInput/IncrementInput.styles.ts +77 -77
  117. package/src/components/IncrementInput/IncrementInput.tsx +95 -95
  118. package/src/components/IncrementInput/index.ts +2 -2
  119. package/src/components/Input/Input.stories.tsx +92 -92
  120. package/src/components/Input/Input.styles.ts +305 -305
  121. package/src/components/Input/Input.tsx +318 -318
  122. package/src/components/Input/index.ts +2 -2
  123. package/src/components/List/List.stories.tsx +62 -62
  124. package/src/components/List/List.styles.ts +52 -52
  125. package/src/components/List/List.tsx +82 -82
  126. package/src/components/List/index.ts +2 -2
  127. package/src/components/Modal/Modal.stories.tsx +113 -113
  128. package/src/components/Modal/Modal.styles.ts +308 -308
  129. package/src/components/Modal/Modal.tsx +210 -210
  130. package/src/components/Modal/index.ts +2 -2
  131. package/src/components/MoreMenu/MoreMenu.stories.tsx +46 -46
  132. package/src/components/MoreMenu/MoreMenu.styles.ts +70 -70
  133. package/src/components/MoreMenu/MoreMenu.tsx +102 -102
  134. package/src/components/MoreMenu/index.ts +2 -2
  135. package/src/components/MultiSelect/MultiSelect.stories.tsx +46 -46
  136. package/src/components/MultiSelect/MultiSelect.styles.ts +55 -55
  137. package/src/components/MultiSelect/MultiSelect.tsx +98 -98
  138. package/src/components/MultiSelect/MultiSelectInput/MultiSelectInput.styles.ts +73 -73
  139. package/src/components/MultiSelect/MultiSelectInput/MultiSelectInput.tsx +62 -62
  140. package/src/components/MultiSelect/MultiSelectInput/index.ts +1 -1
  141. package/src/components/MultiSelect/index.ts +3 -3
  142. package/src/components/MultiSelectList/MultiSelectList.styles.ts +125 -125
  143. package/src/components/MultiSelectList/MultiSelectList.tsx +519 -519
  144. package/src/components/MultiSelectList/index.ts +2 -2
  145. package/src/components/MultiSelectList/locales.ts +37 -37
  146. package/src/components/Notification/Notification.stories.tsx +51 -51
  147. package/src/components/Notification/Notification.styles.ts +50 -50
  148. package/src/components/Notification/Notification.tsx +84 -84
  149. package/src/components/Notification/index.ts +2 -2
  150. package/src/components/NumberInput/NumberInput.stories.tsx +36 -36
  151. package/src/components/NumberInput/NumberInput.tsx +154 -154
  152. package/src/components/NumberInput/helpers.ts +87 -87
  153. package/src/components/NumberInput/index.ts +1 -1
  154. package/src/components/PhoneInput/PhoneInput.stories.tsx +71 -71
  155. package/src/components/PhoneInput/PhoneInput.styles.ts +84 -84
  156. package/src/components/PhoneInput/PhoneInput.tsx +223 -223
  157. package/src/components/PhoneInput/PhoneInputCountryList/PhoneInputCountryList.stories.tsx +21 -21
  158. package/src/components/PhoneInput/PhoneInputCountryList/PhoneInputCountryList.styles.ts +100 -100
  159. package/src/components/PhoneInput/PhoneInputCountryList/PhoneInputCountryList.tsx +171 -171
  160. package/src/components/PhoneInput/PhoneInputCountryList/index.ts +2 -2
  161. package/src/components/PhoneInput/index.ts +6 -6
  162. package/src/components/PhoneInput/phone-info.ts +2167 -2167
  163. package/src/components/PhoneInput/types.ts +16 -16
  164. package/src/components/RadioButton/RadioButton.stories.tsx +46 -46
  165. package/src/components/RadioButton/RadioButton.styles.ts +37 -37
  166. package/src/components/RadioButton/RadioButton.tsx +56 -56
  167. package/src/components/RadioButton/index.ts +2 -2
  168. package/src/components/ScrollIntoViewIfNeeded/ScrollIntoViewIfNeeded.ts +66 -66
  169. package/src/components/ScrollIntoViewIfNeeded/index.ts +1 -1
  170. package/src/components/SearchInput/SearchInput.stories.tsx +24 -24
  171. package/src/components/SearchInput/SearchInput.styles.ts +50 -50
  172. package/src/components/SearchInput/SearchInput.tsx +63 -63
  173. package/src/components/SearchInput/index.ts +2 -2
  174. package/src/components/Select/Select.stories.tsx +258 -258
  175. package/src/components/Select/Select.styles.ts +85 -85
  176. package/src/components/Select/Select.tsx +517 -508
  177. package/src/components/Select/SelectList/SelectList.styles.ts +68 -68
  178. package/src/components/Select/SelectList/SelectList.tsx +139 -139
  179. package/src/components/Select/SelectList/index.ts +1 -1
  180. package/src/components/Select/helpers.ts +21 -21
  181. package/src/components/Select/index.ts +3 -3
  182. package/src/components/SmartInput/SmartInput.stories.tsx +63 -63
  183. package/src/components/SmartInput/SmartInput.tsx +180 -180
  184. package/src/components/SmartInput/helpers.ts +85 -85
  185. package/src/components/SmartInput/index.ts +1 -1
  186. package/src/components/Switch/Switch.stories.tsx +40 -40
  187. package/src/components/Switch/Switch.styles.ts +75 -75
  188. package/src/components/Switch/Switch.tsx +89 -89
  189. package/src/components/Switch/index.ts +2 -2
  190. package/src/components/TextArea/TextArea.stories.tsx +35 -35
  191. package/src/components/TextArea/TextArea.styles.ts +153 -153
  192. package/src/components/TextArea/TextArea.tsx +178 -178
  193. package/src/components/TextArea/index.ts +2 -2
  194. package/src/components/TextWithInfo/TextWithInfo.stories.tsx +53 -53
  195. package/src/components/TextWithInfo/TextWithInfo.styles.ts +60 -60
  196. package/src/components/TextWithInfo/TextWithInfo.tsx +67 -67
  197. package/src/components/TextWithInfo/index.ts +2 -2
  198. package/src/components/TextWithTooltip/TextWithTooltip.stories.tsx +58 -58
  199. package/src/components/TextWithTooltip/TextWithTooltip.styles.ts +19 -19
  200. package/src/components/TextWithTooltip/TextWithTooltip.tsx +163 -163
  201. package/src/components/TextWithTooltip/index.ts +2 -2
  202. package/src/components/ThemedPreloader/ThemedPreloader.stories.tsx +41 -41
  203. package/src/components/ThemedPreloader/ThemedPreloader.styles.ts +21 -21
  204. package/src/components/ThemedPreloader/ThemedPreloader.tsx +56 -56
  205. package/src/components/ThemedPreloader/components/DefaultPreloader/DefaultPreloader.tsx +34 -34
  206. package/src/components/ThemedPreloader/components/DefaultPreloader/index.ts +1 -1
  207. package/src/components/ThemedPreloader/components/DotsPreloader/DotsPreloader.styles.ts +54 -54
  208. package/src/components/ThemedPreloader/components/DotsPreloader/DotsPreloader.tsx +18 -18
  209. package/src/components/ThemedPreloader/components/DotsPreloader/index.ts +2 -2
  210. package/src/components/ThemedPreloader/components/SvgPreloader/SvgPreloader.styles.ts +11 -11
  211. package/src/components/ThemedPreloader/components/SvgPreloader/SvgPreloader.tsx +32 -32
  212. package/src/components/ThemedPreloader/components/SvgPreloader/index.ts +2 -2
  213. package/src/components/ThemedPreloader/components/index.ts +2 -2
  214. package/src/components/ThemedPreloader/index.ts +2 -2
  215. package/src/components/Toaster/Toaster.stories.tsx +34 -34
  216. package/src/components/Toaster/Toaster.styles.ts +59 -59
  217. package/src/components/Toaster/Toaster.tsx +113 -113
  218. package/src/components/Toaster/index.ts +2 -2
  219. package/src/components/Tooltip/Tooltip.stories.tsx +21 -21
  220. package/src/components/Tooltip/Tooltip.styles.ts +45 -45
  221. package/src/components/Tooltip/Tooltip.tsx +40 -40
  222. package/src/components/Tooltip/index.ts +3 -3
  223. package/src/components/Tooltip/types.ts +1 -1
  224. package/src/components/index.ts +36 -36
  225. package/src/helpers/colors.ts +2 -2
  226. package/src/helpers/dateHelpers/date-helpers.ts +9 -9
  227. package/src/helpers/index.ts +4 -5
  228. package/src/helpers/phone.ts +106 -106
  229. package/src/helpers/popper-helpers.ts +17 -17
  230. package/src/helpers/snippets.tsx +5 -5
  231. package/src/helpers/utils.ts +219 -178
  232. package/src/hooks/index.ts +6 -6
  233. package/src/hooks/use-did-mount-effect.ts +21 -21
  234. package/src/hooks/use-dropdown.ts +85 -85
  235. package/src/hooks/use-is-mounted.ts +15 -15
  236. package/src/hooks/use-on-click-outside.ts +92 -92
  237. package/src/hooks/use-theme.ts +36 -36
  238. package/src/hooks/use-tweak-styles.ts +14 -14
  239. package/src/index.ts +6 -6
  240. package/src/theme.ts +155 -155
  241. package/src/types.ts +106 -106
  242. package/src/vite-env.d.ts +1 -1
  243. package/dist/helpers/data-attributes.d.ts +0 -5
  244. package/src/helpers/data-attributes.ts +0 -18
@@ -1,508 +1,517 @@
1
- import {
2
- ReactNode,
3
- FocusEvent,
4
- KeyboardEvent,
5
- MouseEvent,
6
- useCallback,
7
- useEffect,
8
- useMemo,
9
- useRef,
10
- useState,
11
- SyntheticEvent,
12
- } from 'react';
13
- import { Styles } from 'jss';
14
- import clsx from 'clsx';
15
- import { merge } from 'lodash';
16
- import { debounce } from 'ts-debounce';
17
- import { Portal } from 'react-overlays';
18
- import { SelectList } from './SelectList';
19
- import { IInputProps, Input } from '../Input';
20
- import { IIconType, Icon } from '../Icon';
21
- import {
22
- useIsMounted,
23
- useTheme,
24
- useOnClickOutsideWithRef,
25
- useDropdown,
26
- useTweakStyles,
27
- } from '../../hooks';
28
- import { IDropdownWithPopperOptions } from '../../types';
29
- import { getTestId, hasExactParent, isNotEmpty } from '../../helpers';
30
- import {
31
- defaultConvertFunction,
32
- defaultCompareFunction,
33
- getActiveValueIndex,
34
- defaultIsOptionDisabled,
35
- } from './helpers';
36
- import { SelectStyles, styles } from './Select.styles';
37
- import { ISearchInputProps, SearchInput } from '../SearchInput';
38
-
39
- export interface ISelectProps<Value>
40
- extends Omit<IInputProps, 'value' | 'onChange' | 'onBlur' | 'type'> {
41
- tweakStyles?: SelectStyles;
42
- defaultOptionLabel?: string;
43
- noMatchesLabel?: string;
44
- loadingLabel?: ReactNode;
45
- optionsMode?: 'search' | 'dynamic' | 'normal';
46
- debounceTime?: number;
47
- minSymbolsCountToOpenList?: number;
48
- dropdownOptions?: IDropdownWithPopperOptions;
49
- dropdownIcon?: IIconType;
50
- options: Value[];
51
- value: Value | undefined;
52
- shouldScrollToList?: boolean;
53
- searchInput?: { shouldRenderInList: true } & Pick<
54
- ISearchInputProps,
55
- 'placeholder'
56
- >;
57
- isOptionDisabled?(option: Value): boolean;
58
- onChange(value: Value | undefined): void; // подумать как возвращать индекс
59
- onBlur?(event: Event | SyntheticEvent): void;
60
- onType?(value: string): Promise<void>;
61
- optionsFilter?(options: Value[], query: string): Value[];
62
- onOpen?(): void;
63
- compareValuesOnChange?(v1: Value | undefined, v2: Value | undefined): boolean;
64
- // Для избежания проблем юзайте useCallback на эти функции
65
- // или выносите их из компонента (чтобы не было сайдэфектов от перерендеринга их)
66
- convertValueToString?(value: Value): string | undefined;
67
- convertValueToReactNode?(value: Value, isDisabled: boolean): ReactNode;
68
- convertValueToId?(value: Value): string | undefined;
69
- }
70
-
71
- export function Select<Value>({
72
- options,
73
- value,
74
- defaultOptionLabel,
75
- debounceTime = 400,
76
- optionsMode = 'normal',
77
- noMatchesLabel,
78
- loadingLabel,
79
- tweakStyles,
80
- testId,
81
- isDisabled,
82
- dropdownOptions,
83
- minSymbolsCountToOpenList = 0,
84
- dropdownIcon = 'chevron-down',
85
- shouldScrollToList = true,
86
- searchInput,
87
- onChange,
88
- onFocus,
89
- onBlur,
90
- onType,
91
- onOpen,
92
- isOptionDisabled = defaultIsOptionDisabled,
93
- compareValuesOnChange = defaultCompareFunction,
94
- convertValueToString = defaultConvertFunction,
95
- convertValueToId,
96
- convertValueToReactNode,
97
- optionsFilter,
98
- ...inputProps
99
- }: ISelectProps<Value>): JSX.Element {
100
- const { classes, componentStyles } = useTheme('Select', styles, tweakStyles);
101
- const isMounted = useIsMounted();
102
- const [isListOpen, setIsListOpen] = useState(false);
103
- const [areOptionsLoading, setAreOptionsLoading] = useState(false);
104
- const hasDefaultOption = defaultOptionLabel !== undefined;
105
-
106
- const [focusedListCellIndex, setFocusedListCellIndex] = useState(-1);
107
- const [searchValue, setSearchValue] = useState('');
108
-
109
- // если мы ввели что то в строку поиска - то этот булеан будет отключаться
110
- // вынесен отдельно, из-за проблем с дебаунсом при динамич. опциях
111
- const [shouldShowDefaultOption, setShouldShowDefaultOption] = useState(true);
112
-
113
- const inputWrapper = useRef<HTMLDivElement>(null);
114
- const list = useRef<HTMLDivElement>(null);
115
- const input = useRef<HTMLInputElement>(null); // TODO ref снаружи?
116
-
117
- const shouldRenderSearchInputInList =
118
- searchInput?.shouldRenderInList === true;
119
-
120
- const hasSearchInputInList =
121
- optionsMode !== 'normal' && shouldRenderSearchInputInList;
122
-
123
- const stringValue = isNotEmpty(value)
124
- ? convertValueToString(value)
125
- : undefined;
126
-
127
- const filteredOptions = useMemo(() => {
128
- if (optionsMode !== 'search') {
129
- return options;
130
- }
131
- if (optionsFilter) {
132
- return optionsFilter(options, searchValue);
133
- }
134
- const lowerCaseValue = searchValue?.toLowerCase();
135
- return options.filter((option) => {
136
- const convertedOption = convertValueToString(option);
137
- return (
138
- convertedOption !== undefined &&
139
- convertedOption.toLowerCase().includes(lowerCaseValue)
140
- );
141
- });
142
- }, [optionsMode, optionsFilter, options, convertValueToString, searchValue]);
143
-
144
- useEffect(() => {
145
- setFocusedListCellIndex(
146
- getActiveValueIndex(filteredOptions, value, convertValueToString),
147
- );
148
- }, [filteredOptions, value, convertValueToString]);
149
-
150
- const handleListClose = (event: Event | SyntheticEvent) => {
151
- setIsListOpen(false);
152
- setSearchValue('');
153
- setShouldShowDefaultOption(true);
154
- onBlur?.(event);
155
- };
156
-
157
- const handleListOpen = () => {
158
- if (!isListOpen) {
159
- setIsListOpen(true);
160
- }
161
- };
162
-
163
- const handleFocus = (event: FocusEvent<HTMLInputElement>) => {
164
- onFocus?.(event);
165
- handleListOpen();
166
- };
167
-
168
- const handleOnClick = () => {
169
- handleListOpen();
170
- };
171
-
172
- const handleBlur = (event: FocusEvent<HTMLInputElement>) => {
173
- // Когда что-то блокирует открытие листа, но блур все равно должен сработать
174
- // например minSymbolsCount
175
- if (isListOpen && !isOpen) {
176
- handleListClose(event);
177
- return;
178
- }
179
-
180
- if (!isNotEmpty(event.relatedTarget) || !isNotEmpty(list.current)) {
181
- return;
182
- }
183
-
184
- const isActionInsideList = hasExactParent(
185
- event.relatedTarget,
186
- list.current,
187
- );
188
-
189
- // Ниче не делаем если клик был внутри селекта
190
- if (!isActionInsideList) {
191
- handleListClose(event);
192
- }
193
- };
194
-
195
- const handleOnChange = useCallback(
196
- (newValue: Value | undefined) => {
197
- const areValuesEqual = compareValuesOnChange(value, newValue);
198
- if (areValuesEqual) {
199
- return;
200
- }
201
- onChange(newValue);
202
- },
203
- [value, onChange],
204
- );
205
-
206
- const handleOptionSelect = useCallback(
207
- (index: number, event: MouseEvent<HTMLElement> | KeyboardEvent) => {
208
- handleOnChange(index === -1 ? undefined : filteredOptions[index]);
209
- handleListClose(event);
210
- input.current?.blur();
211
- },
212
- [handleOnChange, filteredOptions],
213
- );
214
-
215
- const handleOnType = useCallback(
216
- async (v: string) => {
217
- if (onType === undefined) {
218
- return;
219
- }
220
- if (isMounted()) {
221
- setAreOptionsLoading(true);
222
- }
223
- await onType(v);
224
- if (isMounted()) {
225
- setAreOptionsLoading(false);
226
- }
227
- if (optionsMode === 'dynamic') {
228
- setShouldShowDefaultOption(v === '');
229
- }
230
- },
231
- [onType, optionsMode],
232
- );
233
-
234
- const debounceHandleOnType = useCallback(
235
- debounce(handleOnType, debounceTime),
236
- [handleOnType, debounceTime],
237
- );
238
-
239
- const handleInputChange = (v: string) => {
240
- if (onType !== undefined) {
241
- debounceHandleOnType(v);
242
- }
243
-
244
- if (optionsMode !== 'dynamic') {
245
- setShouldShowDefaultOption(v === '');
246
- }
247
-
248
- if (v === '' && !hasSearchInputInList) {
249
- handleOnChange(undefined);
250
- }
251
-
252
- setSearchValue(v);
253
- };
254
-
255
- const handleKeyDown = (event: KeyboardEvent) => {
256
- if (!isListOpen) {
257
- return;
258
- }
259
-
260
- event.stopPropagation();
261
-
262
- switch (event.code) {
263
- case 'Enter':
264
- case 'NumpadEnter': {
265
- let indexToClick = focusedListCellIndex;
266
-
267
- // если осталось одна опция в списке,
268
- // то выбираем ее нажатием на enter
269
- if (indexToClick === -1 && filteredOptions.length === 1) {
270
- indexToClick = 0;
271
- }
272
-
273
- handleOptionSelect(indexToClick, event);
274
- break;
275
- }
276
-
277
- case 'ArrowDown': {
278
- // чтобы убрать перемещение курсора в инпуте
279
- event.preventDefault();
280
- let newIndex: number = focusedListCellIndex ?? -1;
281
- // ищем первый незадизейбленный вариант, либо дефолтную опцию
282
- for (let i = newIndex; i < newIndex + filteredOptions.length; i++) {
283
- const targetIndex = (i + 1) % filteredOptions.length;
284
- if (
285
- shouldShowDefaultOption &&
286
- hasDefaultOption &&
287
- newIndex > -1 &&
288
- targetIndex === 0
289
- ) {
290
- newIndex = -1;
291
- break;
292
- }
293
- if (!isOptionDisabled(filteredOptions[targetIndex])) {
294
- newIndex = targetIndex;
295
- break;
296
- }
297
- }
298
- setFocusedListCellIndex(newIndex);
299
- break;
300
- }
301
-
302
- case 'ArrowUp': {
303
- // чтобы убрать перемещение курсора в инпуте
304
- event.preventDefault();
305
- const newIndex: number =
306
- (focusedListCellIndex ?? -1) === -1 ? 0 : focusedListCellIndex;
307
- // ищем первый незадизейбленный вариант, либо дефолтную опцию
308
- for (let i = newIndex; i > newIndex - filteredOptions.length; i--) {
309
- const targetIndex = (i > 0 ? i : filteredOptions.length + i) - 1;
310
- if (
311
- shouldShowDefaultOption &&
312
- hasDefaultOption &&
313
- focusedListCellIndex !== -1 &&
314
- targetIndex === filteredOptions.length - 1
315
- ) {
316
- // не выносить сет наружу (приведет к багу)
317
- setFocusedListCellIndex(-1);
318
- break;
319
- }
320
- if (!isOptionDisabled(filteredOptions[targetIndex])) {
321
- // не выносить сет наружу (приведет к багу)
322
- setFocusedListCellIndex(targetIndex);
323
- break;
324
- }
325
- }
326
- break;
327
- }
328
- }
329
- };
330
-
331
- const onArrowClick = () => {
332
- if (isListOpen) {
333
- input.current?.blur();
334
- } else {
335
- input.current?.focus();
336
- }
337
- };
338
-
339
- useOnClickOutsideWithRef(list, handleListClose, inputWrapper);
340
-
341
- const hasEnoughSymbolsToSearch =
342
- searchValue.trim().length >= minSymbolsCountToOpenList;
343
-
344
- const isOpen =
345
- // Пользователь пытается открыть лист
346
- isListOpen &&
347
- // Нам есть что показать:
348
- // Есть опции
349
- (filteredOptions.length > 0 ||
350
- // Дефолтная опция
351
- (defaultOptionLabel !== undefined && !hasEnoughSymbolsToSearch) ||
352
- // Текст "Загрузка..."
353
- inputProps.isLoading ||
354
- // Текст "Совпадений не найдено"
355
- noMatchesLabel !== undefined ||
356
- // У нас есть инпут с поиском внутри листа
357
- hasSearchInputInList) &&
358
- // Последняя проверка на случай, если мы че то ищем в опциях
359
- (optionsMode === 'normal' || hasEnoughSymbolsToSearch);
360
-
361
- const { isReadonly = true } = inputProps;
362
- const shouldUsePointerCursor =
363
- (optionsMode === 'normal' || shouldRenderSearchInputInList) && isReadonly;
364
-
365
- const tweakInputStyles = useMemo(
366
- () =>
367
- merge(
368
- {},
369
- componentStyles.tweakInput,
370
- {
371
- ...(shouldUsePointerCursor
372
- ? { input: { cursor: 'pointer' } }
373
- : undefined),
374
- },
375
- tweakStyles?.tweakInput,
376
- ) as Styles,
377
- [tweakStyles?.tweakInput, shouldUsePointerCursor],
378
- );
379
-
380
- const tweakSearchInputStyles = useTweakStyles(
381
- componentStyles,
382
- tweakStyles,
383
- 'tweakSearchInput',
384
- );
385
-
386
- // Эти значения ставятся в false по дефолту также в useDropdown
387
- const {
388
- shouldUsePopper = false,
389
- shouldRenderInBody = false,
390
- shouldHideOnScroll = false,
391
- } = dropdownOptions ?? {};
392
-
393
- const popperData = useDropdown({
394
- isOpen,
395
- onDropdownClose: handleListClose,
396
- referenceElement: inputWrapper.current,
397
- dropdownElement: list.current,
398
- options: dropdownOptions,
399
- dependenciesForPositionUpdating: [
400
- inputProps.isLoading,
401
- filteredOptions.length,
402
- ],
403
- });
404
-
405
- useEffect(() => {
406
- if (isOpen && onOpen !== undefined) {
407
- onOpen();
408
- }
409
- }, [isOpen, onOpen]);
410
-
411
- const listEl = (
412
- <div
413
- className={clsx(classes.listWrapper, {
414
- [classes.withoutPopper]: !shouldUsePopper,
415
- [classes.listWrapperInBody]: shouldRenderInBody,
416
- })}
417
- ref={list}
418
- style={popperData?.styles.popper as Styles}
419
- onBlur={handleBlur} // обработка для Tab из списка
420
- {...popperData?.attributes.popper}
421
- >
422
- {isOpen && (
423
- <SelectList
424
- options={filteredOptions}
425
- defaultOptionLabel={
426
- hasDefaultOption && shouldShowDefaultOption
427
- ? defaultOptionLabel
428
- : undefined
429
- }
430
- customListHeader={
431
- hasSearchInputInList ? (
432
- <SearchInput
433
- value={searchValue}
434
- onChange={handleInputChange}
435
- tweakStyles={tweakSearchInputStyles}
436
- placeholder="Поиск"
437
- {...searchInput}
438
- />
439
- ) : undefined
440
- }
441
- noMatchesLabel={noMatchesLabel}
442
- focusedIndex={focusedListCellIndex}
443
- activeValue={value}
444
- isLoading={inputProps.isLoading}
445
- loadingLabel={loadingLabel}
446
- tweakStyles={tweakStyles?.tweakSelectList as Styles}
447
- testId={getTestId(testId, 'list')}
448
- // скролл не работает с включеным поппером
449
- shouldScrollToList={
450
- shouldScrollToList && !shouldUsePopper && !shouldHideOnScroll
451
- }
452
- isOptionDisabled={isOptionDisabled}
453
- convertValueToString={convertValueToString}
454
- convertValueToReactNode={convertValueToReactNode}
455
- convertValueToId={convertValueToId}
456
- onOptionClick={handleOptionSelect}
457
- />
458
- )}
459
- </div>
460
- );
461
-
462
- return (
463
- <div className={classes.root} onKeyDown={handleKeyDown}>
464
- <div
465
- className={clsx(classes.inputWrapper, isDisabled && classes.disabled)}
466
- onClick={isDisabled ? undefined : handleOnClick}
467
- ref={inputWrapper}
468
- >
469
- <Input
470
- value={
471
- searchValue !== '' && !shouldRenderSearchInputInList
472
- ? searchValue
473
- : stringValue
474
- }
475
- onChange={handleInputChange}
476
- isActive={isListOpen}
477
- isReadonly={optionsMode === 'normal' || shouldRenderSearchInputInList}
478
- onFocus={handleFocus}
479
- onBlur={handleBlur}
480
- isDisabled={isDisabled}
481
- ref={input}
482
- isLoading={areOptionsLoading}
483
- tweakStyles={tweakInputStyles}
484
- testId={testId}
485
- {...inputProps}
486
- />
487
- <div
488
- onMouseDown={(event: MouseEvent) => {
489
- event.preventDefault();
490
- }}
491
- onClick={onArrowClick}
492
- className={clsx(classes.arrow, isOpen && classes.activeArrow)}
493
- >
494
- <Icon type={dropdownIcon} />
495
- </div>
496
- </div>
497
- {shouldUsePopper ? (
498
- <Portal
499
- container={shouldRenderInBody ? document.body : inputWrapper.current}
500
- >
501
- <>{listEl}</>
502
- </Portal>
503
- ) : (
504
- <>{isOpen && listEl}</>
505
- )}
506
- </div>
507
- );
508
- }
1
+ import {
2
+ ReactNode,
3
+ FocusEvent,
4
+ KeyboardEvent,
5
+ MouseEvent,
6
+ useCallback,
7
+ useEffect,
8
+ useMemo,
9
+ useRef,
10
+ useState,
11
+ SyntheticEvent,
12
+ } from 'react';
13
+ import { Styles } from 'jss';
14
+ import clsx from 'clsx';
15
+ import { merge } from 'lodash';
16
+ import { debounce } from 'ts-debounce';
17
+ import { Portal } from 'react-overlays';
18
+ import { SelectList } from './SelectList';
19
+ import { IInputProps, Input } from '../Input';
20
+ import { IIconType, Icon } from '../Icon';
21
+ import {
22
+ useIsMounted,
23
+ useTheme,
24
+ useOnClickOutsideWithRef,
25
+ useDropdown,
26
+ useTweakStyles,
27
+ } from '../../hooks';
28
+ import { IDropdownWithPopperOptions } from '../../types';
29
+ import { getTestId, hasExactParent, isNotEmpty } from '../../helpers';
30
+ import {
31
+ defaultConvertFunction,
32
+ defaultCompareFunction,
33
+ getActiveValueIndex,
34
+ defaultIsOptionDisabled,
35
+ } from './helpers';
36
+ import { SelectStyles, styles } from './Select.styles';
37
+ import { ISearchInputProps, SearchInput } from '../SearchInput';
38
+
39
+ export interface ISelectProps<Value>
40
+ extends Omit<IInputProps, 'value' | 'onChange' | 'onBlur' | 'type'> {
41
+ tweakStyles?: SelectStyles;
42
+ defaultOptionLabel?: string;
43
+ noMatchesLabel?: string;
44
+ loadingLabel?: ReactNode;
45
+ optionsMode?: 'search' | 'dynamic' | 'normal';
46
+ debounceTime?: number;
47
+ minSymbolsCountToOpenList?: number;
48
+ dropdownOptions?: IDropdownWithPopperOptions;
49
+ dropdownIcon?: IIconType;
50
+ options: Value[];
51
+ value: Value | undefined;
52
+ shouldScrollToList?: boolean;
53
+ searchInput?: { shouldRenderInList: true } & Pick<
54
+ ISearchInputProps,
55
+ 'placeholder'
56
+ >;
57
+ isOptionDisabled?(option: Value): boolean;
58
+ onChange(value: Value | undefined): void; // подумать как возвращать индекс
59
+ onBlur?(event: Event | SyntheticEvent): void;
60
+ onType?(value: string): Promise<void>;
61
+ optionsFilter?(options: Value[], query: string): Value[];
62
+ onOpen?(): void;
63
+ compareValuesOnChange?(v1: Value | undefined, v2: Value | undefined): boolean;
64
+ // Для избежания проблем юзайте useCallback на эти функции
65
+ // или выносите их из компонента (чтобы не было сайдэфектов от перерендеринга их)
66
+ convertValueToString?(value: Value): string | undefined;
67
+ convertValueToReactNode?(value: Value, isDisabled: boolean): ReactNode;
68
+ convertValueToId?(value: Value): string | undefined;
69
+ }
70
+
71
+ export function Select<Value>({
72
+ options,
73
+ value,
74
+ defaultOptionLabel,
75
+ debounceTime = 400,
76
+ optionsMode = 'normal',
77
+ noMatchesLabel,
78
+ loadingLabel,
79
+ tweakStyles,
80
+ testId,
81
+ isDisabled,
82
+ dropdownOptions,
83
+ minSymbolsCountToOpenList = 0,
84
+ dropdownIcon = 'chevron-down',
85
+ shouldScrollToList = true,
86
+ searchInput,
87
+ onChange,
88
+ onFocus,
89
+ onBlur,
90
+ onType,
91
+ onOpen,
92
+ isOptionDisabled = defaultIsOptionDisabled,
93
+ compareValuesOnChange = defaultCompareFunction,
94
+ convertValueToString = defaultConvertFunction,
95
+ convertValueToId,
96
+ convertValueToReactNode,
97
+ optionsFilter,
98
+ ...inputProps
99
+ }: ISelectProps<Value>): JSX.Element {
100
+ const { classes, componentStyles } = useTheme('Select', styles, tweakStyles);
101
+ const isMounted = useIsMounted();
102
+ const [isListOpen, setIsListOpen] = useState(false);
103
+ const [areOptionsLoading, setAreOptionsLoading] = useState(false);
104
+ const hasDefaultOption = defaultOptionLabel !== undefined;
105
+
106
+ const [focusedListCellIndex, setFocusedListCellIndex] = useState(-1);
107
+ const [searchValue, setSearchValue] = useState('');
108
+
109
+ // если мы ввели что то в строку поиска - то этот булеан будет отключаться
110
+ // вынесен отдельно, из-за проблем с дебаунсом при динамич. опциях
111
+ const [shouldShowDefaultOption, setShouldShowDefaultOption] = useState(true);
112
+
113
+ const inputWrapper = useRef<HTMLDivElement>(null);
114
+ const list = useRef<HTMLDivElement>(null);
115
+ const input = useRef<HTMLInputElement>(null); // TODO ref снаружи?
116
+
117
+ const shouldRenderSearchInputInList =
118
+ searchInput?.shouldRenderInList === true;
119
+
120
+ const hasSearchInputInList =
121
+ optionsMode !== 'normal' && shouldRenderSearchInputInList;
122
+
123
+ const stringValue = isNotEmpty(value)
124
+ ? convertValueToString(value)
125
+ : undefined;
126
+
127
+ const filteredOptions = useMemo(() => {
128
+ if (optionsMode !== 'search') {
129
+ return options;
130
+ }
131
+ if (isNotEmpty(optionsFilter)) {
132
+ return optionsFilter(options, searchValue);
133
+ }
134
+ const lowerCaseValue = searchValue?.toLowerCase();
135
+ return options.filter((option) => {
136
+ const convertedOption = convertValueToString(option);
137
+ return (
138
+ convertedOption !== undefined &&
139
+ convertedOption.toLowerCase().includes(lowerCaseValue)
140
+ );
141
+ });
142
+ }, [optionsMode, optionsFilter, options, convertValueToString, searchValue]);
143
+
144
+ useEffect(() => {
145
+ setFocusedListCellIndex(
146
+ getActiveValueIndex(filteredOptions, value, convertValueToString),
147
+ );
148
+ }, [filteredOptions, value, convertValueToString]);
149
+
150
+ const handleListClose = useCallback(
151
+ (event: Event | SyntheticEvent) => {
152
+ setIsListOpen(false);
153
+ setSearchValue('');
154
+ setShouldShowDefaultOption(true);
155
+ onBlur?.(event);
156
+ },
157
+ [onBlur],
158
+ );
159
+
160
+ const handleListOpen = () => {
161
+ if (!isListOpen) {
162
+ setIsListOpen(true);
163
+ }
164
+ };
165
+
166
+ const handleFocus = (event: FocusEvent<HTMLInputElement>) => {
167
+ onFocus?.(event);
168
+ handleListOpen();
169
+ };
170
+
171
+ const handleOnClick = () => {
172
+ handleListOpen();
173
+ };
174
+
175
+ const handleBlur = (event: FocusEvent<HTMLInputElement>) => {
176
+ // Когда что-то блокирует открытие листа, но блур все равно должен сработать
177
+ // например minSymbolsCount
178
+ if (isListOpen && !isOpen) {
179
+ handleListClose(event);
180
+ return;
181
+ }
182
+
183
+ if (
184
+ !isNotEmpty(event.relatedTarget) ||
185
+ !isNotEmpty(list.current) ||
186
+ !isNotEmpty(inputWrapper.current)
187
+ ) {
188
+ return;
189
+ }
190
+
191
+ const isActionInsideSelect =
192
+ hasExactParent(event.relatedTarget as Element, list.current) ||
193
+ hasExactParent(event.relatedTarget as Element, inputWrapper.current);
194
+
195
+ // Ниче не делаем если клик был внутри селекта
196
+ if (!isActionInsideSelect) {
197
+ handleListClose(event);
198
+ }
199
+ };
200
+
201
+ const handleOnChange = useCallback(
202
+ (newValue: Value | undefined) => {
203
+ const areValuesEqual = compareValuesOnChange(value, newValue);
204
+ if (areValuesEqual) {
205
+ return;
206
+ }
207
+ onChange(newValue);
208
+ },
209
+ [value, compareValuesOnChange, onChange],
210
+ );
211
+
212
+ const handleOptionSelect = useCallback(
213
+ (index: number, event: MouseEvent<HTMLElement> | KeyboardEvent) => {
214
+ handleOnChange(index === -1 ? undefined : filteredOptions[index]);
215
+ handleListClose(event);
216
+ input.current?.blur();
217
+ },
218
+ [handleOnChange, handleListClose, filteredOptions],
219
+ );
220
+
221
+ const handleOnType = useCallback(
222
+ async (v: string) => {
223
+ if (onType === undefined) {
224
+ return;
225
+ }
226
+ if (isMounted()) {
227
+ setAreOptionsLoading(true);
228
+ }
229
+ await onType(v);
230
+ if (isMounted()) {
231
+ setAreOptionsLoading(false);
232
+ }
233
+ if (optionsMode === 'dynamic') {
234
+ setShouldShowDefaultOption(v === '');
235
+ }
236
+ },
237
+ [onType, optionsMode],
238
+ );
239
+
240
+ const debounceHandleOnType = useCallback(
241
+ debounce(handleOnType, debounceTime),
242
+ [handleOnType, debounceTime],
243
+ );
244
+
245
+ const handleInputChange = (v: string) => {
246
+ if (onType !== undefined) {
247
+ debounceHandleOnType(v);
248
+ }
249
+
250
+ if (optionsMode !== 'dynamic') {
251
+ setShouldShowDefaultOption(v === '');
252
+ }
253
+
254
+ if (v === '' && !hasSearchInputInList) {
255
+ handleOnChange(undefined);
256
+ }
257
+
258
+ setSearchValue(v);
259
+ };
260
+
261
+ const handleKeyDown = (event: KeyboardEvent) => {
262
+ if (!isListOpen) {
263
+ return;
264
+ }
265
+
266
+ event.stopPropagation();
267
+
268
+ switch (event.code) {
269
+ case 'Enter':
270
+ case 'NumpadEnter': {
271
+ let indexToClick = focusedListCellIndex;
272
+
273
+ // если осталось одна опция в списке,
274
+ // то выбираем ее нажатием на enter
275
+ if (indexToClick === -1 && filteredOptions.length === 1) {
276
+ indexToClick = 0;
277
+ }
278
+
279
+ handleOptionSelect(indexToClick, event);
280
+ break;
281
+ }
282
+
283
+ case 'ArrowDown': {
284
+ // чтобы убрать перемещение курсора в инпуте
285
+ event.preventDefault();
286
+ let newIndex: number = focusedListCellIndex ?? -1;
287
+ // ищем первый незадизейбленный вариант, либо дефолтную опцию
288
+ for (let i = newIndex; i < newIndex + filteredOptions.length; i++) {
289
+ const targetIndex = (i + 1) % filteredOptions.length;
290
+ if (
291
+ shouldShowDefaultOption &&
292
+ hasDefaultOption &&
293
+ newIndex > -1 &&
294
+ targetIndex === 0
295
+ ) {
296
+ newIndex = -1;
297
+ break;
298
+ }
299
+ if (!isOptionDisabled(filteredOptions[targetIndex])) {
300
+ newIndex = targetIndex;
301
+ break;
302
+ }
303
+ }
304
+ setFocusedListCellIndex(newIndex);
305
+ break;
306
+ }
307
+
308
+ case 'ArrowUp': {
309
+ // чтобы убрать перемещение курсора в инпуте
310
+ event.preventDefault();
311
+ const newIndex: number =
312
+ (focusedListCellIndex ?? -1) === -1 ? 0 : focusedListCellIndex;
313
+ // ищем первый незадизейбленный вариант, либо дефолтную опцию
314
+ for (let i = newIndex; i > newIndex - filteredOptions.length; i--) {
315
+ const targetIndex = (i > 0 ? i : filteredOptions.length + i) - 1;
316
+ if (
317
+ shouldShowDefaultOption &&
318
+ hasDefaultOption &&
319
+ focusedListCellIndex !== -1 &&
320
+ targetIndex === filteredOptions.length - 1
321
+ ) {
322
+ // не выносить сет наружу (приведет к багу)
323
+ setFocusedListCellIndex(-1);
324
+ break;
325
+ }
326
+ if (!isOptionDisabled(filteredOptions[targetIndex])) {
327
+ // не выносить сет наружу (приведет к багу)
328
+ setFocusedListCellIndex(targetIndex);
329
+ break;
330
+ }
331
+ }
332
+ break;
333
+ }
334
+ }
335
+ };
336
+
337
+ const onArrowClick = () => {
338
+ if (isListOpen) {
339
+ input.current?.blur();
340
+ } else {
341
+ input.current?.focus();
342
+ }
343
+ };
344
+
345
+ useOnClickOutsideWithRef(list, handleListClose, inputWrapper);
346
+
347
+ const hasEnoughSymbolsToSearch =
348
+ searchValue.trim().length >= minSymbolsCountToOpenList;
349
+
350
+ const isOpen =
351
+ // Пользователь пытается открыть лист
352
+ isListOpen &&
353
+ // Нам есть что показать:
354
+ // Есть опции
355
+ (filteredOptions.length > 0 ||
356
+ // Дефолтная опция
357
+ (defaultOptionLabel !== undefined && !hasEnoughSymbolsToSearch) ||
358
+ // Текст "Загрузка..."
359
+ inputProps.isLoading ||
360
+ // Текст "Совпадений не найдено"
361
+ noMatchesLabel !== undefined ||
362
+ // У нас есть инпут с поиском внутри листа
363
+ hasSearchInputInList) &&
364
+ // Последняя проверка на случай, если мы че то ищем в опциях
365
+ (optionsMode === 'normal' || hasEnoughSymbolsToSearch);
366
+
367
+ const { isReadonly = true } = inputProps;
368
+ const shouldUsePointerCursor =
369
+ (optionsMode === 'normal' || shouldRenderSearchInputInList) && isReadonly;
370
+
371
+ const tweakInputStyles = useMemo(
372
+ () =>
373
+ merge(
374
+ {},
375
+ componentStyles.tweakInput,
376
+ {
377
+ ...(shouldUsePointerCursor
378
+ ? { input: { cursor: 'pointer' } }
379
+ : undefined),
380
+ },
381
+ tweakStyles?.tweakInput,
382
+ ) as Styles,
383
+ [tweakStyles?.tweakInput, shouldUsePointerCursor],
384
+ );
385
+
386
+ const tweakSearchInputStyles = useTweakStyles(
387
+ componentStyles,
388
+ tweakStyles,
389
+ 'tweakSearchInput',
390
+ );
391
+
392
+ // Эти значения ставятся в false по дефолту также в useDropdown
393
+ const {
394
+ shouldUsePopper = false,
395
+ shouldRenderInBody = false,
396
+ shouldHideOnScroll = false,
397
+ } = dropdownOptions ?? {};
398
+
399
+ const popperData = useDropdown({
400
+ isOpen,
401
+ onDropdownClose: handleListClose,
402
+ referenceElement: inputWrapper.current,
403
+ dropdownElement: list.current,
404
+ options: dropdownOptions,
405
+ dependenciesForPositionUpdating: [
406
+ inputProps.isLoading,
407
+ filteredOptions.length,
408
+ ],
409
+ });
410
+
411
+ useEffect(() => {
412
+ if (isOpen && onOpen !== undefined) {
413
+ onOpen();
414
+ }
415
+ }, [isOpen, onOpen]);
416
+
417
+ const listEl = (
418
+ <div
419
+ className={clsx(classes.listWrapper, {
420
+ [classes.withoutPopper]: !shouldUsePopper,
421
+ [classes.listWrapperInBody]: shouldRenderInBody,
422
+ })}
423
+ ref={list}
424
+ style={popperData?.styles.popper as Styles}
425
+ onBlur={handleBlur} // обработка для Tab из списка
426
+ {...popperData?.attributes.popper}
427
+ >
428
+ {isOpen && (
429
+ <SelectList
430
+ options={filteredOptions}
431
+ defaultOptionLabel={
432
+ hasDefaultOption && shouldShowDefaultOption
433
+ ? defaultOptionLabel
434
+ : undefined
435
+ }
436
+ customListHeader={
437
+ hasSearchInputInList ? (
438
+ <SearchInput
439
+ value={searchValue}
440
+ onChange={handleInputChange}
441
+ tweakStyles={tweakSearchInputStyles}
442
+ placeholder="Поиск"
443
+ {...searchInput}
444
+ />
445
+ ) : undefined
446
+ }
447
+ noMatchesLabel={noMatchesLabel}
448
+ focusedIndex={focusedListCellIndex}
449
+ activeValue={value}
450
+ isLoading={inputProps.isLoading}
451
+ loadingLabel={loadingLabel}
452
+ tweakStyles={tweakStyles?.tweakSelectList as Styles}
453
+ testId={getTestId(testId, 'list')}
454
+ // скролл не работает с включеным поппером
455
+ shouldScrollToList={
456
+ shouldScrollToList && !shouldUsePopper && !shouldHideOnScroll
457
+ }
458
+ isOptionDisabled={isOptionDisabled}
459
+ convertValueToString={convertValueToString}
460
+ convertValueToReactNode={convertValueToReactNode}
461
+ convertValueToId={convertValueToId}
462
+ onOptionClick={handleOptionSelect}
463
+ />
464
+ )}
465
+ </div>
466
+ );
467
+
468
+ return (
469
+ <div className={classes.root} onKeyDown={handleKeyDown}>
470
+ <div
471
+ className={clsx(classes.inputWrapper, isDisabled && classes.disabled)}
472
+ onClick={isDisabled ? undefined : handleOnClick}
473
+ ref={inputWrapper}
474
+ >
475
+ <Input
476
+ value={
477
+ searchValue !== '' && !shouldRenderSearchInputInList
478
+ ? searchValue
479
+ : stringValue
480
+ }
481
+ onChange={handleInputChange}
482
+ isActive={isListOpen}
483
+ isReadonly={
484
+ isReadonly ??
485
+ (optionsMode === 'normal' || shouldRenderSearchInputInList)
486
+ }
487
+ onFocus={handleFocus}
488
+ onBlur={handleBlur}
489
+ isDisabled={isDisabled}
490
+ ref={input}
491
+ isLoading={areOptionsLoading}
492
+ tweakStyles={tweakInputStyles}
493
+ testId={testId}
494
+ {...inputProps}
495
+ />
496
+ <div
497
+ onMouseDown={(event: MouseEvent) => {
498
+ event.preventDefault();
499
+ }}
500
+ onClick={onArrowClick}
501
+ className={clsx(classes.arrow, isOpen && classes.activeArrow)}
502
+ >
503
+ <Icon type={dropdownIcon} />
504
+ </div>
505
+ </div>
506
+ {shouldUsePopper ? (
507
+ <Portal
508
+ container={shouldRenderInBody ? document.body : inputWrapper.current}
509
+ >
510
+ <>{listEl}</>
511
+ </Portal>
512
+ ) : (
513
+ <>{isOpen && listEl}</>
514
+ )}
515
+ </div>
516
+ );
517
+ }