Overview
Many modern applications rely on user feedback to drive product improvements and enhance user experience. A common way to gather such feedback is through star ratings, allowing users to quickly express their sentiment or evaluate a product/service. This problem challenges you to build a robust, accessible, and highly configurable Star Rating component that can be used across various parts of a web application, from product review forms to service feedback modules.
What you need to do / Task
Your primary goal is to implement a reusable StarRating React component with the following features:
- Display Stars: Render a configurable number of stars (e.g., 5 stars by default).
- Initial Rating: The component should accept an
initialRating prop to pre-select a rating.
- Interactive Selection:
- Hover: When a user hovers over the stars, highlight the stars up to the hovered position, indicating a preview of the rating.
- Click: Clicking a star should select that rating. The component should visually represent the selected rating persistently.
- Half-Star Support: The component must allow for selecting and displaying half-star ratings (e.g., 3.5 stars). Hovering should precisely highlight full or half stars based on mouse position.
- Clear Rating: Clicking an already selected full star should clear the rating (set it to 0). If a half-star is selected (e.g., 3.5), clicking it should round down to the full star (3), and a subsequent click on that full star should clear it (0).
- Callback: When the rating changes (due to click or keyboard interaction), an
onChange callback prop should be invoked with the new rating value.
- Keyboard Navigation:
- Users should be able to navigate the stars using the
Left and Right arrow keys to adjust the rating by 0.5 increments (if allowHalfStars is true) or 1 increment (if allowHalfStars is false).
- Pressing
Enter or Space should confirm the currently previewed rating, similar to a click.
- Pressing
Escape should clear the rating (set it to 0).
- Accessibility: Ensure the component is accessible for screen readers and keyboard users by using appropriate ARIA attributes.
UI/UX expectations
- Visual Representation: Stars should clearly indicate their state: empty, half-filled, and fully-filled. Use distinct colors or icons (e.g., Unicode characters, SVGs).
- Hover Feedback: A smooth visual change should occur when hovering over stars to indicate the previewed rating.
- Click Feedback: The selected rating should remain visually distinct from unselected stars.
- Layout: Stars should be arranged horizontally.
- Focus Indicator: Ensure a visible focus indicator for keyboard navigation on the component container.
Acceptance criteria
- The
StarRating component renders correctly with a default number of stars.
- The
initialRating prop correctly sets the initial visual state.
- Hovering over stars accurately previews full and half-star ratings.
- Clicking selects the rating, including half-stars, and updates the visual display.
- Clicking an already selected full star clears the rating; half-star clearing logic follows the specification.
- The
onChange callback is triggered with the correct rating value on selection or keyboard interaction.
- Keyboard navigation (Left/Right arrows for increments, Enter/Space for selection, Escape for clear) functions as specified.
- The component uses appropriate ARIA attributes (
role, aria-valuenow, aria-valuemin, aria-valuemax, aria-label) for accessibility.
- The component is reusable and its internal state management is clean.