-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
73 lines (69 loc) · 2.03 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
70
71
72
73
import React, { useRef, useCallback, useState } from 'react';
import ReactDOM from 'react-dom';
import './style.css';
import 'normalize.css';
const itemMaps = {
0: '',
1: '石',
2: '銅',
3: '鐵',
4: '銀',
5: '金',
6: '破',
7: '化',
8: '琥',
9: '紅',
10: '藍',
11: '綠',
12: '鑽',
13: '下'
};
const App = () => {
const selectRef = useRef();
const bedRef = useRef();
const selectedBedIndex = useRef(0);
const [bed, setBed] = useState(() => Array(49).fill(0));
const onBedClick = (index, item) => {
selectRef.current.style.opacity = 1;
selectRef.current.focus();
selectedBedIndex.current = index;
selectRef.current.selectedIndex = item;
};
const onItemChange = useCallback(
e => {
const index = selectedBedIndex.current;
const newBed = bed.slice();
newBed[index] = +e.target.value;
setBed(newBed);
selectRef.current.style.opacity = 0;
selectRef.current.blur();
bedRef.current.focus();
},
[bed]
);
const reset = () => setBed(() => Array(49).fill(0));
return (
<>
<div ref={bedRef} className="bed">
{bed.map((item, i) => (
<button onClick={() => onBedClick(i, item)} key={i}>
<span>{itemMaps[item]}</span>
</button>
))}
</div>
<div className="controls">
<button onClick={reset}>Reset</button>
<select onChange={e => onItemChange(e)} ref={selectRef}>
{Object.keys(itemMaps).map(key => (
<option key={key} value={key}>
{itemMaps[key]}
</option>
))}
</select>
</div>
<p>化:化石類</p>
<p>下:下一層</p>
</>
);
};
ReactDOM.render(<App />, document.getElementById('root'));