@true-engineering/true-react-common-ui-kit 3.40.0 → 3.41.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.
@@ -1,134 +1,116 @@
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 { ClipboardEvent, FormEvent, forwardRef, useRef } from 'react';
2
+ import { isEmpty, isNotEmpty } from '@true-engineering/true-react-platform-helpers';
3
+ import { useMergedRefs } from '../../hooks';
4
+ import { IInputProps, Input } from '../Input';
5
+ import {
6
+ CharactersMap,
7
+ EmailCharactersMap,
8
+ SMART_INPUT_REGEX_MAP,
9
+ TransliterationMap,
10
+ } from './constants';
11
+ import { mapSymbols } from './helpers';
12
+ import { IInputRawValue, ISmartType } from './types';
13
+
14
+ export interface ISmartInputProps extends Omit<IInputProps, 'onChange'> {
15
+ /** @default 'default' */
16
+ smartType?: ISmartType;
17
+ regExp?: RegExp;
18
+ /** @default false */
19
+ isUpperCase?: boolean;
20
+ /** @default true */
21
+ isTransliterationEnabled?: boolean;
22
+ onChange: (
23
+ value: string,
24
+ rawValue: IInputRawValue,
25
+ event?: FormEvent<HTMLInputElement> | ClipboardEvent<HTMLInputElement>,
26
+ ) => void;
27
+ }
28
+
29
+ export const SmartInput = forwardRef<HTMLInputElement, ISmartInputProps>(
30
+ (
31
+ {
32
+ value = '',
33
+ smartType = 'default',
34
+ regExp,
35
+ maxLength,
36
+ isUpperCase = false,
37
+ isTransliterationEnabled = true,
38
+ onChange,
39
+ ...rest
40
+ },
41
+ ref,
42
+ ) => {
43
+ const inputRef = useRef<HTMLInputElement>(null);
44
+ const mergedRef = useMergedRefs([ref, inputRef]);
45
+
46
+ const regex = regExp || SMART_INPUT_REGEX_MAP[smartType];
47
+
48
+ const getUpperCaseIfNeeded = (str: string) => (isUpperCase ? str.toUpperCase() : str);
49
+
50
+ const updateCaretPosition = (position: number | null, newValue: string) => {
51
+ const input = inputRef.current;
52
+ if (isNotEmpty(input) && input.type !== 'email') {
53
+ // Нужно для того, чтобы после ререндера позиция каретки не сбросилась
54
+ input.value = newValue;
55
+ input.setSelectionRange(position, position);
56
+ }
57
+ };
58
+
59
+ const handleChange = async (str: string, event: FormEvent<HTMLInputElement>) => {
60
+ const isValid = regex.test(str);
61
+
62
+ const charactersMap = smartType === 'email' ? EmailCharactersMap : CharactersMap;
63
+ const newValue = getUpperCaseIfNeeded(
64
+ isValid ? str : mapSymbols(str, regex, charactersMap, isTransliterationEnabled),
65
+ );
66
+
67
+ onChange(newValue, { rawValue: str, isValid }, event);
68
+
69
+ const input = inputRef.current;
70
+ if (isNotEmpty(input)) {
71
+ const start = input.selectionStart;
72
+ updateCaretPosition(isEmpty(start) || newValue !== value ? start : start - 1, newValue);
73
+ }
74
+ };
75
+
76
+ const handlePaste = async (event: ClipboardEvent<HTMLInputElement>) => {
77
+ event.preventDefault();
78
+
79
+ const str = event.clipboardData.getData('text/plain').split('').join('');
80
+
81
+ const input = event.currentTarget;
82
+ const selectionStart = input.selectionStart ?? 0;
83
+ const selectionEnd = input.selectionEnd ?? 0;
84
+
85
+ const isValid = regex.test(str);
86
+
87
+ let mappedValue = isValid
88
+ ? str
89
+ : mapSymbols(str, regex, TransliterationMap, isTransliterationEnabled);
90
+
91
+ const newValueLength = mappedValue.length + value.length - (selectionEnd - selectionStart);
92
+
93
+ if (isNotEmpty(maxLength) && maxLength >= 0 && newValueLength > maxLength) {
94
+ mappedValue = mappedValue.substring(0, mappedValue.length - (newValueLength - maxLength));
95
+ }
96
+
97
+ const newValue = getUpperCaseIfNeeded(
98
+ `${value.substring(0, selectionStart)}${mappedValue}${value.substring(selectionEnd)}`,
99
+ );
100
+
101
+ onChange(newValue, { rawValue: str, isValid }, event);
102
+ updateCaretPosition(selectionStart + mappedValue.length, newValue);
103
+ };
104
+
105
+ return (
106
+ <Input
107
+ {...rest}
108
+ ref={mergedRef}
109
+ value={value}
110
+ maxLength={maxLength}
111
+ onPaste={handlePaste}
112
+ onChange={handleChange}
113
+ />
114
+ );
115
+ },
116
+ );
@@ -1,84 +1,91 @@
1
- export const SMART_INPUT_REGEX_MAP = {
2
- default: /./i,
3
- agencyName: /^[a-zA-Z\s0-9-]*$/i,
4
- surname: /^[a-zA-Z\s-]*$/i,
5
- name: /^[a-zA-Z\s-.`']*$/i,
6
- surnameRuEn: /^[a-zA-Zа-яА-Я\s-]*$/i,
7
- nameRuEn: /^[a-zA-Zа-яА-Я\s-.`']*$/i,
8
- email: /^[a-zA-Z0-9@_\-.+']*$/i,
9
- digits: /^[0-9]*$/i,
10
- docNumber: /^[a-zA-Z0-9]*$/i,
11
- benefitCert: /^[a-zA-Z0-9/]*$/i,
12
- };
13
-
14
- export const CharactersMap: { [key: string]: string } = {
15
- й: 'q',
16
- ц: 'w',
17
- у: 'e',
18
- к: 'r',
19
- е: 't',
20
- н: 'y',
21
- г: 'u',
22
- ш: 'i',
23
- щ: 'o',
24
- з: 'p',
25
- х: '[',
26
- ъ: ']',
27
- ф: 'a',
28
- ы: 's',
29
- в: 'd',
30
- а: 'f',
31
- п: 'g',
32
- р: 'h',
33
- о: 'j',
34
- л: 'k',
35
- д: 'l',
36
- ж: ';',
37
- э: "'",
38
- я: 'z',
39
- ч: 'x',
40
- с: 'c',
41
- м: 'v',
42
- и: 'b',
43
- т: 'n',
44
- ь: 'm',
45
- б: ',',
46
- ю: '.',
47
- ё: '`',
48
- };
49
-
50
- export const TransliterationMap: { [key: string]: string } = {
51
- й: 'i',
52
- ц: 'ts',
53
- у: 'u',
54
- к: 'k',
55
- е: 'e',
56
- н: 'n',
57
- г: 'g',
58
- ш: 'sh',
59
- щ: 'shch',
60
- з: 'z',
61
- х: 'h',
62
- ъ: 'ie',
63
- ф: 'f',
64
- ы: 'y',
65
- в: 'v',
66
- а: 'a',
67
- п: 'p',
68
- р: 'r',
69
- о: 'o',
70
- л: 'l',
71
- д: 'd',
72
- ж: 'zh',
73
- э: 'e',
74
- я: 'ia',
75
- ч: 'ch',
76
- с: 's',
77
- м: 'm',
78
- и: 'i',
79
- т: 't',
80
- ь: '',
81
- б: 'b',
82
- ю: 'iu',
83
- ё: 'e',
84
- };
1
+ import { ICharactersMap } from './types';
2
+
3
+ export const SMART_INPUT_REGEX_MAP = {
4
+ default: /./i,
5
+ agencyName: /^[a-zA-Z\s0-9-]*$/i,
6
+ surname: /^[a-zA-Z\s-]*$/i,
7
+ name: /^[a-zA-Z\s-.`']*$/i,
8
+ surnameRuEn: /^[a-zA-Zа-яА-Я\s-]*$/i,
9
+ nameRuEn: /^[a-zA-Zа-яА-Я\s-.`']*$/i,
10
+ email: /^[a-zA-Z0-9@_\-.+']*$/i,
11
+ digits: /^[0-9]*$/i,
12
+ docNumber: /^[a-zA-Z0-9]*$/i,
13
+ benefitCert: /^[a-zA-Z0-9/]*$/i,
14
+ };
15
+
16
+ export const CharactersMap: ICharactersMap = {
17
+ й: 'q',
18
+ ц: 'w',
19
+ у: 'e',
20
+ к: 'r',
21
+ е: 't',
22
+ н: 'y',
23
+ г: 'u',
24
+ ш: 'i',
25
+ щ: 'o',
26
+ з: 'p',
27
+ х: '[',
28
+ ъ: ']',
29
+ ф: 'a',
30
+ ы: 's',
31
+ в: 'd',
32
+ а: 'f',
33
+ п: 'g',
34
+ р: 'h',
35
+ о: 'j',
36
+ л: 'k',
37
+ д: 'l',
38
+ ж: ';',
39
+ э: "'",
40
+ я: 'z',
41
+ ч: 'x',
42
+ с: 'c',
43
+ м: 'v',
44
+ и: 'b',
45
+ т: 'n',
46
+ ь: 'm',
47
+ б: ',',
48
+ ю: '.',
49
+ ё: '`',
50
+ };
51
+
52
+ export const EmailCharactersMap: ICharactersMap = {
53
+ ...CharactersMap,
54
+ '"': '@',
55
+ };
56
+
57
+ export const TransliterationMap: ICharactersMap = {
58
+ й: 'i',
59
+ ц: 'ts',
60
+ у: 'u',
61
+ к: 'k',
62
+ е: 'e',
63
+ н: 'n',
64
+ г: 'g',
65
+ ш: 'sh',
66
+ щ: 'shch',
67
+ з: 'z',
68
+ х: 'h',
69
+ ъ: 'ie',
70
+ ф: 'f',
71
+ ы: 'y',
72
+ в: 'v',
73
+ а: 'a',
74
+ п: 'p',
75
+ р: 'r',
76
+ о: 'o',
77
+ л: 'l',
78
+ д: 'd',
79
+ ж: 'zh',
80
+ э: 'e',
81
+ я: 'ia',
82
+ ч: 'ch',
83
+ с: 's',
84
+ м: 'm',
85
+ и: 'i',
86
+ т: 't',
87
+ ь: '',
88
+ б: 'b',
89
+ ю: 'iu',
90
+ ё: 'e',
91
+ };
@@ -1,13 +1,26 @@
1
- import { ISmartType } from './types';
2
-
3
- export const transformCaseSensitive = (
4
- smartType: ISmartType,
5
- map: { [key: string]: string },
6
- symbol: string,
7
- ): string => {
8
- const isUpperCase = symbol.toUpperCase() === symbol;
9
- const result = smartType.endsWith('RuEn')
10
- ? symbol.toLowerCase()
11
- : map[symbol.toLowerCase()] ?? symbol;
12
- return isUpperCase ? result.toUpperCase() : result;
13
- };
1
+ import { ICharactersMap } from './types';
2
+
3
+ export const mapSymbols = (
4
+ str: string,
5
+ regex: RegExp,
6
+ charactersMap: ICharactersMap,
7
+ isTransliterationEnabled: boolean,
8
+ ): string =>
9
+ str
10
+ .split('')
11
+ .map((symbol) => {
12
+ if (regex.test(symbol)) {
13
+ return symbol;
14
+ }
15
+ if (!isTransliterationEnabled) {
16
+ return '';
17
+ }
18
+
19
+ const mappedSymbol = charactersMap[symbol.toLowerCase()] ?? symbol;
20
+
21
+ const isUpperCase = symbol.toUpperCase() === symbol;
22
+ const result = isUpperCase ? mappedSymbol.toUpperCase() : mappedSymbol;
23
+
24
+ return regex.test(result) ? result : '';
25
+ })
26
+ .join('');
@@ -1,11 +1,18 @@
1
- export type ISmartType =
2
- | 'name'
3
- | 'nameRuEn'
4
- | 'surname'
5
- | 'surnameRuEn'
6
- | 'email'
7
- | 'agencyName'
8
- | 'default'
9
- | 'digits'
10
- | 'docNumber'
11
- | 'benefitCert';
1
+ export type ICharactersMap = Record<string, string>;
2
+
3
+ export interface IInputRawValue {
4
+ rawValue: string;
5
+ isValid: boolean;
6
+ }
7
+
8
+ export type ISmartType =
9
+ | 'name'
10
+ | 'nameRuEn'
11
+ | 'surname'
12
+ | 'surnameRuEn'
13
+ | 'email'
14
+ | 'agencyName'
15
+ | 'default'
16
+ | 'digits'
17
+ | 'docNumber'
18
+ | 'benefitCert';