@cdek-it/react-native-ui-kit 0.4.0 → 0.4.1
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,8 +1,10 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
|
|
1
|
+
import { type Ref } from 'react';
|
|
2
|
+
import { TextInput, type TextInputProps, type PressableProps } from 'react-native';
|
|
3
|
+
export interface InputOtpProps extends Omit<TextInputProps, 'onChangeText' | 'onChange' | 'ref' | 'keyboardType' | 'style'>, Pick<PressableProps, 'testOnly_pressed'> {
|
|
3
4
|
length: number;
|
|
4
|
-
|
|
5
|
+
onChange: (value: string) => void;
|
|
5
6
|
disabled?: boolean;
|
|
6
7
|
error?: boolean;
|
|
8
|
+
inputRef?: Ref<TextInput | null>;
|
|
7
9
|
}
|
|
8
10
|
export declare const InputOtp: import("react").NamedExoticComponent<InputOtpProps>;
|
|
@@ -1,29 +1,27 @@
|
|
|
1
|
-
import { memo, useCallback, useMemo, useRef, useState } from 'react';
|
|
1
|
+
import { memo, useCallback, useImperativeHandle, useMemo, useRef, useState, } from 'react';
|
|
2
2
|
import { Pressable, View, TextInput, } from 'react-native';
|
|
3
3
|
import { makeStyles } from '../../../utils/makeStyles';
|
|
4
4
|
import { InputOtpItem } from './InputOtpItem';
|
|
5
|
-
export const InputOtp = memo(({ length,
|
|
5
|
+
export const InputOtp = memo(({ length, onChange, disabled = false, error = false, testOnly_pressed, inputRef: propsInputRef, testID, value = '', onFocus, onBlur, ...rest }) => {
|
|
6
6
|
const styles = useStyles();
|
|
7
|
-
const [value, setValue] = useState('');
|
|
8
7
|
const [isFocused, setIsFocused] = useState(false);
|
|
9
8
|
const inputRef = useRef(null);
|
|
9
|
+
useImperativeHandle(propsInputRef, () => inputRef.current);
|
|
10
10
|
const handlePress = useCallback(() => {
|
|
11
11
|
inputRef.current?.focus();
|
|
12
12
|
}, []);
|
|
13
13
|
const handleChange = useCallback((text) => {
|
|
14
14
|
const sanitizedText = text.replace(/[^0-9]/g, '');
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
inputRef.current?.blur();
|
|
19
|
-
}
|
|
20
|
-
}, [length, onComplete]);
|
|
21
|
-
const handleFocus = useCallback(() => {
|
|
15
|
+
onChange(sanitizedText);
|
|
16
|
+
}, [onChange]);
|
|
17
|
+
const handleFocus = useCallback((e) => {
|
|
22
18
|
setIsFocused(true);
|
|
23
|
-
|
|
24
|
-
|
|
19
|
+
onFocus?.(e);
|
|
20
|
+
}, [onFocus]);
|
|
21
|
+
const handleBlur = useCallback((e) => {
|
|
25
22
|
setIsFocused(false);
|
|
26
|
-
|
|
23
|
+
onBlur?.(e);
|
|
24
|
+
}, [onBlur]);
|
|
27
25
|
const activeIndex = useMemo(() => Math.min(value.length, length - 1), [value.length, length]);
|
|
28
26
|
const renderArray = useMemo(() => Array.from({ length }).fill(null), [length]);
|
|
29
27
|
return (<Pressable disabled={disabled} style={styles.container} testID={testID} testOnly_pressed={testOnly_pressed} onPress={handlePress}>
|
package/package.json
CHANGED