1
1
import React , { useState , useEffect } from 'react' ;
2
+ import shortid from 'shortid' ;
2
3
3
4
import classnames from '../../utils/classNames' ;
4
5
5
6
import './index.scss' ;
6
7
import Icon from '../Icons' ;
7
8
import { IProps } from './types' ;
9
+ import RateIcon from './subcomponents/rate-icon' ;
8
10
9
11
const baseClass = 'vant-rate' ;
10
12
11
- const renderIcons = ( icons , iconCount , iconName , iconColor , iconSize ) => {
12
- for ( let i = 0 ; i < iconCount ; i ++ ) {
13
- icons . push ( < Icon color = { iconColor } size = { iconSize } name = { iconName } /> ) ;
13
+ const renderIcon = (
14
+ color ,
15
+ size ,
16
+ icon ,
17
+ numberOfIcons ,
18
+ handleClick ,
19
+ isActive ,
20
+ activeCount ,
21
+ gutter
22
+ ) => {
23
+ const icons = new Array ( numberOfIcons ) ;
24
+ for ( let i = 0 ; i < numberOfIcons ; i ++ ) {
25
+ icons . push (
26
+ < RateIcon
27
+ index = { i }
28
+ gutter = { gutter }
29
+ handleClick = { ( index ) =>
30
+ handleClick ( isActive ? index : index + activeCount )
31
+ }
32
+ key = { shortid . generate ( ) }
33
+ icon = { < Icon color = { color } size = { size } name = { icon } /> }
34
+ className = { `${ baseClass } __icon` }
35
+ />
36
+ ) ;
14
37
}
15
38
return icons ;
16
39
} ;
@@ -28,32 +51,9 @@ const Rate = ({
28
51
allowHalf,
29
52
disabled,
30
53
readonly,
31
- touchable
54
+ change
32
55
} : IProps ) => {
33
- const [ rateIcons , setRateIcons ] = useState ( [ ] ) ;
34
-
35
- useEffect ( ( ) => {
36
- let currentIcons = [ ...rateIcons ] ;
37
- if ( disabled ) {
38
- currentIcons = renderIcons (
39
- currentIcons ,
40
- count ,
41
- icon ,
42
- disabledColor ,
43
- size
44
- ) ;
45
- } else {
46
- currentIcons = renderIcons ( currentIcons , currentRate , icon , color , size ) ;
47
- currentIcons = renderIcons (
48
- currentIcons ,
49
- count - currentRate ,
50
- voidIcon ,
51
- voidColor ,
52
- size
53
- ) ;
54
- }
55
- setRateIcons ( currentIcons ) ;
56
- } , [ ] ) ;
56
+ const [ activeCount , setActiveCount ] = useState ( currentRate || count ) ;
57
57
58
58
const rateProps = {
59
59
className : classnames ( baseClass , [
@@ -65,13 +65,39 @@ const Rate = ({
65
65
] )
66
66
} ;
67
67
68
+ // TODO: Add half star feature
69
+ // TODO: Add touchable feature
70
+
71
+ const handleClick = ( index ) => {
72
+ if ( ! disabled && ! readonly ) {
73
+ const nextRate = index + 1 ;
74
+ setActiveCount ( nextRate ) ;
75
+ if ( ! ! change ) change ( nextRate ) ;
76
+ }
77
+ } ;
78
+
68
79
return (
69
80
< div { ...rateProps } >
70
- { rateIcons . map ( ( v , i ) => (
71
- < div key = { i } className = { `${ baseClass } __icon` } >
72
- { v }
73
- </ div >
74
- ) ) }
81
+ { renderIcon (
82
+ disabled ? disabledColor : color ,
83
+ size ,
84
+ icon ,
85
+ activeCount ,
86
+ handleClick ,
87
+ true ,
88
+ activeCount ,
89
+ gutter
90
+ ) }
91
+ { renderIcon (
92
+ disabled ? disabledColor : voidColor ,
93
+ size ,
94
+ voidIcon ,
95
+ count - activeCount ,
96
+ handleClick ,
97
+ false ,
98
+ activeCount ,
99
+ gutter
100
+ ) }
75
101
</ div >
76
102
) ;
77
103
} ;
0 commit comments