0.2.1 • Published 5 years ago

react-native-swipeable-rating v0.2.1

Weekly downloads
268
License
MIT
Repository
github
Last release
5 years ago

react-native-swipeable-rating

Star rating component with support for swipe and / or touch selection

Example

Usage

react-native-vector-icons package is required, set it up if you haven't already.

install > npm i react-native-swipeable-rating

Basic example

import React, { Component } from 'react';
import { View } from 'react-native';
import SwipeableRating from 'react-native-swipeable-rating';

class MyStarRating extends Component {
  state = {
    rating: 0
  }

  handleRating = (rating) => {
    this.setState({rating});
  }

  render(){
    return(
      <View style={{marginHorizontal: 30}}>
        <SwipeableRating
          rating={this.state.rating}
          size={32}
          gap={4}
          onPress={this.handleRating}
          xOffset={30}
        />
      </View>
    )
  }
}

See the full example app

There are a few different ways the component can be used:

  • The above example works with swiping and tapping on individual stars.
  • rating={n}, swipeable={false} and no onPress will make it a dumb/static component that displays a rating.
  • swipeable={false} with an onPress function will make the individual stars a tappable TouchableOpacity.
  • if you use allowHalves you will also need to change minRating to 0.5

Props

Prop nametypedefaultDescRequired?
ratingnumberThe rating
onPressfunctionThe function to be called when swiping or tapping
swipeablebooleantrueAllow/disallow swiping the component to change the rating
xOffsetnumber0The offset from the left of the screen to the start of the component (ignore if swipeable={false})no but you will almost definitely need to change it!
styleobjectStyles the rating container
colorstring'crimson'Color applied to the default star icon
emptyColorstring'crimson'Color applied to the default empty star icon
sizenumber24The size applied to the default icons
gapnumber0marginRight applied to the default icons
minRatingnumber1The minimum rating to allow
maxRatingnumber5The maximum rating to allow / amount of stars to display
allowHalvesbooleanfalseAllow ratings to go up in increments of 0.5 instead of 1
filledIconstring or function'star'The MaterialIcons icon to use for the filled star OR your custom component function (receives size, gap, number args)
halfFilledIconstring or function'star-half'The MaterialIcons icon to use for the half filled star OR your custom component function (receives size, gap, number args)
emptyIconstring or function'star-border'The MaterialIcons icon to use for the empty star OR your custom component function (receives size, gap, number args)

You must make your custom icon components size (+ gap, if you use it) total width, so the correct swipe distances can be calculated.

size, gap, color, emptyColor, n (the icon's rating number (index) beginning at 1) are passed to the custom icon functions for convenience.