@cdek-it/react-native-ui-kit 0.3.0 → 0.4.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.
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type TextInputProps, type PressableProps } from 'react-native';
|
|
2
|
+
export interface InputOtpProps extends Omit<TextInputProps, 'value' | 'onChangeText' | 'onFocus' | 'onBlur' | 'ref' | 'keyboardType' | 'style'>, Pick<PressableProps, 'testOnly_pressed'> {
|
|
3
|
+
length: number;
|
|
4
|
+
onComplete?: (value: string) => void;
|
|
5
|
+
disabled?: boolean;
|
|
6
|
+
error?: boolean;
|
|
7
|
+
}
|
|
8
|
+
export declare const InputOtp: import("react").NamedExoticComponent<InputOtpProps>;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { memo, useCallback, useMemo, useRef, useState } from 'react';
|
|
2
|
+
import { Pressable, View, TextInput, } from 'react-native';
|
|
3
|
+
import { makeStyles } from '../../../utils/makeStyles';
|
|
4
|
+
import { InputOtpItem } from './InputOtpItem';
|
|
5
|
+
export const InputOtp = memo(({ length, onComplete, disabled = false, error = false, testOnly_pressed, testID, ...rest }) => {
|
|
6
|
+
const styles = useStyles();
|
|
7
|
+
const [value, setValue] = useState('');
|
|
8
|
+
const [isFocused, setIsFocused] = useState(false);
|
|
9
|
+
const inputRef = useRef(null);
|
|
10
|
+
const handlePress = useCallback(() => {
|
|
11
|
+
inputRef.current?.focus();
|
|
12
|
+
}, []);
|
|
13
|
+
const handleChange = useCallback((text) => {
|
|
14
|
+
const sanitizedText = text.replace(/[^0-9]/g, '');
|
|
15
|
+
setValue(sanitizedText);
|
|
16
|
+
if (sanitizedText.length === length) {
|
|
17
|
+
onComplete?.(sanitizedText);
|
|
18
|
+
inputRef.current?.blur();
|
|
19
|
+
}
|
|
20
|
+
}, [length, onComplete]);
|
|
21
|
+
const handleFocus = useCallback(() => {
|
|
22
|
+
setIsFocused(true);
|
|
23
|
+
}, []);
|
|
24
|
+
const handleBlur = useCallback(() => {
|
|
25
|
+
setIsFocused(false);
|
|
26
|
+
}, []);
|
|
27
|
+
const activeIndex = useMemo(() => Math.min(value.length, length - 1), [value.length, length]);
|
|
28
|
+
const renderArray = useMemo(() => Array.from({ length }).fill(null), [length]);
|
|
29
|
+
return (<Pressable disabled={disabled} style={styles.container} testID={testID} testOnly_pressed={testOnly_pressed} onPress={handlePress}>
|
|
30
|
+
{({ pressed }) => (<>
|
|
31
|
+
<View style={styles.content}>
|
|
32
|
+
{renderArray.map((_, index) => (<InputOtpItem disabled={disabled} error={error} focused={isFocused ? index === activeIndex : false}
|
|
33
|
+
// eslint-disable-next-line react/no-array-index-key
|
|
34
|
+
key={index} pressed={pressed} testID={`${testID}Item`} value={value[index]}/>))}
|
|
35
|
+
</View>
|
|
36
|
+
<TextInput keyboardType='number-pad' maxLength={length} ref={inputRef} style={styles.input} testID={`${testID}HiddenInput`} value={value} onBlur={handleBlur} onChangeText={handleChange} onFocus={handleFocus} {...rest}/>
|
|
37
|
+
</>)}
|
|
38
|
+
</Pressable>);
|
|
39
|
+
});
|
|
40
|
+
const useStyles = makeStyles(({ spacing }) => ({
|
|
41
|
+
container: {},
|
|
42
|
+
content: { flexDirection: 'row', gap: spacing.Gap['gap-2'] },
|
|
43
|
+
input: { position: 'absolute', width: 1, height: 1, opacity: 0 },
|
|
44
|
+
}));
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type ViewProps } from 'react-native';
|
|
2
|
+
export interface InputOtpItemProps extends Pick<ViewProps, 'testID'> {
|
|
3
|
+
value?: string;
|
|
4
|
+
error: boolean;
|
|
5
|
+
pressed: boolean;
|
|
6
|
+
disabled: boolean;
|
|
7
|
+
focused: boolean;
|
|
8
|
+
}
|
|
9
|
+
export declare const InputOtpItem: import("react").NamedExoticComponent<InputOtpItemProps>;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { memo, useEffect } from 'react';
|
|
2
|
+
import { View, Text } from 'react-native';
|
|
3
|
+
import Animated, { Easing, useAnimatedStyle, useSharedValue, withRepeat, withTiming, } from 'react-native-reanimated';
|
|
4
|
+
import { makeStyles } from '../../../utils/makeStyles';
|
|
5
|
+
const CURSOR_ANIMATION_DURATION = 500;
|
|
6
|
+
export const InputOtpItem = memo(({ value, error, pressed, disabled, focused, testID }) => {
|
|
7
|
+
const styles = useStyles();
|
|
8
|
+
const opacity = useSharedValue(1);
|
|
9
|
+
useEffect(() => {
|
|
10
|
+
if (focused) {
|
|
11
|
+
opacity.value = withRepeat(withTiming(0.2, {
|
|
12
|
+
duration: CURSOR_ANIMATION_DURATION,
|
|
13
|
+
easing: Easing.ease,
|
|
14
|
+
}), -1, true);
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
opacity.value = 1;
|
|
18
|
+
}
|
|
19
|
+
}, [focused, opacity]);
|
|
20
|
+
const cursorBlinking = useAnimatedStyle(() => ({ opacity: opacity.value }));
|
|
21
|
+
return (<View style={[
|
|
22
|
+
styles.container,
|
|
23
|
+
error && styles.error,
|
|
24
|
+
pressed && styles.pressed,
|
|
25
|
+
disabled && styles.disabled,
|
|
26
|
+
]}>
|
|
27
|
+
<Text style={styles.text} testID={testID}>
|
|
28
|
+
{value}
|
|
29
|
+
{focused ? (<Animated.Text style={[styles.cursor, cursorBlinking]}>
|
|
30
|
+
|
|
|
31
|
+
</Animated.Text>) : null}
|
|
32
|
+
</Text>
|
|
33
|
+
</View>);
|
|
34
|
+
});
|
|
35
|
+
const useStyles = makeStyles(({ theme, border, fonts, typography }) => ({
|
|
36
|
+
container: {
|
|
37
|
+
minHeight: theme.Button.Common.buttonHeight,
|
|
38
|
+
minWidth: theme.Button.Common.buttonHeight,
|
|
39
|
+
paddingHorizontal: theme.Form.InputText.inputPaddingLeftRight,
|
|
40
|
+
paddingVertical: theme.Form.InputText.inputPaddingTopBottom,
|
|
41
|
+
borderBottomWidth: border.Width.border,
|
|
42
|
+
borderColor: theme.Form.InputText.inputBorderColor,
|
|
43
|
+
alignItems: 'center',
|
|
44
|
+
justifyContent: 'center',
|
|
45
|
+
},
|
|
46
|
+
text: {
|
|
47
|
+
fontSize: typography.Size['text-2xl'],
|
|
48
|
+
fontFamily: fonts.secondary,
|
|
49
|
+
fontWeight: '400',
|
|
50
|
+
color: theme.Form.InputText.inputTextColor,
|
|
51
|
+
},
|
|
52
|
+
pressed: { borderColor: theme.Form.InputText.inputHoverBorderColor },
|
|
53
|
+
error: { borderColor: theme.Form.InputText.inputErrorBorderColor },
|
|
54
|
+
disabled: { mixBlendMode: 'luminosity', opacity: 0.6 },
|
|
55
|
+
cursor: { color: theme.Form.InputText.inputFocusBorderColor },
|
|
56
|
+
}));
|
package/package.json
CHANGED