@true-engineering/true-react-common-ui-kit 1.5.1 → 1.5.3
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/LICENSE +201 -201
- package/dist/components/Flag/augment.d.ts +1 -1
- package/dist/components/Icon/complexIcons/augment.d.ts +1 -1
- package/dist/components/Input/Input.d.ts +4 -4
- package/dist/true-react-common-ui-kit.js +61 -59
- package/dist/true-react-common-ui-kit.js.map +1 -1
- package/dist/true-react-common-ui-kit.umd.cjs +61 -59
- package/dist/true-react-common-ui-kit.umd.cjs.map +1 -1
- package/dist/vite-env.d.ts +1 -1
- package/package.json +91 -91
- package/src/components/AccountInfo/AccountInfo.stories.tsx +35 -35
- package/src/components/AccountInfo/AccountInfo.styles.ts +55 -55
- package/src/components/AccountInfo/AccountInfo.tsx +106 -106
- package/src/components/AccountInfo/index.ts +2 -2
- package/src/components/AddButton/AddButton.stories.tsx +21 -21
- package/src/components/AddButton/AddButton.styles.ts +34 -34
- package/src/components/AddButton/AddButton.tsx +49 -49
- package/src/components/AddButton/index.ts +2 -2
- package/src/components/Button/Button.stories.tsx +61 -61
- package/src/components/Button/Button.styles.ts +196 -196
- package/src/components/Button/Button.tsx +195 -195
- package/src/components/Button/index.ts +2 -2
- package/src/components/Checkbox/Checkbox.stories.tsx +35 -35
- package/src/components/Checkbox/Checkbox.styles.ts +59 -59
- package/src/components/Checkbox/Checkbox.tsx +93 -93
- package/src/components/Checkbox/index.ts +2 -2
- package/src/components/CloseButton/CloseButton.styles.ts +34 -34
- package/src/components/CloseButton/CloseButton.tsx +37 -37
- package/src/components/CloseButton/index.ts +2 -2
- package/src/components/Colors/Colors.stories.tsx +7 -7
- package/src/components/Colors/Colors.styles.ts +38 -38
- package/src/components/Colors/Colors.tsx +34 -34
- package/src/components/Colors/index.ts +2 -2
- package/src/components/CssBaseline/CssBaseline.styles.ts +15 -15
- package/src/components/CssBaseline/CssBaseline.tsx +17 -17
- package/src/components/CssBaseline/index.ts +2 -2
- package/src/components/DateInput/DateInput.stories.tsx +63 -63
- package/src/components/DateInput/DateInput.styles.ts +14 -14
- package/src/components/DateInput/DateInput.tsx +60 -60
- package/src/components/DateInput/index.ts +2 -2
- package/src/components/DatePicker/DatePicker.stories.tsx +96 -96
- package/src/components/DatePicker/DatePicker.styles.ts +54 -54
- package/src/components/DatePicker/DatePicker.tsx +358 -358
- package/src/components/DatePicker/DatePickerHeader/DatePickerHeader.styles.ts +84 -84
- package/src/components/DatePicker/DatePickerHeader/DatePickerHeader.tsx +94 -94
- package/src/components/DatePicker/DatePickerHeader/index.ts +1 -1
- package/src/components/DatePicker/DatePickerInput/DatePickerInput.styles.ts +25 -25
- package/src/components/DatePicker/DatePickerInput/DatePickerInput.tsx +31 -31
- package/src/components/DatePicker/DatePickerInput/index.ts +1 -1
- package/src/components/DatePicker/index.ts +4 -4
- package/src/components/Description/Description.stories.tsx +29 -29
- package/src/components/Description/Description.styles.ts +31 -31
- package/src/components/Description/Description.tsx +69 -69
- package/src/components/Description/index.ts +2 -2
- package/src/components/FiltersPane/FilterInterval/FilterInterval.styles.ts +64 -64
- package/src/components/FiltersPane/FilterInterval/FilterInterval.tsx +162 -162
- package/src/components/FiltersPane/FilterInterval/index.ts +1 -1
- package/src/components/FiltersPane/FilterMultiSelect/FilterMultiSelect.tsx +14 -14
- package/src/components/FiltersPane/FilterMultiSelect/index.ts +1 -1
- package/src/components/FiltersPane/FilterSelect/FilterSelect.styles.ts +144 -144
- package/src/components/FiltersPane/FilterSelect/FilterSelect.tsx +397 -397
- package/src/components/FiltersPane/FilterSelect/index.ts +1 -1
- package/src/components/FiltersPane/FilterSelect/locales.ts +37 -37
- package/src/components/FiltersPane/FilterValueView/FilterValueView.styles.tsx +15 -15
- package/src/components/FiltersPane/FilterValueView/FilterValueView.tsx +186 -186
- package/src/components/FiltersPane/FilterValueView/index.tsx +1 -1
- package/src/components/FiltersPane/FilterWithDates/FilterWithDates.styles.ts +60 -60
- package/src/components/FiltersPane/FilterWithDates/FilterWithDates.tsx +222 -222
- package/src/components/FiltersPane/FilterWithDates/index.ts +1 -1
- package/src/components/FiltersPane/FilterWithPeriod/FilterWithPeriod.styles.ts +17 -17
- package/src/components/FiltersPane/FilterWithPeriod/FilterWithPeriod.tsx +231 -231
- package/src/components/FiltersPane/FilterWithPeriod/index.ts +1 -1
- package/src/components/FiltersPane/FilterWrapper/FilterWrapper.styles.ts +110 -110
- package/src/components/FiltersPane/FilterWrapper/FilterWrapper.tsx +360 -360
- package/src/components/FiltersPane/FilterWrapper/index.ts +1 -1
- package/src/components/FiltersPane/FiltersPane.stories.tsx +308 -308
- package/src/components/FiltersPane/FiltersPane.styles.ts +71 -71
- package/src/components/FiltersPane/FiltersPane.tsx +193 -193
- package/src/components/FiltersPane/FiltersPaneSearch/FiltersPaneSearch.styles.ts +109 -109
- package/src/components/FiltersPane/FiltersPaneSearch/FiltersPaneSearch.tsx +175 -175
- package/src/components/FiltersPane/FiltersPaneSearch/index.ts +1 -1
- package/src/components/FiltersPane/index.ts +20 -20
- package/src/components/FiltersPane/locales.ts +107 -107
- package/src/components/FiltersPane/types.ts +126 -126
- package/src/components/Flag/Flag.stories.tsx +29 -29
- package/src/components/Flag/Flag.styles.ts +18 -18
- package/src/components/Flag/Flag.tsx +28 -28
- package/src/components/Flag/augment.d.ts +1 -1
- package/src/components/Flag/index.ts +2 -2
- package/src/components/FlexibleTable/FlexibleTable.stories.tsx +80 -80
- package/src/components/FlexibleTable/FlexibleTable.styles.ts +131 -131
- package/src/components/FlexibleTable/FlexibleTable.tsx +243 -243
- package/src/components/FlexibleTable/TableRow.tsx +171 -171
- package/src/components/FlexibleTable/TableValue.tsx +83 -83
- package/src/components/FlexibleTable/fixture-test.ts +254 -254
- package/src/components/FlexibleTable/index.ts +3 -3
- package/src/components/FlexibleTable/types.ts +58 -58
- package/src/components/Icon/ComplexIconBoilerplate.tsx +17 -17
- package/src/components/Icon/Icon.stories.tsx +88 -88
- package/src/components/Icon/Icon.styles.ts +10 -10
- package/src/components/Icon/Icon.tsx +34 -34
- package/src/components/Icon/IconBoilerplate.tsx +42 -42
- package/src/components/Icon/complexIcons/augment.d.ts +1 -1
- package/src/components/Icon/complexIcons/avatarGreen.svg +57 -57
- package/src/components/Icon/complexIcons/icons.ts +7 -7
- package/src/components/Icon/complexIcons/index.ts +1 -1
- package/src/components/Icon/icons/icons.ts +838 -838
- package/src/components/Icon/icons/index.ts +1 -1
- package/src/components/Icon/index.ts +4 -4
- package/src/components/IncrementInput/ChangeButton.tsx +34 -34
- package/src/components/IncrementInput/IncrementInput.stories.tsx +34 -34
- package/src/components/IncrementInput/IncrementInput.styles.ts +77 -77
- package/src/components/IncrementInput/IncrementInput.tsx +95 -95
- package/src/components/IncrementInput/index.ts +2 -2
- package/src/components/Input/Input.stories.tsx +92 -92
- package/src/components/Input/Input.styles.ts +305 -305
- package/src/components/Input/Input.tsx +318 -310
- package/src/components/Input/index.ts +2 -2
- package/src/components/List/List.stories.tsx +62 -62
- package/src/components/List/List.styles.ts +52 -52
- package/src/components/List/List.tsx +82 -82
- package/src/components/List/index.ts +2 -2
- package/src/components/Modal/Modal.stories.tsx +113 -113
- package/src/components/Modal/Modal.styles.ts +308 -308
- package/src/components/Modal/Modal.tsx +210 -210
- package/src/components/Modal/index.ts +2 -2
- package/src/components/MoreMenu/MoreMenu.stories.tsx +46 -46
- package/src/components/MoreMenu/MoreMenu.styles.ts +70 -70
- package/src/components/MoreMenu/MoreMenu.tsx +102 -102
- package/src/components/MoreMenu/index.ts +2 -2
- package/src/components/MultiSelect/MultiSelect.stories.tsx +46 -46
- package/src/components/MultiSelect/MultiSelect.styles.ts +55 -55
- package/src/components/MultiSelect/MultiSelect.tsx +98 -98
- package/src/components/MultiSelect/MultiSelectInput/MultiSelectInput.styles.ts +73 -73
- package/src/components/MultiSelect/MultiSelectInput/MultiSelectInput.tsx +62 -62
- package/src/components/MultiSelect/MultiSelectInput/index.ts +1 -1
- package/src/components/MultiSelect/index.ts +3 -3
- package/src/components/MultiSelectList/MultiSelectList.styles.ts +125 -125
- package/src/components/MultiSelectList/MultiSelectList.tsx +519 -519
- package/src/components/MultiSelectList/index.ts +2 -2
- package/src/components/MultiSelectList/locales.ts +37 -37
- package/src/components/Notification/Notification.stories.tsx +51 -51
- package/src/components/Notification/Notification.styles.ts +50 -50
- package/src/components/Notification/Notification.tsx +84 -84
- package/src/components/Notification/index.ts +2 -2
- package/src/components/NumberInput/NumberInput.stories.tsx +36 -36
- package/src/components/NumberInput/NumberInput.tsx +154 -154
- package/src/components/NumberInput/helpers.ts +87 -87
- package/src/components/NumberInput/index.ts +1 -1
- package/src/components/PhoneInput/PhoneInput.stories.tsx +71 -71
- package/src/components/PhoneInput/PhoneInput.styles.ts +84 -84
- package/src/components/PhoneInput/PhoneInput.tsx +223 -223
- package/src/components/PhoneInput/PhoneInputCountryList/PhoneInputCountryList.stories.tsx +21 -21
- package/src/components/PhoneInput/PhoneInputCountryList/PhoneInputCountryList.styles.ts +100 -100
- package/src/components/PhoneInput/PhoneInputCountryList/PhoneInputCountryList.tsx +171 -171
- package/src/components/PhoneInput/PhoneInputCountryList/index.ts +2 -2
- package/src/components/PhoneInput/index.ts +6 -6
- package/src/components/PhoneInput/phone-info.ts +2167 -2167
- package/src/components/PhoneInput/types.ts +16 -16
- package/src/components/RadioButton/RadioButton.stories.tsx +46 -46
- package/src/components/RadioButton/RadioButton.styles.ts +37 -37
- package/src/components/RadioButton/RadioButton.tsx +56 -56
- package/src/components/RadioButton/index.ts +2 -2
- package/src/components/ScrollIntoViewIfNeeded/ScrollIntoViewIfNeeded.ts +66 -66
- package/src/components/ScrollIntoViewIfNeeded/index.ts +1 -1
- package/src/components/SearchInput/SearchInput.stories.tsx +24 -24
- package/src/components/SearchInput/SearchInput.styles.ts +50 -50
- package/src/components/SearchInput/SearchInput.tsx +63 -63
- package/src/components/SearchInput/index.ts +2 -2
- package/src/components/Select/Select.stories.tsx +258 -258
- package/src/components/Select/Select.styles.ts +85 -85
- package/src/components/Select/Select.tsx +508 -508
- package/src/components/Select/SelectList/SelectList.styles.ts +68 -68
- package/src/components/Select/SelectList/SelectList.tsx +139 -139
- package/src/components/Select/SelectList/index.ts +1 -1
- package/src/components/Select/helpers.ts +21 -21
- package/src/components/Select/index.ts +3 -3
- package/src/components/SmartInput/SmartInput.stories.tsx +63 -63
- package/src/components/SmartInput/SmartInput.tsx +180 -180
- package/src/components/SmartInput/helpers.ts +85 -85
- package/src/components/SmartInput/index.ts +1 -1
- package/src/components/Switch/Switch.stories.tsx +40 -40
- package/src/components/Switch/Switch.styles.ts +75 -75
- package/src/components/Switch/Switch.tsx +89 -89
- package/src/components/Switch/index.ts +2 -2
- package/src/components/TextArea/TextArea.stories.tsx +35 -35
- package/src/components/TextArea/TextArea.styles.ts +153 -153
- package/src/components/TextArea/TextArea.tsx +178 -178
- package/src/components/TextArea/index.ts +2 -2
- package/src/components/TextWithInfo/TextWithInfo.stories.tsx +53 -53
- package/src/components/TextWithInfo/TextWithInfo.styles.ts +60 -60
- package/src/components/TextWithInfo/TextWithInfo.tsx +67 -67
- package/src/components/TextWithInfo/index.ts +2 -2
- package/src/components/TextWithTooltip/TextWithTooltip.stories.tsx +58 -58
- package/src/components/TextWithTooltip/TextWithTooltip.styles.ts +19 -19
- package/src/components/TextWithTooltip/TextWithTooltip.tsx +163 -163
- package/src/components/TextWithTooltip/index.ts +2 -2
- package/src/components/ThemedPreloader/ThemedPreloader.stories.tsx +41 -41
- package/src/components/ThemedPreloader/ThemedPreloader.styles.ts +21 -21
- package/src/components/ThemedPreloader/ThemedPreloader.tsx +56 -56
- package/src/components/ThemedPreloader/components/DefaultPreloader/DefaultPreloader.tsx +34 -34
- package/src/components/ThemedPreloader/components/DefaultPreloader/index.ts +1 -1
- package/src/components/ThemedPreloader/components/DotsPreloader/DotsPreloader.styles.ts +54 -54
- package/src/components/ThemedPreloader/components/DotsPreloader/DotsPreloader.tsx +18 -18
- package/src/components/ThemedPreloader/components/DotsPreloader/index.ts +2 -2
- package/src/components/ThemedPreloader/components/SvgPreloader/SvgPreloader.styles.ts +11 -11
- package/src/components/ThemedPreloader/components/SvgPreloader/SvgPreloader.tsx +32 -32
- package/src/components/ThemedPreloader/components/SvgPreloader/index.ts +2 -2
- package/src/components/ThemedPreloader/components/index.ts +2 -2
- package/src/components/ThemedPreloader/index.ts +2 -2
- package/src/components/Toaster/Toaster.stories.tsx +34 -34
- package/src/components/Toaster/Toaster.styles.ts +59 -59
- package/src/components/Toaster/Toaster.tsx +113 -113
- package/src/components/Toaster/index.ts +2 -2
- package/src/components/Tooltip/Tooltip.stories.tsx +21 -21
- package/src/components/Tooltip/Tooltip.styles.ts +45 -45
- package/src/components/Tooltip/Tooltip.tsx +40 -40
- package/src/components/Tooltip/index.ts +3 -3
- package/src/components/Tooltip/types.ts +1 -1
- package/src/components/index.ts +36 -36
- package/src/helpers/colors.ts +2 -2
- package/src/helpers/data-attributes.ts +18 -18
- package/src/helpers/dateHelpers/date-helpers.ts +9 -9
- package/src/helpers/index.ts +5 -5
- package/src/helpers/phone.ts +106 -106
- package/src/helpers/popper-helpers.ts +17 -17
- package/src/helpers/snippets.tsx +5 -5
- package/src/helpers/utils.ts +178 -178
- package/src/hooks/index.ts +6 -6
- package/src/hooks/use-did-mount-effect.ts +21 -21
- package/src/hooks/use-dropdown.ts +85 -85
- package/src/hooks/use-is-mounted.ts +15 -15
- package/src/hooks/use-on-click-outside.ts +92 -92
- package/src/hooks/use-theme.ts +36 -36
- package/src/hooks/use-tweak-styles.ts +14 -14
- package/src/index.ts +6 -6
- package/src/theme.ts +155 -155
- package/src/types.ts +106 -106
- package/src/vite-env.d.ts +1 -1
|
@@ -1,508 +1,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 (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 (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
|
+
}
|