Star.js
1.28 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
53
54
import React ,{useState} from 'react'
import StarRate from './StarRate'
function Star() {
const [idx, setIdx] = useState(0);
const [rating, setRating] = useState(0);
const [cacheIdx, setCacheIdx] = useState(0);
const [cacheRating, setCacheRating] = useState(0);
const [rated, setRated] = useState(false);
const _mouseOver = (e,i) => {
if(!rated){
e.persist()
let offsetX = e.nativeEvent.offsetX;
let clientX = e.target.clientWidth;
if(offsetX > clientX / 2){
let value = 2;
setIdx(i);
setRating(value);
}else{
let value = 1;
setIdx(i);
setRating(value);
}
}
}
const handleChange = (i,v,b) => {
if(b){
setIdx(0);
setRating(0);
setCacheIdx(i);
setCacheRating(v);
setRated(true);
console.log(`i : ${i}`);
console.log(`v : ${v}`);
}
}
return (
<StarRate
_mouseOver={_mouseOver}
onChange={handleChange}
idx={idx}
rating={rating}
cacheIdx={cacheIdx}
cacheRating={cacheRating}
/>
)
}
export default Star