@@ -6,12 +6,10 @@ import { TabsItemIconTypes } from '../Tabs/TabsItem';
66
77export type icoType = [ TabsItemIconTypes , TabsItemIconTypes ] ;
88export interface RatingProps {
9- /** 默认几个 */
9+ /** 默认选中几个 */
1010 defaultRating ?: number ;
11- /** 总分 */
12- count ?: number ;
13- /** 默认分数 */
14- defaultCount ?: number ;
11+ /** 总数*/
12+ resultRating ?: number ;
1513 /** icon 大小 */
1614 size ?: number ;
1715 /** icon 颜色 */
@@ -27,83 +25,70 @@ export interface RatingProps {
2725
2826export interface RatingState {
2927 icon : Array < IconsName | React . ReactElement | React . ReactNode > ;
30- count : number ;
31- defaultCount : number ;
28+ resultRating : number ;
3229 size : number ;
3330 color : string ;
3431 defaultRating : number ;
3532 typeIcon : icoType ;
3633}
3734
3835export default class Rating extends React . Component < RatingProps , RatingState > {
39- // static state: RatingState
4036 constructor ( props : RatingProps ) {
4137 super ( props ) ;
4238 let start = ( props . icon && props . icon [ 0 ] ) || 'star-off' ;
4339 let end = ( props . icon && props . icon [ 1 ] ) || 'star-on' ;
4440 this . state = {
45- defaultRating : props . defaultRating || 5 ,
41+ defaultRating : props . defaultRating || 0 ,
42+ resultRating : props . resultRating || 5 ,
4643 icon : [ ] ,
47- count : props . count ?? 5 ,
48- defaultCount : props . defaultCount ?? 1 ,
4944 size : props . size ?? 24 ,
5045 color : props . color || '#ebc445' ,
5146 typeIcon : [ start , end ] ,
5247 } ;
5348 }
5449 componentDidMount ( ) {
55- const { defaultCount } = this . state ;
56- this . updateIcon ( defaultCount ) ;
50+ const { defaultRating } = this . state ;
51+ this . updateIcon ( defaultRating ) ;
5752 }
5853 flag = true ;
5954 updateIcon = ( index : number ) => {
60- const { defaultRating } = this . state ;
55+ const { resultRating } = this . state ;
56+ const { onPress } = this . props ;
6157 let start = this . state . typeIcon [ 0 ] ;
6258 let end = this . state . typeIcon [ 1 ] ;
6359 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 ) ;
6562 this . flag = false ;
6663 } 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 ) ] } ) ;
6865 this . flag = true ;
66+ onPress ?.( 0 ) ;
6967 } 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 ) ] } ) ;
7169 this . flag = true ;
70+ onPress ?.( index ) ;
7271 }
7372 } ;
7473 render ( ) {
7574 return (
7675 < View >
77- { /* <Text >{this.state.count}</Text> */ }
7876 < View style = { styles . RatingContainer } >
7977 { 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' ? (
9086 < 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+ ) ;
10792 } ) }
10893 </ View >
10994 </ View >
0 commit comments