-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathhooks.ts
More file actions
52 lines (48 loc) · 1.2 KB
/
hooks.ts
File metadata and controls
52 lines (48 loc) · 1.2 KB
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
import { useState, useCallback, useEffect } from 'react'
const MNEMONIC_SENTENCE_WORDS = 12
export const useInputWords = (wordsCount: number = MNEMONIC_SENTENCE_WORDS) => {
const [inputsWords, setInputsWords] = useState<string[]>(new Array(wordsCount).fill(''))
const onChangeInput = useCallback(
(
e:
| React.ChangeEvent<HTMLInputElement>
| {
target: {
dataset: { idx: string }
value: string
}
}
) => {
const idx = Number(e.target.dataset.idx)
if (Number.isNaN(idx)) return
const { value } = e.target
if (Number(idx) === 0) {
const list = value
.trim()
.replace(/[^0-9a-z]+/g, ' ')
.split(' ')
if (list.length === MNEMONIC_SENTENCE_WORDS) {
setInputsWords(list)
return
}
}
setInputsWords(v => {
const newWords = [...v]
newWords[idx] = value
return newWords
})
},
[setInputsWords]
)
useEffect(() => {
setInputsWords(new Array(wordsCount).fill(''))
}, [wordsCount])
return {
inputsWords,
onChangeInput,
setInputsWords,
}
}
export default {
useInputWords,
}