Giter VIP home page Giter VIP logo

ekeric13 / react-star-ratings Goto Github PK

View Code? Open in Web Editor NEW
149.0 2.0 43.0 683 KB

A customizable svg star rating component for selecting x stars or visualizing x stars

Home Page: https://codepen.io/ekeric13/project/full/DkJYpA/

License: BSD 3-Clause "New" or "Revised" License

JavaScript 93.55% CSS 2.55% Makefile 0.19% HTML 3.71%
star rating star-rating react-component component react svg svg-icons input react-star-rating-component

react-star-ratings's Introduction

React Star Ratings

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

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

Demo Example Image

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

Prop Type Default Description Example
rating number 0 The user's rating. Number of stars to highlight. 3
numberOfStars number 5 The max number of stars to choose from or to display 6
changeRating function ()=>{} Callback that will be passed the new rating a user selects const setNewRating = (rating) => this.props.dispatch( fooActions.setRating(rating) )
starRatedColor string 'rgb(109, 122, 130)' Color of stars that the user has rated black
starEmptyColor string 'rgb(203, 211, 227)' Color of stars that the use hasn't rated grey
starHoverColor string 'rgb(230, 67, 47)' Color of star when hovering over it in selection mode yellow
starDimension string '50px' The width and height of the star 15px
starSpacing string '7px' The spacing between the stars 0
gradientPathName string '' gradientPathname needed if app's path is not at the root /app/
ignoreInlineStyles boolean false ignore all the inline styles and write your own css using the provided classes true
svgIconPath string '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'
svgIconViewBox string '0 0 51 48' Set the view box for a custom svg path you might have '0 0 51 48'
name string '' Component's unique identification. Can be used when more than one star rating components are used 'rating'

API v1

Prop Type Default Description Example
rating number 0 The user's rating. Number of stars to highlight. 3
numOfStars number 5 The max number of stars to choose from or to display 6
changeRating function ()=>{} Callback that will be passed the new rating a user selects const setNewRating = (rating) => this.props.dispatch( fooActions.setRating(rating) )
isSelectable boolean false Determines whether user can select a new rating or whether the stars are just for display true
isAggregateRating boolean true Determines whether stars' will show a fraction of a star (.5 stars) false
starSelectingHoverColor string 'rgb(230, 67, 47)' Color of star when hovering over it in selection mode yellow
starRatedColor string 'rgb(109, 122, 130)' Color of stars that the user has rated black
starEmptyColor string 'rgb(203, 211, 227)' Color of stars that the use hasn't rated grey
starWidthAndHeight string '50px' The width and height of the star 15px
starSpacing string '7px' The spacing between the stars 0
gradientPathName string '' gradientPathname needed if app's path is not at the root /app/
ignoreInlineStyles boolean false ignore all the inline styles and write your own css using the provided classes true
svgIconPath string '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'
svgIconViewBox string '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)

react-star-ratings's People

Contributors

ekeric13 avatar monikamangal123 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

react-star-ratings's Issues

Warning: Prop `id` did not match.

Hello, I'm rendering React 16 on client and server(SSR) with new function React.hydrate(). And console throw Warning message:
Warning: Prop id did not match. Server: "starGrad239537257264089" Client: "starGrad357747279155326"

This is the only problem with this component. Thank's a lot!

changeRating not working

I pretty much took the example provided in the package and put it in one of my components. The changeRating prop is not executed when i click on any of the stars.

I'm using React 16.2 and react-star-ratings 2.2.0

Here is my changeRating method:

changeRating(newRating) {
    this.setState({
        rating: newRating
    });
}

And here is how i render the component:

<StarRatings
    rating={this.state.rating}
    starRatedColor="blue"
    changeRating={this.changeRating}
    numberOfStars={6}
/>

If i edit the state myself the stars get updated and otherwise the component works as expected. The issue is that this.changeRating never gets called.

Help!

disable rating?

Is there a way to disable the rating, just for the view? I noticed that there was a flag (isSelectable) in the previous version of the API, but that's not the case for the API v2. Is there another way of disabling this and why is this feature removed anyway? it's kind of helpful.

UX Improvement -- Add support for afterChangeRating()

Currently this library provides a change function that's triggered every time a star is changed.

However, many of my users invariably end up gliding their finger across the stars when inputting a rating, thus triggering several change events.

It would be great if this library supported an afterChangeRating function.

This function would return the current rating when the user releases their finger from the component, similar to an onMouseUp() event.

There's a good example of how this works in the react-slider library.

Remove './' from usage demo import

Once you import the component by using ./ the app will try to get the component from you local folder instead node_modules folder.

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

Just to avoid an initial error when someone cope and past the usage example like me.

Thanks for the nice component.

className?

Does this really not implement className prop? Seems counterintuitive.

Allow "Type Of Widget" as parameter

It would be nice to be able to set this prop using the API (component):
typeOfWidget: 'Star', line 197 star-ratings.js

If you are using a different icon like hearts or other it would be nice to change this string for the title (hover).

