npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-star-rating-system

v1.0.2

Published

Star rating system written in react.

Downloads

3

Readme

React Star Rating System Docs

  • Component Name: StarRatingSystem
  • Component Interface:
    • hoverColor: string; // yellow
    • clickColor: string; // gold
    • defaultColor: string; // lightGray
    • numberOfStars: number; // 5
    • svg: { width: string; height: string; viewBox: string; svgProps: { ...rest } // any component props; pathProps: { ...rest } // any component props; }; // default undefined... Allows you to customize the SVG being used.
    • container: ReactNode | ReactNode[]; // undefined
    • customClick: (clickRating?: number) => void; // () => {}... Allows you to perform custom behaviour on click
    • customHover: (hoverRating?: number) => void; // () => {}... Allows you to perform custom behaviour on hover (like a network call)
    • customSubmit: (rating?: number) => void // undefined, relies on isCustomSubmit to be true to work... Allows you to perform custom behaviour on submit, like if you wanted to submit the rating result at a later time and attach to your own click handler
    • defaultRating: number; // default 0
    • shouldResetWhenNotHovered: boolean // false... resets stars to default color and rating (doesn't work if you provide a default rating)
    • shouldResetWhenNotClicked: boolean; // false... resets starts to default color and rating when you click on something that isn't stars themself (doesn't apply if you provide defaultRating)
    • isCustomSubmit: boolean; // false, I recommend setting this props with a click handler rather than using a plain boolean value. You could do the below:
    • const handleClick = (val) => { setIsCustomSubmit(true); // if defaultRating is set in the customSubmit function, then you don't need to setIsCustomSubmit back to false // if you don't use defaultRating prop after submitting, then you'll need to find a way to set isCustomSubmit back to false to prevent the UI from re-rendering over and over an infinite amount of times... Here is one way to do that, by adding this code to this function. setTimeout(() => { setIsCustomSubmit(false); }, 0); };

Example of Package Being Used

export default function App() {
  const [data, setData] = useState({title: '', id: 0});
  const [rating, setRating] = useState(0);
  const [isCustomSubmit, setIsCustomSubmit] = useState(false);

  const customClick = async (clickRating: number) => {
    console.log("click", clickRating);

    if (clickRating) {
      fetch(
        // must await the custom click function at the declaration level too
        "https://jsonplaceholder.typicode.com/todos/" + clickRating
      )
        .then((response) =>
          response.json().then((r) => {
            console.log(r);
            setData(r);
            setRating(clickRating);
          })
        )
        .then((json) => console.log(json));

      return console.log(clickRating);
    }
  };

  const customSubmit = async (rating: number) => {
    console.log("submit", rating);

    if (rating) {
      fetch(
        // must await the custom click function at the declaration level too
        "https://jsonplaceholder.typicode.com/todos/" + rating
      )
        .then((response) =>
          response.json().then((r) => {
            console.log(r);
            setData(r);
            setRating(rating);
          })
        )
        .then((json) => console.log(json));

      return console.log(rating);
    }
  };

  const customHover = async (hoverRating: number) => {
    console.log("hover", hoverRating);

    if (hoverRating) {
      return fetch(
        "https://jsonplaceholder.typicode.com/todos/" + hoverRating
      ).then((response) =>
        response.json().then((r) => {
          console.log(r);
        })
      );
    }
  };

  const handleClick = () => {
    setIsCustomSubmit(true);
    setTimeout(() => {
      setIsCustomSubmit(false);
    }, 0); // since it's wrapped in a timeout, it sets it to false after the render, giving the prop time to be true until the submit function is completed.
  };

  return (
    <>
      <h1>Title: {data?.title as string}</h1>
      <p>ID: {data?.id}</p>
      <StarRatingSystem
        defaultRating={rating}
        isCustomSubmit={isCustomSubmit}
        customSubmit={customSubmit}
        shouldResetWhenNotClicked
        shouldResetWhenNotHovered
        container={Container}
      />
      <br />
      <br />
      <button onClick={handleClick}>Submit</button>
    </>
  );
}

const Container = ({children}: any) => {
  return <span id="star-container-out">{children}</span>;
};