1
1
import React from 'react' ;
2
- import { View , StyleSheet , TouchableOpacity } from 'react-native' ;
2
+ import { View , StyleSheet , TouchableOpacity , Text , StyleProp , TextStyle } from 'react-native' ;
3
3
4
4
import Icon , { IconsName } from '../Icon' ;
5
5
import { TabsItemIconTypes } from '../Tabs/TabsItem' ;
@@ -24,6 +24,12 @@ export interface RatingProps {
24
24
* @param score type: number 得到几分
25
25
*/
26
26
onPress ?: ( score : number ) => void ;
27
+ /** 自定义每项的提示信息 */
28
+ tooltips ?: string [ ] ;
29
+ /** 自定义提示信息样式 */
30
+ tooltipsStyle ?: StyleProp < TextStyle > ;
31
+ /** 只读模式 */
32
+ disabled : boolean ;
27
33
}
28
34
29
35
export interface RatingState {
@@ -33,66 +39,87 @@ export interface RatingState {
33
39
color : string ;
34
40
defaultRating : number ;
35
41
typeIcon : icoType ;
42
+ tooltips ?: string [ ] ;
43
+ tooltipsText ?: string ;
44
+ disabled : boolean ;
36
45
}
37
46
38
47
export default class Rating extends React . Component < RatingProps , RatingState > {
39
48
constructor ( props : RatingProps ) {
40
49
super ( props ) ;
41
50
let start = ( props . icon && props . icon . unactived ) || 'star-off' ;
42
51
let end = ( props . icon && props . icon . actived ) || 'star-on' ;
52
+
43
53
this . state = {
44
54
defaultRating : props . defaultRating || 0 ,
45
55
resultRating : props . resultRating || 5 ,
46
56
icon : [ ] ,
47
57
size : props . size ?? 24 ,
48
58
color : props . color || '#ebc445' ,
49
59
typeIcon : [ start , end ] ,
60
+ tooltips : props . tooltips ,
61
+ tooltipsText : '' ,
62
+ disabled : false ,
50
63
} ;
51
64
}
52
65
componentDidMount ( ) {
53
66
const { defaultRating } = this . state ;
54
67
this . updateIcon ( defaultRating ) ;
55
68
}
69
+
56
70
flag = true ;
57
71
updateIcon = ( index : number ) => {
58
72
const { resultRating } = this . state ;
59
- const { onPress } = this . props ;
73
+ const { onPress, disabled } = this . props ;
60
74
let start = this . state . typeIcon [ 0 ] ;
61
75
let end = this . state . typeIcon [ 1 ] ;
76
+ if ( disabled ) {
77
+ this . setState ( { disabled : disabled } ) ;
78
+ }
62
79
if ( index === 1 && this . flag ) {
63
80
this . setState ( { icon : [ ...new Array ( index ) . fill ( end ) , ...new Array ( resultRating - index ) . fill ( start ) ] } ) ;
64
81
onPress ?.( 1 ) ;
82
+ if ( this . state . tooltips ) {
83
+ this . setState ( { tooltipsText : this . state . tooltips [ index ] } ) ;
84
+ }
65
85
this . flag = false ;
66
86
} else if ( index === 1 && ! this . flag ) {
67
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 ] } ) ;
90
+ }
68
91
this . flag = true ;
69
92
onPress ?.( 0 ) ;
70
93
} else {
71
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 ] } ) ;
97
+ }
72
98
this . flag = true ;
73
99
onPress ?.( index ) ;
74
100
}
75
101
} ;
76
102
render ( ) {
103
+ const { icon, size, color, tooltipsText, disabled } = this . state ;
104
+ const { tooltipsStyle } = this . props ;
77
105
return (
78
106
< View >
79
107
< View style = { styles . RatingContainer } >
80
- { this . state . icon . map ( ( item , index ) => {
108
+ { icon . map ( ( item , index ) => {
81
109
return (
82
110
< TouchableOpacity
83
111
onPress = { ( ) => {
84
- this . updateIcon ( index + 1 ) ;
112
+ if ( disabled === false ) {
113
+ this . updateIcon ( index + 1 ) ;
114
+ }
85
115
} }
86
116
key = { index }
87
117
>
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 }
93
119
</ TouchableOpacity >
94
120
) ;
95
121
} ) }
122
+ < Text style = { [ styles . tooltipsText , { fontSize : size - 5 , color : color } , tooltipsStyle ] } > { tooltipsText } </ Text >
96
123
</ View >
97
124
</ View >
98
125
) ;
@@ -102,5 +129,9 @@ export default class Rating extends React.Component<RatingProps, RatingState> {
102
129
const styles = StyleSheet . create ( {
103
130
RatingContainer : {
104
131
flexDirection : 'row' ,
132
+ alignItems : 'center' ,
133
+ } ,
134
+ tooltipsText : {
135
+ marginHorizontal : 10 ,
105
136
} ,
106
137
} ) ;
0 commit comments