Code Monkey home page Code Monkey logo

stars-rating-react-hooks's Introduction

stars-rating-react-hooks ⭐️

stars-rating-react-hooks

React Rating Stars - Customizable and headless hooks.


NPM

Install

npm install --save stars-rating-react-hooks

Usage (Basic)

stars-rating-react-hooks

import React from 'react'

import { StarsRating } from "stars-rating-react-hooks";

function Example() {

  const config = {
    totalStars: 5,
    initialSelectedValue: 4.5,
    renderFull: (
      <img src="https://img.icons8.com/ios-filled/50/000000/star--v1.png" />
    ),
    renderEmpty: (
      <img src="https://img.icons8.com/ios/50/000000/star--v1.png" />
    ),
    renderHalf: (
      <img src="https://img.icons8.com/ios-filled/50/000000/star-half-empty.png" />
    )
  };

     <StarsRating config={config}
       onStarsRated={(value) => {
        alert(`${value} stars rated`);
      }}
      onSelecting={(isSelecting, selectingValue) => {
        console.log(isSelecting, selectingValue)
      }} 
    />

    return  <StarsRating config={config} />

}

export default Example

Usage (Customizable and Headless)

import { useStars } from 'stars-rating-react-hooks'

const config = {
    totalStars: 5,
    initialSelectedValue: 2,
    renderFull: '★',
    renderEmpty: '☆',
  };

const {
    stars,
    getStarProps,
    getStarWrapperProps,
    isSelecting,
    selectingValue,
    selectedValue,
  } = useStars(config);

             <span
              {...getStarWrapperProps({
                style: {
                  cursor: 'pointer',
                  display: 'inline-block'
                },
              })}
            >
              {stars?.map((star, i) => (
                <span
                  key={i}
                  {...getStarProps(i, {
                    style: {
                      fontSize: '40px',
                      display: 'inline-block'
                    },
                    onClick: (event, ratedValue) => {
                      console.log(`You just rated ${ratedValue} Stars!!`);
                    },
                  })}
                >
                  {star}
                </span>
              ))}
            </span>

'getStarWrapperProps' and 'getStarProps' are prop getters, More info on prop getters

Demo:

Basic

Open Basic codesandbox

Customizable and Headless:

Open Customizable codesandbox

License

MIT © 07harish

stars-rating-react-hooks's People

Contributors

07harish avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

stars-rating-react-hooks's Issues

Refresh Stars after click

After clicking, clicked number of stars and cursor is shown. Desired behaviour: component is reset
useState with updating of config parameters did not do the trick
Use case is triggering rating on containing page

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.