1- import React from 'react' ;
1+ import React , { useEffect , useState } from 'react' ;
22import { View , StyleSheet , TouchableOpacity , Text , StyleProp , TextStyle } from 'react-native' ;
33
44import Icon , { IconsName } from '../Icon' ;
@@ -44,13 +44,11 @@ export interface RatingState {
4444 disabled : boolean ;
4545}
4646
47- export default class Rating extends React . Component < RatingProps , RatingState > {
48- constructor ( props : RatingProps ) {
49- super ( props ) ;
47+ function Rating ( props : RatingProps ) {
48+ const [ state , setState ] = useState < RatingState > ( ( ) => {
5049 let start = ( props . icon && props . icon . unactived ) || 'star-off' ;
5150 let end = ( props . icon && props . icon . actived ) || 'star-on' ;
52-
53- this . state = {
51+ return {
5452 defaultRating : props . defaultRating || 0 ,
5553 resultRating : props . resultRating || 5 ,
5654 icon : [ ] ,
@@ -61,71 +59,72 @@ export default class Rating extends React.Component<RatingProps, RatingState> {
6159 tooltipsText : '' ,
6260 disabled : false ,
6361 } ;
64- }
65- componentDidMount ( ) {
66- const { defaultRating } = this . state ;
67- this . updateIcon ( defaultRating ) ;
68- }
69-
70- flag = true ;
71- updateIcon = ( index : number ) => {
72- const { resultRating } = this . state ;
73- const { onPress, disabled } = this . props ;
74- let start = this . state . typeIcon [ 0 ] ;
75- let end = this . state . typeIcon [ 1 ] ;
62+ } ) ;
63+ const [ flag , setFlag ] = useState ( true ) ;
64+ const updateIcon = ( index : number ) => {
65+ const { resultRating } = state ;
66+ const { onPress, disabled } = props ;
67+ let start = state . typeIcon [ 0 ] ;
68+ let end = state . typeIcon [ 1 ] ;
7669 if ( disabled ) {
77- this . setState ( { disabled : disabled } ) ;
70+ setState ( { ... state , disabled : disabled } ) ;
7871 }
79- if ( index === 1 && this . flag ) {
80- this . setState ( { icon : [ ...new Array ( index ) . fill ( end ) , ...new Array ( resultRating - index ) . fill ( start ) ] } ) ;
72+ if ( index === 1 && flag ) {
73+ setState ( { ... state , icon : [ ...new Array ( index ) . fill ( end ) , ...new Array ( resultRating - index ) . fill ( start ) ] } ) ;
8174 onPress ?.( 1 ) ;
82- if ( this . state . tooltips ) {
83- this . setState ( { tooltipsText : this . state . tooltips [ index ] } ) ;
75+ if ( state . tooltips ) {
76+ setState ( { ... state , tooltipsText : state . tooltips [ index ] } ) ;
8477 }
85- this . flag = false ;
86- } else if ( index === 1 && ! this . flag ) {
87- this . setState ( { icon : [ ...new Array ( index - 1 ) . fill ( end ) , ...new Array ( resultRating - index + 1 ) . fill ( start ) ] } ) ;
88- if ( this . state . tooltips ) {
89- this . setState ( { tooltipsText : this . state . tooltips [ index - 1 ] } ) ;
78+ setFlag ( false ) ;
79+ } else if ( index === 1 && ! flag ) {
80+ setState ( {
81+ ...state ,
82+ icon : [ ...new Array ( index - 1 ) . fill ( end ) , ...new Array ( resultRating - index + 1 ) . fill ( start ) ] ,
83+ } ) ;
84+ if ( state . tooltips ) {
85+ setState ( { ...state , tooltipsText : state . tooltips [ index - 1 ] } ) ;
9086 }
91- this . flag = true ;
87+ setFlag ( true ) ;
9288 onPress ?.( 0 ) ;
9389 } else {
94- this . setState ( { icon : [ ...new Array ( index ) . fill ( end ) , ...new Array ( resultRating - index ) . fill ( start ) ] } ) ;
95- if ( this . state . tooltips ) {
96- this . setState ( { tooltipsText : this . state . tooltips [ index ] } ) ;
90+ setState ( { ... state , icon : [ ...new Array ( index ) . fill ( end ) , ...new Array ( resultRating - index ) . fill ( start ) ] } ) ;
91+ if ( state . tooltips ) {
92+ setState ( { ... state , tooltipsText : state . tooltips [ index ] } ) ;
9793 }
98- this . flag = true ;
94+ setFlag ( true ) ;
9995 onPress ?.( index ) ;
10096 }
10197 } ;
102- render ( ) {
103- const { icon, size, color, tooltipsText, disabled } = this . state ;
104- const { tooltipsStyle } = this . props ;
105- return (
106- < View >
107- < View style = { styles . RatingContainer } >
108- { icon . map ( ( item , index ) => {
109- return (
110- < TouchableOpacity
111- onPress = { ( ) => {
112- if ( disabled === false ) {
113- this . updateIcon ( index + 1 ) ;
114- }
115- } }
116- key = { index }
117- >
118- { typeof item === 'string' ? < Icon name = { item as IconsName } color = { color } size = { size } /> : item }
119- </ TouchableOpacity >
120- ) ;
121- } ) }
122- < Text style = { [ styles . tooltipsText , { fontSize : size - 5 , color : color } , tooltipsStyle ] } > { tooltipsText } </ Text >
123- </ View >
98+
99+ useEffect ( ( ) => {
100+ updateIcon ( state . defaultRating ) ;
101+ } , [ ] ) ;
102+
103+ const { icon, size, color, tooltipsText, disabled } = state ;
104+ const { tooltipsStyle } = props ;
105+ return (
106+ < View >
107+ < View style = { styles . RatingContainer } >
108+ { icon . map ( ( item , index ) => {
109+ return (
110+ < TouchableOpacity
111+ onPress = { ( ) => {
112+ if ( disabled === false ) {
113+ updateIcon ( index + 1 ) ;
114+ }
115+ } }
116+ key = { index }
117+ >
118+ { typeof item === 'string' ? < Icon name = { item as IconsName } color = { color } size = { size } /> : item }
119+ </ TouchableOpacity >
120+ ) ;
121+ } ) }
122+ < Text style = { [ styles . tooltipsText , { fontSize : size - 5 , color : color } , tooltipsStyle ] } > { tooltipsText } </ Text >
124123 </ View >
125- ) ;
126- }
124+ </ View >
125+ ) ;
127126}
128-
127+ export default Rating ;
129128const styles = StyleSheet . create ( {
130129 RatingContainer : {
131130 flexDirection : 'row' ,
0 commit comments