Skip to content

Commit 0920189

Browse files
authored
Merge pull request #141 from Monte9/bugfix/fix-swipe-rating
Bugfix/fix swipe rating
2 parents a3a563e + c14cd2b commit 0920189

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

demo/src/SwipeRatingScreen.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ class SwipeRatingScreen extends Component {
2323
</View>
2424
<ScrollView style={styles.flex} contentContainerStyle={styles.center}>
2525
<Card title="DEFAULT" containerStyle={styles.card}>
26-
<Rating showRating={false} fractions={false} />
26+
<Rating showRating={false} fractions={false}/>
2727
</Card>
28-
<Card title="WITH RATING" containerStyle={styles.card}>
29-
<Rating showRating={true} fractions={false} />
28+
<Card title="WITH RATING(custom start value)" containerStyle={styles.card}>
29+
<Rating showRating={true} fractions={false} startingValue={ 4 }/>
3030
</Card>
3131
<Card title="WITH FRACTIONS" containerStyle={styles.card}>
3232
<Rating

src/SwipeRating.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,15 @@ export default class SwipeRating extends Component {
104104

105105
componentDidMount() {
106106
try {
107-
this.setState( { display: true, isComponentMounted: true } );
107+
this.setState(
108+
{ display: true, isComponentMounted: true },
109+
() => this.setCurrentRating( this.props.startingValue )
110+
);
108111
} catch ( err ) {
109112
// eslint-disable-next-line no-console
110113
console.log( err )
111114
}
112115

113-
this.setCurrentRating( this.props.startingValue );
114116
}
115117

116118
componentDidUpdate( prevProps ) {
@@ -203,7 +205,9 @@ export default class SwipeRating extends Component {
203205
} else if ( value < -ratingCount * imageSize / 2 ) {
204206
currentRating = this.props.minValue ? this.props.minValue : 0;
205207
} else if ( value <= imageSize || value > imageSize ) {
206-
currentRating = ( startingValue + value ) / imageSize;
208+
const diff = value / imageSize
209+
210+
currentRating = startingValue + diff;
207211
currentRating = fractions ? Number( currentRating.toFixed( fractions ) ) : Math.ceil( currentRating );
208212
} else {
209213
currentRating = fractions ? Number( startingValue.toFixed( fractions ) ) : Math.ceil( startingValue );

0 commit comments

Comments
 (0)