|
@@ -0,0 +1,37 @@
|
|
1
|
+import React, { useEffect, useRef, useState } from 'react'
|
|
2
|
+import Star from './Star'
|
|
3
|
+
|
|
4
|
+const total = new Array(5).fill(0).map((_, inx) => inx + 1)
|
|
5
|
+
|
|
6
|
+// 小数得分会被转为整数
|
|
7
|
+export default props => {
|
|
8
|
+ const [score, setScore] = useState(0)
|
|
9
|
+ const inited = useRef(false)
|
|
10
|
+
|
|
11
|
+ // 暂时只写了默认以及 size=large 的样式
|
|
12
|
+ const sizeClass = props.size ? `${gj-star}-${props.size}` : ''
|
|
13
|
+
|
|
14
|
+ const handleClick = i => {
|
|
15
|
+ if (props.editable) {
|
|
16
|
+ setScore(i)
|
|
17
|
+ if (props.onChange) {
|
|
18
|
+ props.onChange(i)
|
|
19
|
+ }
|
|
20
|
+ }
|
|
21
|
+ }
|
|
22
|
+
|
|
23
|
+ useEffect(() => {
|
|
24
|
+ if (props.value !== null && props.value !== undefined && !inited.current) {
|
|
25
|
+ setScore(Math.floor(props.value))
|
|
26
|
+ inited.current = true
|
|
27
|
+ }
|
|
28
|
+ }, [props.value])
|
|
29
|
+
|
|
30
|
+ return (
|
|
31
|
+ <view className="gj-stars">
|
|
32
|
+ {
|
|
33
|
+ total.map(i => <Star className={`gj-start ${sizeClass}`} activeClass="gj-star-active" key={i} active={score >= i} onClick={() => handleClick(i)} />)
|
|
34
|
+ }
|
|
35
|
+ </view>
|
|
36
|
+ )
|
|
37
|
+}
|