11import React from 'react' ;
2- import { View , StyleSheet , TouchableOpacity } from 'react-native' ;
2+ import { View , StyleSheet , TouchableOpacity , Text , StyleProp , TextStyle } from 'react-native' ;
33
44import Icon , { IconsName } from '../Icon' ;
55import { TabsItemIconTypes } from '../Tabs/TabsItem' ;
@@ -24,6 +24,12 @@ export interface RatingProps {
2424 * @param score type: number 得到几分
2525 */
2626 onPress ?: ( score : number ) => void ;
27+ /** 自定义每项的提示信息 */
28+ tooltips ?: string [ ] ;
29+ /** 自定义提示信息样式 */
30+ tooltipsStyle ?: StyleProp < TextStyle > ;
31+ /** 只读模式 */
32+ disabled : boolean ;
2733}
2834
2935export interface RatingState {
@@ -33,66 +39,87 @@ export interface RatingState {
3339 color : string ;
3440 defaultRating : number ;
3541 typeIcon : icoType ;
42+ tooltips ?: string [ ] ;
43+ tooltipsText ?: string ;
44+ disabled : boolean ;
3645}
3746
3847export default class Rating extends React . Component < RatingProps , RatingState > {
3948 constructor ( props : RatingProps ) {
4049 super ( props ) ;
4150 let start = ( props . icon && props . icon . unactived ) || 'star-off' ;
4251 let end = ( props . icon && props . icon . actived ) || 'star-on' ;
52+
4353 this . state = {
4454 defaultRating : props . defaultRating || 0 ,
4555 resultRating : props . resultRating || 5 ,
4656 icon : [ ] ,
4757 size : props . size ?? 24 ,
4858 color : props . color || '#ebc445' ,
4959 typeIcon : [ start , end ] ,
60+ tooltips : props . tooltips ,
61+ tooltipsText : '' ,
62+ disabled : false ,
5063 } ;
5164 }
5265 componentDidMount ( ) {
5366 const { defaultRating } = this . state ;
5467 this . updateIcon ( defaultRating ) ;
5568 }
69+
5670 flag = true ;
5771 updateIcon = ( index : number ) => {
5872 const { resultRating } = this . state ;
59- const { onPress } = this . props ;
73+ const { onPress, disabled } = this . props ;
6074 let start = this . state . typeIcon [ 0 ] ;
6175 let end = this . state . typeIcon [ 1 ] ;
76+ if ( disabled ) {
77+ this . setState ( { disabled : disabled } ) ;
78+ }
6279 if ( index === 1 && this . flag ) {
6380 this . setState ( { icon : [ ...new Array ( index ) . fill ( end ) , ...new Array ( resultRating - index ) . fill ( start ) ] } ) ;
6481 onPress ?.( 1 ) ;
82+ if ( this . state . tooltips ) {
83+ this . setState ( { tooltipsText : this . state . tooltips [ index ] } ) ;
84+ }
6585 this . flag = false ;
6686 } else if ( index === 1 && ! this . flag ) {
6787 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 ] } ) ;
90+ }
6891 this . flag = true ;
6992 onPress ?.( 0 ) ;
7093 } else {
7194 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 ] } ) ;
97+ }
7298 this . flag = true ;
7399 onPress ?.( index ) ;
74100 }
75101 } ;
76102 render ( ) {
103+ const { icon, size, color, tooltipsText, disabled } = this . state ;
104+ const { tooltipsStyle } = this . props ;
77105 return (
78106 < View >
79107 < View style = { styles . RatingContainer } >
80- { this . state . icon . map ( ( item , index ) => {
108+ { icon . map ( ( item , index ) => {
81109 return (
82110 < TouchableOpacity
83111 onPress = { ( ) => {
84- this . updateIcon ( index + 1 ) ;
112+ if ( disabled === false ) {
113+ this . updateIcon ( index + 1 ) ;
114+ }
85115 } }
86116 key = { index }
87117 >
88- { typeof item === 'string' ? (
89- < Icon name = { item as IconsName } color = "#ebc445" size = { this . state . size } />
90- ) : (
91- item
92- ) }
118+ { typeof item === 'string' ? < Icon name = { item as IconsName } color = { color } size = { size } /> : item }
93119 </ TouchableOpacity >
94120 ) ;
95121 } ) }
122+ < Text style = { [ styles . tooltipsText , { fontSize : size - 5 , color : color } , tooltipsStyle ] } > { tooltipsText } </ Text >
96123 </ View >
97124 </ View >
98125 ) ;
@@ -102,5 +129,9 @@ export default class Rating extends React.Component<RatingProps, RatingState> {
102129const styles = StyleSheet . create ( {
103130 RatingContainer : {
104131 flexDirection : 'row' ,
132+ alignItems : 'center' ,
133+ } ,
134+ tooltipsText : {
135+ marginHorizontal : 10 ,
105136 } ,
106137} ) ;
0 commit comments