11import  React ,  {  useState ,  useEffect  }  from  'react' ; 
2+ import  shortid  from  'shortid' ; 
23
34import  classnames  from  '../../utils/classNames' ; 
45
56import  './index.scss' ; 
67import  Icon  from  '../Icons' ; 
78import  {  IProps  }  from  './types' ; 
9+ import  RateIcon  from  './subcomponents/rate-icon' ; 
810
911const  baseClass  =  'vant-rate' ; 
1012
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 }  } 
35+       /> 
36+     ) ; 
1437  } 
1538  return  icons ; 
1639} ; 
@@ -28,32 +51,9 @@ const Rate = ({
2851  allowHalf, 
2952  disabled, 
3053  readonly, 
31-   touchable 
54+   change 
3255} : 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 ) ; 
5757
5858  const  rateProps  =  { 
5959    className : classnames ( baseClass ,  [ 
@@ -65,13 +65,39 @@ const Rate = ({
6565    ] ) 
6666  } ; 
6767
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+ 
6879  return  ( 
6980    < div  { ...rateProps } > 
70-       { rateIcons . map ( ( v ,  i )  =>  ( 
71-         < div  key = { i }  className = { `${ baseClass }  } > 
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+       ) } 
75101    </ div > 
76102  ) ; 
77103} ; 
0 commit comments