@true-engineering/true-react-common-ui-kit 3.8.1 → 3.9.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 (122) hide show
  1. package/LICENSE +201 -201
  2. package/README.md +10 -0
  3. package/dist/components/Checkbox/Checkbox.d.ts +2 -2
  4. package/dist/components/NewMoreMenu/NewMoreMenu.styles.d.ts +3 -1
  5. package/dist/components/ScrollIntoViewIfNeeded/ScrollIntoViewIfNeeded.d.ts +1 -1
  6. package/dist/components/Select/Select.d.ts +4 -4
  7. package/dist/components/Select/components/SelectList/SelectList.d.ts +5 -6
  8. package/dist/components/Select/components/SelectListItem/SelectListItem.d.ts +2 -2
  9. package/dist/components/WithPopup/WithPopup.styles.d.ts +1 -1
  10. package/dist/true-react-common-ui-kit.js +111 -86
  11. package/dist/true-react-common-ui-kit.js.map +1 -1
  12. package/dist/true-react-common-ui-kit.umd.cjs +111 -86
  13. package/dist/true-react-common-ui-kit.umd.cjs.map +1 -1
  14. package/package.json +1 -1
  15. package/src/components/AccountInfo/AccountInfo.stories.tsx +32 -32
  16. package/src/components/AccountInfo/AccountInfo.tsx +80 -80
  17. package/src/components/AddButton/AddButton.stories.tsx +21 -21
  18. package/src/components/AddButton/AddButton.tsx +52 -52
  19. package/src/components/Button/Button.stories.tsx +56 -56
  20. package/src/components/Button/Button.tsx +129 -129
  21. package/src/components/Checkbox/Checkbox.stories.tsx +28 -28
  22. package/src/components/Checkbox/Checkbox.tsx +7 -4
  23. package/src/components/CloseButton/CloseButton.tsx +34 -34
  24. package/src/components/Colors/Colors.stories.tsx +7 -7
  25. package/src/components/DateInput/DateInput.tsx +90 -90
  26. package/src/components/DateInput/constants.ts +2 -2
  27. package/src/components/DatePicker/DatePicker.tsx +308 -308
  28. package/src/components/Description/Description.stories.tsx +27 -27
  29. package/src/components/Description/Description.tsx +61 -61
  30. package/src/components/FiltersPane/FiltersPane.tsx +158 -158
  31. package/src/components/FiltersPane/components/Filter/Filter.tsx +203 -203
  32. package/src/components/FiltersPane/components/FilterValueView/FilterValueView.tsx +166 -166
  33. package/src/components/FiltersPane/components/FilterWithDates/FilterWithDates.tsx +210 -210
  34. package/src/components/FiltersPane/components/FilterWithPeriod/FilterWithPeriod.tsx +177 -177
  35. package/src/components/FiltersPane/components/FilterWrapper/FilterWrapper.tsx +167 -167
  36. package/src/components/Flag/Flag.stories.tsx +29 -29
  37. package/src/components/Flag/Flag.tsx +26 -26
  38. package/src/components/Flag/augment.d.ts +1 -1
  39. package/src/components/FlexibleTable/FlexibleTable.stories.tsx +267 -267
  40. package/src/components/FlexibleTable/FlexibleTable.styles.ts +110 -110
  41. package/src/components/FlexibleTable/FlexibleTable.tsx +271 -271
  42. package/src/components/FlexibleTable/components/FlexibleTableCell/FlexibleTableCell.styles.ts +38 -38
  43. package/src/components/FlexibleTable/components/FlexibleTableCell/FlexibleTableCell.tsx +83 -83
  44. package/src/components/FlexibleTable/components/FlexibleTableRow/FlexibleTableRow.styles.ts +25 -25
  45. package/src/components/FlexibleTable/components/FlexibleTableRow/FlexibleTableRow.tsx +196 -196
  46. package/src/components/FlexibleTable/helpers.ts +13 -13
  47. package/src/components/FlexibleTable/types.ts +52 -52
  48. package/src/components/Icon/Icon.stories.tsx +86 -86
  49. package/src/components/Icon/complexIcons/augment.d.ts +1 -1
  50. package/src/components/Icon/complexIcons/avatarGreen.svg +57 -57
  51. package/src/components/Icon/complexIcons/index.ts +1 -1
  52. package/src/components/IncrementInput/IncrementInput.tsx +105 -105
  53. package/src/components/Input/Input.tsx +297 -297
  54. package/src/components/Input/types.ts +32 -32
  55. package/src/components/List/List.stories.tsx +70 -70
  56. package/src/components/List/List.tsx +33 -33
  57. package/src/components/List/components/ListItem/ListItem.tsx +57 -57
  58. package/src/components/Modal/Modal.stories.tsx +105 -105
  59. package/src/components/Modal/Modal.tsx +196 -196
  60. package/src/components/MoreMenu/MoreMenu.styles.ts +68 -68
  61. package/src/components/MultiSelect/MultiSelect.stories.tsx +46 -46
  62. package/src/components/MultiSelect/MultiSelect.tsx +106 -106
  63. package/src/components/MultiSelect/components/MultiSelectInput/MultiSelectInput.tsx +53 -53
  64. package/src/components/MultiSelectList/MultiSelectList.tsx +461 -461
  65. package/src/components/NewMoreMenu/NewMoreMenu.styles.ts +5 -5
  66. package/src/components/NewMoreMenu/NewMoreMenu.tsx +15 -1
  67. package/src/components/Notification/Notification.stories.tsx +46 -46
  68. package/src/components/Notification/Notification.tsx +69 -69
  69. package/src/components/NumberInput/NumberInput.tsx +137 -137
  70. package/src/components/NumberInput/index.ts +1 -1
  71. package/src/components/PhoneInput/PhoneInput.tsx +214 -214
  72. package/src/components/PhoneInput/components/PhoneInputCountryList/PhoneInputCountryList.tsx +155 -155
  73. package/src/components/PhoneInput/types.ts +16 -16
  74. package/src/components/RadioButton/RadioButton.stories.tsx +46 -46
  75. package/src/components/RadioButton/RadioButton.tsx +57 -57
  76. package/src/components/ScrollIntoViewIfNeeded/index.ts +1 -1
  77. package/src/components/Select/CustomSelect.stories.tsx +217 -217
  78. package/src/components/Select/MultiSelect.stories.tsx +240 -240
  79. package/src/components/Select/Select.stories.tsx +235 -235
  80. package/src/components/Select/Select.tsx +57 -28
  81. package/src/components/Select/components/SelectList/SelectList.tsx +8 -9
  82. package/src/components/Select/components/SelectListItem/SelectListItem.tsx +7 -3
  83. package/src/components/Select/constants.ts +2 -2
  84. package/src/components/Select/types.ts +1 -1
  85. package/src/components/Selector/Selector.stories.tsx +62 -62
  86. package/src/components/Selector/Selector.styles.ts +164 -164
  87. package/src/components/Selector/Selector.tsx +115 -115
  88. package/src/components/Selector/index.ts +2 -2
  89. package/src/components/Selector/types.ts +12 -12
  90. package/src/components/Skeleton/Skeleton.stories.tsx +19 -19
  91. package/src/components/SmartInput/SmartInput.tsx +134 -134
  92. package/src/components/Status/Status.stories.tsx +73 -73
  93. package/src/components/Status/Status.styles.ts +143 -143
  94. package/src/components/Status/Status.tsx +49 -49
  95. package/src/components/Status/constants.ts +11 -11
  96. package/src/components/Status/index.ts +3 -3
  97. package/src/components/Status/types.ts +5 -5
  98. package/src/components/Switch/Switch.stories.tsx +40 -40
  99. package/src/components/Switch/Switch.tsx +75 -75
  100. package/src/components/TextArea/TextArea.tsx +180 -180
  101. package/src/components/TextButton/TextButton.stories.tsx +46 -46
  102. package/src/components/TextButton/TextButton.styles.ts +129 -129
  103. package/src/components/TextButton/TextButton.tsx +103 -103
  104. package/src/components/TextButton/index.ts +4 -4
  105. package/src/components/TextWithInfo/TextWithInfo.stories.tsx +53 -53
  106. package/src/components/TextWithInfo/TextWithInfo.tsx +62 -62
  107. package/src/components/TextWithTooltip/TextWithTooltip.stories.tsx +58 -58
  108. package/src/components/ThemedPreloader/ThemedPreloader.stories.tsx +41 -41
  109. package/src/components/ThemedPreloader/ThemedPreloader.tsx +54 -54
  110. package/src/components/ThemedPreloader/components/DefaultPreloader/index.ts +1 -1
  111. package/src/components/Toaster/Toaster.stories.tsx +30 -30
  112. package/src/components/Toaster/Toaster.tsx +108 -108
  113. package/src/components/Tooltip/Tooltip.stories.tsx +19 -19
  114. package/src/components/Tooltip/Tooltip.tsx +35 -35
  115. package/src/components/Tooltip/types.ts +1 -1
  116. package/src/components/WithPopup/WithPopup.styles.ts +4 -0
  117. package/src/components/WithPopup/WithPopup.tsx +6 -1
  118. package/src/helpers/popper-helpers.ts +17 -17
  119. package/src/hooks/use-dropdown.ts +84 -84
  120. package/src/hooks/use-is-mounted.ts +15 -15
  121. package/src/theme/helpers.ts +76 -76
  122. package/src/vite-env.d.ts +1 -1