Type definition

// react-star-ratings.d.ts

import * as React from 'react';

declare namespace StarRatings {
  interface Props {
    rating?: number;
    numberOfStars?: number;
    changeRating?: void;
    starRatedColor?: string;
    starEmptyColor?: string;
    starHoverColor?: string;
    starDimension?: string;
    starSpacing?: string;
    gradientPathName?: string;
    ignoreInlineStyles?: boolean;
    svgIconPath?: string;
    svgIconViewBox?: string;
    name?: string;
  }
}
declare class StarRatings extends React.Component<StarRatings.Props, {}> {
  
}
export default StarRatings;

for who needs

Cannot choose half-rating

I'm using the StarRating Component in a form and for some reason the user cannot select a half star. I have added the isAggregateRating to true in the props. Please help.

<StarRatings
  numberOfStars={numberOfStars}
  starHoverColor="chocolate"
  starRatedColor="orange"
  isAggregateRating={true}
  rating={0}
  changeRating={val => onChange(val)}
/>

Stars are not taking width & height

Working fine on Chrome but on Mozilla Starts are not accepting width and height
On Mozilla:
image

On Chrome:
image

My code:

<div className="testiCard-user-reviews-grid__rating-name-wrap">
     <StarRatings
       rating={data.stars}
       starDimension="25"
       starSpacing="2px"
       starRatedColor="var(--h-primary)"
       />
      <p className="testiCard-user-reviews-grid__name">{data.name}</p>
</div>

any idea?

starRatedColor not working

the starRatedColor option is not working wheter i put in starRatedColor='rgb(109, 122, 130)' or starRatedColor='black'. Is there any known bugs? Using 2.3

Disable hover

Hi, I would really appreciate the option for disabling the option to hover.
The color change in the star ratings while hovered can limit the use in mobile phones or touch users.

Thanks

Possibility to add a stroke on svg path

Hello, first thanks a lot for this great rating component !

I'm trying to find a way to provide a stroke to the star's svg path (see picture below), do you have any idea ?

capture d ecran 2018-06-27 a 11 40 22

If nothing allows me to do so, maybe could we add an object svgAttributes that could be destructurated in the svg or even these props:

  • starRatedStroke
  • starEmptyStroke
  • starHoverStroke

Thanks!

Module not found

Is anyone having problems installing the package?
I am getting Module not found: Can't resolve './react-star-ratings'....
I cleared and reinstalled node modules.

using yarn 1.22.5

Can we customize tooltip?

I think that it's better to provide a function to format tooltip such as '10 point's instead of '1 stars',
'20 points' instead of '2 stars'

Tooltip issue

The star tooltip become '4. Stars' for 4.9 rating.
I looked at the source code and found the reason.
image
Please see the attached screenshot.
I guess you wanted to remove 0 at end, but you didn't count dot (.)
formattedRating = formattedRating.slice(0, 2);
so 4.90 will be 4.

Changelog

It is good practice to include changelog when releasing a new version of library. It would help developers identify any (possibly breaking) changes without looking into commits. The changelog can be added to releases version in releases tab on GitHub or a new file called CHANGELOG could be created for ability to read changes offline.

https://keepachangelog.com/en/1.0.0/

How to use the changeRating props with 3 argument

I am finding it difficult to use a function with three argument that works. like
changeRating = (a, b, c)=>{
...do something
};
currently, it only works with two argument, the ratings, and the name, e.g
changeRating = (rating, name) => {
...do something
}
can i extend it?
is there any recommended way?

Cannot find module './star-ratings'

It looks like that the module index.js in the build folder is improperly pathed when trying to pull from the star-ratings.js file in the src folder, which is causing an error when react-star-ratings is imported. Simply modifying the two './' imports in build/index.js also causes the program to crash as well

TypeError: currentRating.toFixed is not a function

link: https://qwrqwr.de/productListing/delivery/76133/Kaiserallee%201,%2076133%20Karlsruhe/2

click to rated star in the middle.

get
node_modules/react-star-ratings/build/star-ratings.js:136

133 | var hoveredRating = this.state.highestStarHovered;
134 | var currentRating = hoveredRating > 0 ? hoveredRating : selectedRating;
135 | // fix it at 2 decimal places and remove trailing 0s

136 | var formattedRating = parseFloat(currentRating.toFixed(2)).toString();
| ^ 137 | if (Number.isInteger(currentRating)) {
138 | formattedRating = String(currentRating);
139 | }

Add formatted tooltip text

Specifying typeOfWidget is insufficient. For example, I'm using this with election software and would like tooltips such as:

John Smith received an average rating of 4.5 stars among 1000 voters

In addition, the current implementation of titleText makes an assumption that typeOfWidget is a noun that can be pluralized by adding an 's'. This won't work in all cases. For example, the plural of "Cherry" is "Cherries"

The tooltip needs to be calculated dynamically. I suggest adding a new property that accepts a function:

tooltipFormater: is passed the current star rating and returns a string.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.