Interactive Star Rating Component
Pinterest users constantly discover new ideas, products, and content. Providing a clear and intuitive way for users to rate these "Pins" or products is crucial for improving recommendations and helping others discover high-quality content. This problem challenges you to build a reusable and accessible Star Rating component, similar to what you might see on an e-commerce product detail page or a user profile.
Your goal is to create a React component called StarRating that allows users to provide a rating by interacting with a set of stars.
StarRating that accepts the following props:
maxRating: number (e.g., 5 for a 5-star system).initialRating?: number (optional, the default selected rating).onRatingChange: (rating: number | null) => void (callback fired when the rating changes, null if cleared).maxRating stars. Each star should visually indicate its state (empty, filled, or hovered).null).onRatingChange callback should be invoked with the new rating (or null).StarRating component renders maxRating distinct star elements.onRatingChange callback is correctly invoked with the new rating (or null) on selection/clear.role, aria-valuenow, aria-valuemax, aria-label) are used for accessibility.Constraints
maxRating prop will always be a positive integer between 1 and 10.StarRating component is designed for reusability, clear props, and internal state management.Loading editor...