@@ -6,12 +6,10 @@ import { TabsItemIconTypes } from '../Tabs/TabsItem';
6
6
7
7
export type icoType = [ TabsItemIconTypes , TabsItemIconTypes ] ;
8
8
export interface RatingProps {
9
- /** 默认几个 */
9
+ /** 默认选中几个 */
10
10
defaultRating ?: number ;
11
- /** 总分 */
12
- count ?: number ;
13
- /** 默认分数 */
14
- defaultCount ?: number ;
11
+ /** 总数*/
12
+ resultRating ?: number ;
15
13
/** icon 大小 */
16
14
size ?: number ;
17
15
/** icon 颜色 */
@@ -27,83 +25,70 @@ export interface RatingProps {
27
25
28
26
export interface RatingState {
29
27
icon : Array < IconsName | React . ReactElement | React . ReactNode > ;
30
- count : number ;
31
- defaultCount : number ;
28
+ resultRating : number ;
32
29
size : number ;
33
30
color : string ;
34
31
defaultRating : number ;
35
32
typeIcon : icoType ;
36
33
}
37
34
38
35
export default class Rating extends React . Component < RatingProps , RatingState > {
39
- // static state: RatingState
40
36
constructor ( props : RatingProps ) {
41
37
super ( props ) ;
42
38
let start = ( props . icon && props . icon [ 0 ] ) || 'star-off' ;
43
39
let end = ( props . icon && props . icon [ 1 ] ) || 'star-on' ;
44
40
this . state = {
45
- defaultRating : props . defaultRating || 5 ,
41
+ defaultRating : props . defaultRating || 0 ,
42
+ resultRating : props . resultRating || 5 ,
46
43
icon : [ ] ,
47
- count : props . count ?? 5 ,
48
- defaultCount : props . defaultCount ?? 1 ,
49
44
size : props . size ?? 24 ,
50
45
color : props . color || '#ebc445' ,
51
46
typeIcon : [ start , end ] ,
52
47
} ;
53
48
}
54
49
componentDidMount ( ) {
55
- const { defaultCount } = this . state ;
56
- this . updateIcon ( defaultCount ) ;
50
+ const { defaultRating } = this . state ;
51
+ this . updateIcon ( defaultRating ) ;
57
52
}
58
53
flag = true ;
59
54
updateIcon = ( index : number ) => {
60
- const { defaultRating } = this . state ;
55
+ const { resultRating } = this . state ;
56
+ const { onPress } = this . props ;
61
57
let start = this . state . typeIcon [ 0 ] ;
62
58
let end = this . state . typeIcon [ 1 ] ;
63
59
if ( index === 1 && this . flag ) {
64
- this . setState ( { icon : [ ...new Array ( index ) . fill ( end ) , ...new Array ( defaultRating - index ) . fill ( start ) ] } ) ;
60
+ this . setState ( { icon : [ ...new Array ( index ) . fill ( end ) , ...new Array ( resultRating - index ) . fill ( start ) ] } ) ;
61
+ onPress ?.( 1 ) ;
65
62
this . flag = false ;
66
63
} else if ( index === 1 && ! this . flag ) {
67
- this . setState ( { icon : [ ...new Array ( index - 1 ) . fill ( end ) , ...new Array ( defaultRating - index + 1 ) . fill ( start ) ] } ) ;
64
+ this . setState ( { icon : [ ...new Array ( index - 1 ) . fill ( end ) , ...new Array ( resultRating - index + 1 ) . fill ( start ) ] } ) ;
68
65
this . flag = true ;
66
+ onPress ?.( 0 ) ;
69
67
} else {
70
- this . setState ( { icon : [ ...new Array ( index ) . fill ( end ) , ...new Array ( defaultRating - index ) . fill ( start ) ] } ) ;
68
+ this . setState ( { icon : [ ...new Array ( index ) . fill ( end ) , ...new Array ( resultRating - index ) . fill ( start ) ] } ) ;
71
69
this . flag = true ;
70
+ onPress ?.( index ) ;
72
71
}
73
72
} ;
74
73
render ( ) {
75
74
return (
76
75
< View >
77
- { /* <Text >{this.state.count}</Text> */ }
78
76
< View style = { styles . RatingContainer } >
79
77
{ this . state . icon . map ( ( item , index ) => {
80
- if ( typeof item === 'string' ) {
81
- return (
82
- < TouchableOpacity
83
- onPress = { ( ) => {
84
- this . updateIcon ( index + 1 ) ;
85
- this . props . onPress &&
86
- this . props . onPress ( ( this . state . count / this . state . defaultRating ) * ( index + 1 ) ) ;
87
- } }
88
- key = { index }
89
- >
78
+ return (
79
+ < TouchableOpacity
80
+ onPress = { ( ) => {
81
+ this . updateIcon ( index + 1 ) ;
82
+ } }
83
+ key = { index }
84
+ >
85
+ { typeof item === 'string' ? (
90
86
< Icon name = { item as IconsName } color = "#ebc445" size = { this . state . size } />
91
- </ TouchableOpacity >
92
- ) ;
93
- } else {
94
- return (
95
- < TouchableOpacity
96
- onPress = { ( ) => {
97
- this . updateIcon ( index + 1 ) ;
98
- this . props . onPress &&
99
- this . props . onPress ( ( this . state . count / this . state . defaultRating ) * ( index + 1 ) ) ;
100
- } }
101
- key = { index }
102
- >
103
- { item }
104
- </ TouchableOpacity >
105
- ) ;
106
- }
87
+ ) : (
88
+ item
89
+ ) }
90
+ </ TouchableOpacity >
91
+ ) ;
107
92
} ) }
108
93
</ View >
109
94
</ View >
0 commit comments