Build an Accessible Star Rating Component
Imagine you're contributing to a new product review system for an e-commerce platform. A critical part of user feedback is the ability to rate products easily and intuitively. This problem focuses on building a robust and accessible star rating component that can be integrated across various product pages, allowing users to provide feedback with a simple click.
Your task is to create a reusable StarRating React functional component that meets the following requirements:
StarRating functional component that accepts the following props:
totalStars: A number indicating the maximum number of stars (e.g., 5). Defaults to 5.initialRating: A number representing the pre-selected rating. Defaults to 0.onRatingChange: An optional callback function (rating: number) => void that is invoked when a new rating is selected.totalStars visually (e.g., using a star character like ★).StarRating component, it should revert to showing the currently selected permanent rating.onRatingChange callback should be fired with the new rating.role and aria-label).StarRating component renders the correct number of stars based on totalStars prop.initialRating (or 0 if not provided).onRatingChange callback is triggered with the correct rating when a star is clicked.Constraints
styles.css. No external styling libraries or CSS-in-JS solutions are allowed.totalStars prop will always be a positive integer, typically between 1 and 10.Loading editor...