forked from thesam73/wordle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Keyboard.tsx
127 lines (121 loc) · 3 KB
/
Keyboard.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { CharValue, getStatuses, Word, isCharValue } from '../../lib/statuses'
import { Key } from './Key'
import { useEffect } from 'react'
import { ENTER_TEXT, DELETE_TEXT } from '../../constants/strings'
export type KeyValue = CharValue | 'ENTER' | 'DELETE'
type Props = {
onChar: (value: CharValue) => void
onDelete: () => void
onEnter: () => void
guesses: Word[]
isRevealing?: boolean
}
export const Keyboard = ({
onChar,
onDelete,
onEnter,
guesses,
isRevealing,
}: Props) => {
const charStatuses = getStatuses(guesses)
const onClick = (value: KeyValue) => {
if (value === 'ENTER') {
onEnter()
} else if (value === 'DELETE') {
onDelete()
} else {
onChar(value)
}
}
useEffect(() => {
const listener = (e: KeyboardEvent) => {
if (e.code === 'Enter') {
onEnter()
} else if (e.code === 'Backspace') {
onDelete()
} else {
const key = e.key.toUpperCase()
if (key.length === 1 && isCharValue(key)) {
onChar(key)
}
}
}
window.addEventListener('keyup', listener)
return () => {
window.removeEventListener('keyup', listener)
}
}, [onEnter, onDelete, onChar])
return (
<div>
<div className="flex justify-center mb-1">
{(
[
'CS',
'DZ',
'DZS',
'GY',
'LY',
'NY',
'SZ',
'TY',
'ZS',
'Ö',
'Ü',
'Ó',
] as const
).map((key) => (
<Key
value={key}
key={key}
onClick={onClick}
status={charStatuses[key]}
isRevealing={isRevealing}
/>
))}
</div>
<div className="flex justify-center mb-1">
{(
['Q', 'W', 'E', 'R', 'T', 'Z', 'U', 'I', 'O', 'P', 'Ő', 'Ú'] as const
).map((key) => (
<Key
value={key}
key={key}
onClick={onClick}
status={charStatuses[key]}
isRevealing={isRevealing}
/>
))}
</div>
<div className="flex justify-center mb-1">
{(
['A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'É', 'Á', 'Ű'] as const
).map((key) => (
<Key
value={key}
key={key}
onClick={onClick}
status={charStatuses[key]}
isRevealing={isRevealing}
/>
))}
</div>
<div className="flex justify-center">
<Key width={65.4} value="ENTER" onClick={onClick}>
{ENTER_TEXT}
</Key>
{(['Í', 'Y', 'X', 'C', 'V', 'B', 'N', 'M'] as const).map((key) => (
<Key
value={key}
key={key}
onClick={onClick}
status={charStatuses[key]}
isRevealing={isRevealing}
/>
))}
<Key width={65.4} value="DELETE" onClick={onClick}>
{DELETE_TEXT}
</Key>
</div>
</div>
)
}