UNPKG

2.03 kBTypeScriptView Raw
1import React, { Component } from 'react'
2import { View } from 'react-native'
3import { AirbnbRating, Rating } from 'react-native-ratings'
4import { RatingProps } from '../utils/PropTypes'
5import ErrorTip from './helper/ErrorTip'
6import getFieldDecorator from '../utils/getFieldDecorator'
7import Label from './helper/Label'
8
9export class SsRatingView extends Component<any, any> {
10 static defaultProps = {
11 required: false,
12 }
13
14 private ratingCompleted = (nValue: number) => {
15 const { onChange } = this.props
16 onChange && onChange(nValue)
17 }
18
19 render() {
20 const { label, required, initialValue, allowHalf } = this.props
21 return (
22 <View style={{ flexDirection: 'row', justifyContent: 'space-between', paddingRight: 10, paddingVertical: 8 }}>
23 <Label required={required} label={label} />
24 {allowHalf ? (
25 <Rating
26 showRating={false}
27 startingValue={initialValue}
28 ratingCount={5}
29 onFinishRating={this.ratingCompleted}
30 />
31 ) : (
32 <AirbnbRating
33 reviews={[]}
34 showRating={false}
35 defaultRating={initialValue}
36 count={5}
37 onFinishRating={this.ratingCompleted}
38 />
39 )}
40 </View>
41 )
42 }
43}
44
45export default class SsRating extends Component<RatingProps, {}> {
46 private fieldDecorator: any
47 static defaultProps = {
48 required: false,
49 }
50
51 componentWillMount() {
52 const { form, id, initialValue, rules } = this.props
53 this.fieldDecorator = getFieldDecorator(form, id, initialValue, rules)
54 }
55
56 render() {
57 const { label, required, form, id, onChange, initialValue, allowHalf } = this.props
58 return (
59 <ErrorTip error={form.getFieldError(id)}>
60 {this.fieldDecorator(
61 <SsRatingView
62 label={label}
63 required={required}
64 onChange={onChange}
65 initialValue={Number(initialValue)}
66 allowHalf={allowHalf}
67 />
68 )}
69 </ErrorTip>
70 )
71 }
72}