@@ -1,115 +1,115 @@
1
- import { useEffect, useRef, useState } from 'react';
2
- import clsx from 'clsx';
3
- import {
4
- addDataTestId,
5
- getTestId,
6
- hasDuplicates,
7
- isNotEmpty,
8
- isReactNodeNotEmpty,
9
- } from '@true-engineering/true-react-platform-helpers';
10
- import { addDataAttributes } from '../../helpers';
11
- import { ICommonProps } from '../../types';
12
- import { renderIcon } from '../Icon';
13
- import { ISelectorOption, ISelectorValue } from './types';
14
- import { getSelectorLineStyle, useStyles, ISelectorStyles } from './Selector.styles';
15
-
16
- export interface ISelectorProps<V extends ISelectorValue> extends ICommonProps<ISelectorStyles> {
17
- options: Array<ISelectorOption<V>>;
18
- value?: V;
19
- /** @default false */
20
- isDisabled?: boolean;
21
- /** @default false */
22
- isRequired?: boolean;
23
- /** @default false */
24
- isInvalid?: boolean;
25
- /** @default true */
26
- hasSameOptionsWidth?: boolean;
27
- /** @default 'left' */
28
- iconPosition?: 'left' | 'right';
29
- /** @default 'l' */
30
- size?: 's' | 'm' | 'l';
31
- onChange: (value: V) => void;
32
- }
33
-
34
- export function Selector<V extends ISelectorValue>({
35
- options,
36
- value,
37
- isDisabled = false,
38
- isRequired = false,
39
- isInvalid = false,
40
- hasSameOptionsWidth = true,
41
- iconPosition = 'left',
42
- size = 'l',
43
- testId,
44
- data,
45
- tweakStyles,
46
- onChange,
47
- }: ISelectorProps<V>): JSX.Element {
48
- const classes = useStyles({ theme: tweakStyles });
49
- const optionsValues = options.map((opt) => opt.value);
50
-
51
- const [elementsData, setElementsData] = useState<HTMLElement[]>([]);
52
- const listRef = useRef<HTMLDivElement>(null);
53
-
54
- if (hasDuplicates(optionsValues)) {
55
- // eslint-disable-next-line no-console
56
- console.error('Selector: Значения options.value должны быть уникальными');
57
- }
58
-
59
- useEffect(() => {
60
- const listEl = listRef.current;
61
- if (listEl === null) {
62
- return;
63
- }
64
-
65
- setElementsData([...listEl.querySelectorAll<HTMLElement>(`.${classes.optionWrapper}`)]);
66
- }, [options, size]);
67
-
68
- const activeElementData = isNotEmpty(value)
69
- ? elementsData[optionsValues.indexOf(value)]
70
- : undefined;
71
-
72
- return (
73
- <div
74
- className={clsx(classes.root, classes.selector, {
75
- [classes.iconFromRight]: iconPosition === 'right',
76
- [classes.invalid]: isInvalid,
77
- [classes.required]: isRequired,
78
- [classes.autoWidth]: hasSameOptionsWidth,
79
- })}
80
- ref={listRef}
81
- {...addDataTestId(testId)}
82
- {...addDataAttributes(data)}
83
- >
84
- {isNotEmpty(activeElementData) && (
85
- <div className={classes.line} style={getSelectorLineStyle(activeElementData)} />
86
- )}
87
-
88
- {options.map((option) => {
89
- const optionId = option.value.toString();
90
- const isDisabledOption = option.isDisabled ?? isDisabled;
91
- const isActiveOption = option.value === value;
92
-
93
- return (
94
- <div key={optionId} className={classes.optionWrapper} {...addDataTestId(testId)}>
95
- <button
96
- type="button"
97
- className={clsx(classes.option, classes[size], {
98
- [classes.active]: isActiveOption,
99
- [classes.disabled]: isDisabledOption,
100
- })}
101
- disabled={isDisabledOption}
102
- onClick={!isDisabledOption ? () => onChange(option.value) : undefined}
103
- {...addDataTestId(getTestId(testId, optionId))}
104
- >
105
- {isReactNodeNotEmpty(option.icon) && (
106
- <div className={classes.optionIcon}>{renderIcon(option.icon)}</div>
107
- )}
108
- <div className={classes.optionText}>{option.label}</div>
109
- </button>
110
- </div>
111
- );
112
- })}
113
- </div>
114
- );
115
- }
1
+ import { useEffect, useRef, useState } from 'react';
2
+ import clsx from 'clsx';
3
+ import {
4
+ addDataTestId,
5
+ getTestId,
6
+ hasDuplicates,
7
+ isNotEmpty,
8
+ isReactNodeNotEmpty,
9
+ } from '@true-engineering/true-react-platform-helpers';
10
+ import { addDataAttributes } from '../../helpers';
11
+ import { ICommonProps } from '../../types';
12
+ import { renderIcon } from '../Icon';
13
+ import { ISelectorOption, ISelectorValue } from './types';
14
+ import { getSelectorLineStyle, useStyles, ISelectorStyles } from './Selector.styles';
15
+
16
+ export interface ISelectorProps<V extends ISelectorValue> extends ICommonProps<ISelectorStyles> {
17
+ options: Array<ISelectorOption<V>>;
18
+ value?: V;
19
+ /** @default false */
20
+ isDisabled?: boolean;
21
+ /** @default false */
22
+ isRequired?: boolean;
23
+ /** @default false */
24
+ isInvalid?: boolean;
25
+ /** @default true */
26
+ hasSameOptionsWidth?: boolean;
27
+ /** @default 'left' */
28
+ iconPosition?: 'left' | 'right';
29
+ /** @default 'l' */
30
+ size?: 's' | 'm' | 'l';
31
+ onChange: (value: V) => void;
32
+ }
33
+
34
+ export function Selector<V extends ISelectorValue>({
35
+ options,
36
+ value,
37
+ isDisabled = false,
38
+ isRequired = false,
39
+ isInvalid = false,
40
+ hasSameOptionsWidth = true,
41
+ iconPosition = 'left',
42
+ size = 'l',
43
+ testId,
44
+ data,
45
+ tweakStyles,
46
+ onChange,
47
+ }: ISelectorProps<V>): JSX.Element {
48
+ const classes = useStyles({ theme: tweakStyles });
49
+ const optionsValues = options.map((opt) => opt.value);
50
+
51
+ const [elementsData, setElementsData] = useState<HTMLElement[]>([]);
52
+ const listRef = useRef<HTMLDivElement>(null);
53
+
54
+ if (hasDuplicates(optionsValues)) {
55
+ // eslint-disable-next-line no-console
56
+ console.error('Selector: Значения options.value должны быть уникальными');
57
+ }
58
+
59
+ useEffect(() => {
60
+ const listEl = listRef.current;
61
+ if (listEl === null) {
62
+ return;
63
+ }
64
+
65
+ setElementsData([...listEl.querySelectorAll<HTMLElement>(`.${classes.optionWrapper}`)]);
66
+ }, [options, size]);
67
+
68
+ const activeElementData = isNotEmpty(value)
69
+ ? elementsData[optionsValues.indexOf(value)]
70
+ : undefined;
71
+
72
+ return (
73
+ <div
74
+ className={clsx(classes.root, classes.selector, {
75
+ [classes.iconFromRight]: iconPosition === 'right',
76
+ [classes.invalid]: isInvalid,
77
+ [classes.required]: isRequired,
78
+ [classes.autoWidth]: hasSameOptionsWidth,
79
+ })}
80
+ ref={listRef}
81
+ {...addDataTestId(testId)}
82
+ {...addDataAttributes(data)}
83
+ >
84
+ {isNotEmpty(activeElementData) && (
85
+ <div className={classes.line} style={getSelectorLineStyle(activeElementData)} />
86
+ )}
87
+
88
+ {options.map((option) => {
89
+ const optionId = option.value.toString();
90
+ const isDisabledOption = option.isDisabled ?? isDisabled;
91
+ const isActiveOption = option.value === value;
92
+
93
+ return (
94
+ <div key={optionId} className={classes.optionWrapper} {...addDataTestId(testId)}>
95
+ <button
96
+ type="button"
97
+ className={clsx(classes.option, classes[size], {
98
+ [classes.active]: isActiveOption,
99
+ [classes.disabled]: isDisabledOption,
100
+ })}
101
+ disabled={isDisabledOption}
102
+ onClick={!isDisabledOption ? () => onChange(option.value) : undefined}
103
+ {...addDataTestId(getTestId(testId, optionId))}
104
+ >
105
+ {isReactNodeNotEmpty(option.icon) && (
106
+ <div className={classes.optionIcon}>{renderIcon(option.icon)}</div>
107
+ )}
108
+ <div className={classes.optionText}>{option.label}</div>
109
+ </button>
110
+ </div>
111
+ );
112
+ })}
113
+ </div>
114
+ );
115
+ }
@@ -1,2 +1,2 @@
1
- export * from './Selector';
2
- export type { ISelectorStyles } from './Selector.styles';
1
+ export * from './Selector';
2
+ export type { ISelectorStyles } from './Selector.styles';
@@ -1,12 +1,12 @@
1
- import { ReactNode } from 'react';
2
- import { IIcon } from '../Icon';
3
-
4
- export type ISelectorValue = string | number | boolean;
5
-
6
- export interface ISelectorOption<V> {
7
- label: ReactNode;
8
- icon?: IIcon;
9
- value: V;
10
- /** @default false */
11
- isDisabled?: boolean;
12
- }
1
+ import { ReactNode } from 'react';
2
+ import { IIcon } from '../Icon';
3
+
4
+ export type ISelectorValue = string | number | boolean;
5
+
6
+ export interface ISelectorOption<V> {
7
+ label: ReactNode;
8
+ icon?: IIcon;
9
+ value: V;
10
+ /** @default false */
11
+ isDisabled?: boolean;
12
+ }
@@ -1,19 +1,19 @@
1
- import { ComponentStory } from '@storybook/react';
2
- import { Skeleton } from './Skeleton';
3
-
4
- export default {
5
- title: 'Feedback/Skeleton',
6
- component: Skeleton,
7
- };
8
-
9
- const Template: ComponentStory<typeof Skeleton> = (args) => (
10
- <div style={{ padding: 32, backgroundColor: '#fff', width: 200 }}>
11
- <Skeleton {...args} />
12
- </div>
13
- );
14
-
15
- export const Default = Template.bind({});
16
-
17
- Default.args = {
18
- height: '20px',
19
- };
1
+ import { ComponentStory } from '@storybook/react';
2
+ import { Skeleton } from './Skeleton';
3
+
4
+ export default {
5
+ title: 'Feedback/Skeleton',
6
+ component: Skeleton,
7
+ };
8
+
9
+ const Template: ComponentStory<typeof Skeleton> = (args) => (
10
+ <div style={{ padding: 32, backgroundColor: '#fff', width: 200 }}>
11
+ <Skeleton {...args} />
12
+ </div>
13
+ );
14
+
15
+ export const Default = Template.bind({});
16
+
17
+ Default.args = {
18
+ height: '20px',
19
+ };
@@ -1,134 +1,134 @@
1
- import { useState, useEffect, forwardRef, FormEvent, ClipboardEvent } from 'react';
2
- import { Input, IInputProps } from '../Input';
3
- import { CharactersMap, SMART_INPUT_REGEX_MAP, TransliterationMap } from './constants';
4
- import { transformCaseSensitive } from './helpers';
5
- import { ISmartType } from './types';
6
-
7
- export interface ISmartInputProps extends IInputProps {
8
- /** @default false */
9
- isUpperCase?: boolean;
10
- /** @default 'default' */
11
- smartType?: ISmartType;
12
- regExp?: RegExp;
13
- onChange: (value: string) => void;
14
- }
15
-
16
- export const SmartInput = forwardRef<HTMLInputElement, ISmartInputProps>(
17
- (
18
- {
19
- onChange,
20
- isUpperCase = false,
21
- smartType = 'default',
22
- regExp,
23
- value = '',
24
- maxLength,
25
- ...rest
26
- },
27
- ref,
28
- ) => {
29
- const [currentValue, setCurrentValue] = useState<string>(getUpperCaseIfNeeded(value));
30
- const [caretPosition, setCaretPosition] = useState<number | null>(null);
31
- const [input, setInput] = useState<HTMLInputElement | null>(null);
32
- const regex = regExp || SMART_INPUT_REGEX_MAP[smartType];
33
-
34
- useEffect(() => {
35
- if (input && input.type !== 'email' && input.selectionStart !== caretPosition) {
36
- input.selectionStart = caretPosition;
37
- input.selectionEnd = caretPosition;
38
- }
39
- }, [caretPosition]);
40
-
41
- useEffect(() => {
42
- setCurrentValue(getUpperCaseIfNeeded(value));
43
- }, [value]);
44
-
45
- function getUpperCaseIfNeeded(str: string) {
46
- return isUpperCase ? str.toUpperCase() : str;
47
- }
48
-
49
- const handleChange = (str: string, event?: FormEvent<HTMLInputElement>) => {
50
- const mappedValue = str
51
- .split('')
52
- .map((symbol) =>
53
- regex.test(symbol)
54
- ? symbol
55
- : transformCaseSensitive(
56
- smartType,
57
- smartType !== 'email' ? CharactersMap : { ...CharactersMap, '"': '@' },
58
- symbol,
59
- ),
60
- )
61
- .filter((symbol) => regex.test(symbol))
62
- .join('');
63
- const domElement = event?.currentTarget;
64
-
65
- if (domElement) {
66
- if (!input) {
67
- setInput(domElement);
68
- }
69
-
70
- setCurrentValue(getUpperCaseIfNeeded(mappedValue));
71
- onChange(getUpperCaseIfNeeded(mappedValue));
72
-
73
- if (mappedValue !== currentValue) {
74
- setCaretPosition(domElement.selectionStart);
75
- } else {
76
- setCaretPosition(domElement.selectionStart ? domElement.selectionStart - 1 : null);
77
- }
78
- }
79
- };
80
-
81
- const handlePaste = (event: ClipboardEvent<HTMLInputElement>) => {
82
- const str = event.clipboardData.getData('text/plain').split('').join('');
83
- const domElement = event.currentTarget;
84
-
85
- if (!input) {
86
- setInput(domElement);
87
- }
88
-
89
- event.preventDefault();
90
- const selectionStart = domElement.selectionStart ?? 0;
91
- const selectionEnd = domElement.selectionEnd ?? 0;
92
-
93
- let mappedValue = str
94
- .split('')
95
- .map((symbol) =>
96
- regex.test(symbol)
97
- ? symbol
98
- : transformCaseSensitive(smartType, TransliterationMap, symbol),
99
- )
100
- .filter((letter) => regex.test(letter))
101
- .join('');
102
-
103
- const newValueLength =
104
- mappedValue.length + currentValue.length - (selectionEnd - selectionStart);
105
-
106
- if (maxLength !== undefined && maxLength >= 0 && newValueLength > maxLength) {
107
- const validMappedValueLength = mappedValue.length - (newValueLength - maxLength);
108
-
109
- mappedValue = mappedValue.substring(0, validMappedValueLength);
110
- }
111
-
112
- const newValue = getUpperCaseIfNeeded(
113
- `${currentValue?.substring(0, selectionStart)}${mappedValue}${currentValue?.substring(
114
- selectionEnd,
115
- )}`,
116
- );
117
-
118
- setCaretPosition(selectionStart + mappedValue.length);
119
- setCurrentValue(newValue);
120
- onChange(newValue);
121
- };
122
-
123
- return (
124
- <Input
125
- {...rest}
126
- ref={ref}
127
- maxLength={maxLength}
128
- onChange={handleChange}
129
- onPaste={handlePaste}
130
- value={currentValue}
131
- />
132
- );
133
- },
134
- );
1
+ import { useState, useEffect, forwardRef, FormEvent, ClipboardEvent } from 'react';
2
+ import { Input, IInputProps } from '../Input';
3
+ import { CharactersMap, SMART_INPUT_REGEX_MAP, TransliterationMap } from './constants';
4
+ import { transformCaseSensitive } from './helpers';
5
+ import { ISmartType } from './types';
6
+
7
+ export interface ISmartInputProps extends IInputProps {
8
+ /** @default false */
9
+ isUpperCase?: boolean;
10
+ /** @default 'default' */
11
+ smartType?: ISmartType;
12
+ regExp?: RegExp;
13
+ onChange: (value: string) => void;
14
+ }
15
+
16
+ export const SmartInput = forwardRef<HTMLInputElement, ISmartInputProps>(
17
+ (
18
+ {
19
+ onChange,
20
+ isUpperCase = false,
21
+ smartType = 'default',
22
+ regExp,
23
+ value = '',
24
+ maxLength,
25
+ ...rest
26
+ },
27
+ ref,
28
+ ) => {
29
+ const [currentValue, setCurrentValue] = useState<string>(getUpperCaseIfNeeded(value));
30
+ const [caretPosition, setCaretPosition] = useState<number | null>(null);
31
+ const [input, setInput] = useState<HTMLInputElement | null>(null);
32
+ const regex = regExp || SMART_INPUT_REGEX_MAP[smartType];
33
+
34
+ useEffect(() => {
35
+ if (input && input.type !== 'email' && input.selectionStart !== caretPosition) {
36
+ input.selectionStart = caretPosition;
37
+ input.selectionEnd = caretPosition;
38
+ }
39
+ }, [caretPosition]);
40
+
41
+ useEffect(() => {
42
+ setCurrentValue(getUpperCaseIfNeeded(value));
43
+ }, [value]);
44
+
45
+ function getUpperCaseIfNeeded(str: string) {
46
+ return isUpperCase ? str.toUpperCase() : str;
47
+ }
48
+
49
+ const handleChange = (str: string, event?: FormEvent<HTMLInputElement>) => {
50
+ const mappedValue = str
51
+ .split('')
52
+ .map((symbol) =>
53
+ regex.test(symbol)
54
+ ? symbol
55
+ : transformCaseSensitive(
56
+ smartType,
57
+ smartType !== 'email' ? CharactersMap : { ...CharactersMap, '"': '@' },
58
+ symbol,
59
+ ),
60
+ )
61
+ .filter((symbol) => regex.test(symbol))
62
+ .join('');
63
+ const domElement = event?.currentTarget;
64
+
65
+ if (domElement) {
66
+ if (!input) {
67
+ setInput(domElement);
68
+ }
69
+
70
+ setCurrentValue(getUpperCaseIfNeeded(mappedValue));
71
+ onChange(getUpperCaseIfNeeded(mappedValue));
72
+
73
+ if (mappedValue !== currentValue) {
74
+ setCaretPosition(domElement.selectionStart);
75
+ } else {
76
+ setCaretPosition(domElement.selectionStart ? domElement.selectionStart - 1 : null);
77
+ }
78
+ }
79
+ };
80
+
81
+ const handlePaste = (event: ClipboardEvent<HTMLInputElement>) => {
82
+ const str = event.clipboardData.getData('text/plain').split('').join('');
83
+ const domElement = event.currentTarget;
84
+
85
+ if (!input) {
86
+ setInput(domElement);
87
+ }
88
+
89
+ event.preventDefault();
90
+ const selectionStart = domElement.selectionStart ?? 0;
91
+ const selectionEnd = domElement.selectionEnd ?? 0;
92
+
93
+ let mappedValue = str
94
+ .split('')
95
+ .map((symbol) =>
96
+ regex.test(symbol)
97
+ ? symbol
98
+ : transformCaseSensitive(smartType, TransliterationMap, symbol),
99
+ )
100
+ .filter((letter) => regex.test(letter))
101
+ .join('');
102
+
103
+ const newValueLength =
104
+ mappedValue.length + currentValue.length - (selectionEnd - selectionStart);
105
+
106
+ if (maxLength !== undefined && maxLength >= 0 && newValueLength > maxLength) {
107
+ const validMappedValueLength = mappedValue.length - (newValueLength - maxLength);
108
+
109
+ mappedValue = mappedValue.substring(0, validMappedValueLength);
110
+ }
111
+
112
+ const newValue = getUpperCaseIfNeeded(
113
+ `${currentValue?.substring(0, selectionStart)}${mappedValue}${currentValue?.substring(
114
+ selectionEnd,
115
+ )}`,
116
+ );
117
+
118
+ setCaretPosition(selectionStart + mappedValue.length);
119
+ setCurrentValue(newValue);
120
+ onChange(newValue);
121
+ };
122
+
123
+ return (
124
+ <Input
125
+ {...rest}
126
+ ref={ref}
127
+ maxLength={maxLength}
128
+ onChange={handleChange}
129
+ onPaste={handlePaste}
130
+ value={currentValue}
131
+ />
132
+ );
133
+ },
134
+ );