forked from retyui/react-native-confirmation-code-field
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
67 lines (59 loc) · 1.51 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import React, {useState} from 'react';
import {SafeAreaView, Text} from 'react-native';
import {
CodeField,
Cursor,
useBlurOnFulfill,
useClearByFocusCell,
MaskSymbol,
isLastFilledCell,
} from 'react-native-confirmation-code-field';
import styles from './styles';
const CELL_COUNT = 6;
const MaskExample = () => {
const [value, setValue] = useState('');
const ref = useBlurOnFulfill({value, cellCount: CELL_COUNT});
const [props, getCellOnLayoutHandler] = useClearByFocusCell({
value,
setValue,
});
const renderCell = ({index, symbol, isFocused}) => {
let textChild = null;
if (symbol) {
textChild = (
<MaskSymbol
maskSymbol="❤️"
isLastFilledCell={isLastFilledCell({index, value})}>
{symbol}
</MaskSymbol>
);
} else if (isFocused) {
textChild = <Cursor />;
}
return (
<Text
key={index}
style={[styles.cell, isFocused && styles.focusCell]}
onLayout={getCellOnLayoutHandler(index)}>
{textChild}
</Text>
);
};
return (
<SafeAreaView style={styles.root}>
<Text style={styles.title}>Field with custom mask</Text>
<CodeField
ref={ref}
{...props}
value={value}
onChangeText={setValue}
cellCount={CELL_COUNT}
rootStyle={styles.codeFieldRoot}
keyboardType="number-pad"
textContentType="oneTimeCode"
renderCell={renderCell}
/>
</SafeAreaView>
);
};
export default MaskExample;