forked from ttdung11t2/react-native-confirmation-code-input
-
Notifications
You must be signed in to change notification settings - Fork 125
/
index.js
69 lines (60 loc) · 1.75 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
68
69
import React, {useState} from 'react';
import {SafeAreaView, Text, View} from 'react-native';
import {
CodeField,
Cursor,
useBlurOnFulfill,
useClearByFocusCell,
} from 'react-native-confirmation-code-field';
import styles from './styles';
const CELL_COUNT = 5;
// Inspired by
// https://github.com/retyui/react-native-confirmation-code-field/issues/129
// https://dribbble.com/shots/8020632/attachments/530078?mode=media
const UnmaskExample = () => {
const [enableMask, setEnableMask] = useState(true);
const [value, setValue] = useState('');
const ref = useBlurOnFulfill({value, cellCount: CELL_COUNT});
const [props, getCellOnLayoutHandler] = useClearByFocusCell({
value,
setValue,
});
const toggleMask = () => setEnableMask((f) => !f);
const renderCell = ({index, symbol, isFocused}) => {
let textChild = null;
if (symbol) {
textChild = enableMask ? '•' : symbol;
} 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}>Show & Hide Password</Text>
<View style={styles.fieldRow}>
<CodeField
ref={ref}
{...props}
value={value}
onChangeText={setValue}
cellCount={CELL_COUNT}
keyboardType="number-pad"
textContentType="oneTimeCode"
renderCell={renderCell}
/>
<Text style={styles.toggle} onPress={toggleMask}>
{enableMask ? '🙈' : '🐵'}
</Text>
</View>
</SafeAreaView>
);
};
export default UnmaskExample;