2.3.0 • Published 6 years ago

react-star-ratings v2.3.0

Weekly downloads
45,472
License
BSD-3-Clause
Repository
github
Last release
6 years ago

React Star Ratings

Customizable react star ratings. SVG stars that show aggregate star ratings to the hundreths decimal point.

Install

npm install --save react-star-ratings

Heads up

I made a better version (in my opinion at least) of this repo right here: react-ratings-declarative

It is a lot more extendable and customizable.

Demo

codepen playground of similar project

Demo Example Image

npm.io

Usage

import StarRatings from './react-star-ratings';

class Foo extends Component {
    changeRating( newRating, name ) {
      this.setState({
        rating: newRating
      });
    }

    render() {
      // rating = 2;
      return (
        <StarRatings
          rating={this.state.rating}
          starRatedColor="blue"
          changeRating={this.changeRating}
          numberOfStars={6}
          name='rating'
        />
      );
    }
}


class Bar extends Component {
  render() {
    // aggregateRating = 2.35;
    return (
      <StarRatings
        rating={2.403}
        starDimension="40px"
        starSpacing="15px"
      />
    );
  }
}

API v2

PropTypeDefaultDescriptionExample
ratingnumber0The user's rating. Number of stars to highlight.3
numberOfStarsnumber5The max number of stars to choose from or to display6
changeRatingfunction()=>{}Callback that will be passed the new rating a user selectsconst setNewRating = (rating) => this.props.dispatch( fooActions.setRating(rating) )
starRatedColorstring'rgb(109, 122, 130)'Color of stars that the user has ratedblack
starEmptyColorstring'rgb(203, 211, 227)'Color of stars that the use hasn't ratedgrey
starHoverColorstring'rgb(230, 67, 47)'Color of star when hovering over it in selection modeyellow
starDimensionstring'50px'The width and height of the star15px
starSpacingstring'7px'The spacing between the stars0
gradientPathNamestring''gradientPathname needed if app's path is not at the root/app/
ignoreInlineStylesbooleanfalseignore all the inline styles and write your own css using the provided classestrue
svgIconPathstring'm25,1 6,17h18l-14,11 5,17-15-10-15,10 5-17-14-11h18z'Set a path that describes the svg shape'm25,1 6,17h18l-14,11 5,17-15-10-15,10 5-17-14-11h18z'
svgIconViewBoxstring'0 0 51 48'Set the view box for a custom svg path you might have'0 0 51 48'
namestring''Component's unique identification. Can be used when more than one star rating components are used'rating'

API v1

PropTypeDefaultDescriptionExample
ratingnumber0The user's rating. Number of stars to highlight.3
numOfStarsnumber5The max number of stars to choose from or to display6
changeRatingfunction()=>{}Callback that will be passed the new rating a user selectsconst setNewRating = (rating) => this.props.dispatch( fooActions.setRating(rating) )
isSelectablebooleanfalseDetermines whether user can select a new rating or whether the stars are just for displaytrue
isAggregateRatingbooleantrueDetermines whether stars' will show a fraction of a star (.5 stars)false
starSelectingHoverColorstring'rgb(230, 67, 47)'Color of star when hovering over it in selection modeyellow
starRatedColorstring'rgb(109, 122, 130)'Color of stars that the user has ratedblack
starEmptyColorstring'rgb(203, 211, 227)'Color of stars that the use hasn't ratedgrey
starWidthAndHeightstring'50px'The width and height of the star15px
starSpacingstring'7px'The spacing between the stars0
gradientPathNamestring''gradientPathname needed if app's path is not at the root/app/
ignoreInlineStylesbooleanfalseignore all the inline styles and write your own css using the provided classestrue
svgIconPathstring'm25,1 6,17h18l-14,11 5,17-15-10-15,10 5-17-14-11h18z'Set a path that describes the svg shape'm25,1 6,17h18l-14,11 5,17-15-10-15,10 5-17-14-11h18z'
svgIconViewBoxstring'0 0 51 48'Set the view box for a custom svg path you might have'0 0 51 48'

Browser Support

Supports Chrome, firefox, safari, edge, and ie 9+. The star is SVG, so this library fails for any browser that doesn't support svg.

Potential Gradient Path Name Issue

I use the css property fill: 'url(#starGrad<randomNum>)'; to fill in just a percentage of a star. It has some weird bugs depending on the pathname of the app. Normally SPA's have window.location.pathname === '/', but if you append window.location.origin with the pathname of say app, so that window.location.pathname === '/app/', then you need a gradientPathName of '/app/'.

Here is a stackoverflow post that I found that was related to this issue: http://stackoverflow.com/questions/36774188/svg-internal-url-links-and-iframes-on-wirecloud

Try Example And Contribute

To try out the example in this repo: First clone this repo. And then using a complicated build set up stolen from TJ you run make start and go to port 5000. I actually used a forked version of that with slight changes that makes it easier to build multiple files. The only change I made pertains to how the babel cli is used

If you want to contribute: Make changes in the src folder. And then run make build. And of course test by running make start. The make build command compiles react and es6 stuff using babel from src/ into build/. (make start currently broken. instead cd in test folder and run npm run start)

2.3.0

6 years ago

2.2.0

6 years ago

2.1.0

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.4.1

6 years ago

1.4.0

6 years ago

1.3.2

6 years ago

1.3.0

6 years ago

1.2.0

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.20

7 years ago

1.0.19

7 years ago

1.0.18

7 years ago

1.0.17

7 years ago

1.0.16

7 years ago

1.0.15

7 years ago

1.0.14

7 years ago

1.0.13

7 years ago

1.0.12

7 years ago

1.0.11

7 years ago

1.0.10

7 years ago

1.0.9

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago