-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExample.tsx
72 lines (65 loc) · 1.88 KB
/
Example.tsx
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
70
71
72
import React, { useEffect } from 'react';
import { motion } from 'framer-motion';
import { usePinForm } from '../../index';
import { CSSProperties } from 'react';
const LENGTH = 6;
export function Example() {
const { value, inputRef, focusedIndex, inputProps, onBoxFocus } = usePinForm({ length: LENGTH, autoFocus: true });
useEffect(() => {
const isCompleted = [...value].every(v => v !== ' ');
if (isCompleted) {
window.alert('Congratulation! 🥳');
}
}, [value]);
return (
<div style={containerStyle}>
<input style={inputStyle} ref={inputRef} {...inputProps} />
{[...value].map((v, i) => (
<motion.div
key={i}
aria-hidden
initial={{ backgroundColor: 'rgb(255, 255, 255)' }}
animate={
focusedIndex === i
? {
backgroundColor: ['rgb(200, 200, 200)', 'rgb(100, 100, 100)'],
border: '1px solid rgb(100, 100, 100)',
transition: { repeat: Infinity, repeatType: 'reverse', duration: 0.8 },
}
: v === ' '
? { backgroundColor: 'rgb(255, 255, 255)', border: '1px solid rgb(0, 0, 0)' }
: { backgroundColor: 'rgb(0, 0, 0)', border: '1px solid rgb(0, 0, 0)' }
}
style={boxStyle}
onClick={() => onBoxFocus(i)}
>
{v}
</motion.div>
))}
</div>
);
}
const containerStyle: CSSProperties = {
display: 'flex',
gap: '15px',
width: '100%',
justifyContent: 'center',
};
const inputStyle: CSSProperties = {
position: 'absolute',
top: -9999,
left: -9999,
opacity: 0,
};
const boxStyle: CSSProperties = {
width: '70px',
height: '90px',
borderRadius: '10px',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
color: 'white',
fontSize: '40px',
fontWeight: 600,
backgroundColor: 'black',
};