零依赖性,可高度自定义的评分组件,适用于 React
Zero dependency, highly customizable rating component for React.
Demo and Examples — NextJS Page Router — Vite
pnpm add @smastrom/react-ratingyarn add @smastrom/react-ratingnpm i @smastrom/react-ratingimport { Rating } from '@smastrom/react-rating'
import '@smastrom/react-rating/style.css'Importing the CSS only once (most likely main.js or App.jsx) is enough to use Rating in any other component of your App.
Remix
app/root.tsx
import styles from '@smastrom/react-rating/style.css';
export function links() {
return [{ rel: 'stylesheet', href: styles }];
}
export default function App() {
// ...in any page.tsx
import { Rating } from '@smastrom/react-rating';
export default function Index() {
// ...NextJS 13 - App Router
app/layout.tsx
import '@smastrom/react-rating/style.css'components/Rating.tsx
'use client'
import { useState } from 'react'
import { Rating as ReactRating } from '@smastrom/react-rating'
export function Rating() {
const [rating, setRating] = useState(0)
return
}in any page/component:
import { Rating } from './components/Rating'
export default function Home() {
return (
)
}app/layout.tsx
import '@smastrom/react-rating/style.css'in any page/component:
import { Rating } from '@smastrom/react-rating'
export default function Home() {
return (
)
}NextJS - Pages Router
pages/_app.js
import '@smastrom/react-rating/style.css';
function MyApp({ Component, pageProps }) {
// ...in any page/component:
import { Rating } from '@smastrom/react-rating';
export default function Home() {
// ...Gatsby
gatsby-browser.js - Create the file at the root of your project if it doesn't exist, and relaunch the dev server.
import '@smastrom/react-rating/style.css'in any page/component:
import { Rating } from '@smastrom/react-rating';
const IndexPage = () => {
// ...Vite
main.jsx
import '@smastrom/react-rating/style.css'in any component:
import { Rating } from '@smastrom/react-rating';
function App() {
// ...Since Rating will span across the entire container, define a maximum width via inline styles or css class:
function App() {
const [rating, setRating] = useState(0) // Initial value
return
}CSS Class
my-styles.css
.my-rating-class {
max-width: 600px;
}App.jsx
import './my-styles.css'
function App() {
const [rating, setRating] = useState(0)
return
}Legend
| Color | Description |
|---|---|
| :green_circle: | Has always effect |
| :large_blue_circle: | Has effect only if readOnly is false |
| :purple_circle: | Has effect only if readOnly is true |
| :thinking: | Prop | Description | Type | Default | Required |
|---|---|---|---|---|---|
| :green_circle: | value |
An integer from 0 to items. It can be a float if readOnly is true. |
number | undefined | :white_check_mark: |
| :large_blue_circle: | onChange |
Setter or custom function to update the rating. | RatingChange | () => {} | Only if readOnly is false |
| :large_blue_circle: | onHoverChange |
Callback to execute while navigating the rating items. | (hoveredValue: number) => void | () => {} | :x: |
| :green_circle: | items |
Rating items to display. | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 5 | :x: |
| :green_circle: | readOnly |
Whether to render an accessible image element or not. | boolean | false | :x: |
| :large_blue_circle: | isDisabled |
Whether to disable the radio group or not. | boolean | false | :x: |
| :large_blue_circle: | isRequired |
Whether users should be able to set rating to 0 or not. | boolean | false | :x: |
| :green_circle: | preventDefault |
Whether or not to call event.preventDefault on click and Enter/Space select. |
click | keydown | all | none |
all |
:x: |
ref, id, className, style, onBlur, onFocus are also available.
| :thinking: | Prop | Description | Type | Default | Required |
|---|---|---|---|---|---|
| :green_circle: | highlightOnlySelected |
Whether to highlight only the selected rating item or not. | boolean | false | :x: |
| :purple_circle: | halfFillMode |
Whether to half-fill the shape or the bounding box. | svg | box |
svg |
:x: |
| :green_circle: | itemStyles |
Custom shapes and colors. | ItemStyles | defaultStyles | :x: |
| :green_circle: | spaceInside |
Responsive padding of each rating item. | none | small | medium | large |
small |
:x: |
| :green_circle: | spaceBetween |
Responsive gap between rating items. | none | small | medium | large |
none |
:x: |
| :green_circle: | radius |
Responsive radius of the bounding box. | none | small | medium | large | full |
none |
:x: |
| :green_circle: | orientation |
Orientation of the rating items. | horizontal | vertical |
horizontal |
:x: |
| :large_blue_circle: | transition |
Transition to apply when hovering/selecting. | none | zoom | colors | opacity | position |
colors |
:x: |
| :thinking: | Prop | Description | Type | Default | Required |
|---|---|---|---|---|---|
| :green_circle: | invisibleLabel |
Accessible label of the rating group / image. | string | • Rating Selection (radioGroup) |
|
• Rated {value} on {items} (readOnly) |
:x: | ||||
| :large_blue_circle: | invisibleItemLabels |
Accessible labels of each rating item. | string[] | Rate 1, Rate 2... |
:x: |
| :large_blue_circle: | visibleLabelId |
DOM ID of the element used as rating group label. If set, takes precedence over invisibleLabel. |
string | undefined | :x: |
| :large_blue_circle: | visibleItemLabelIds |
DOM IDs of the elements used as labels for each rating item. If set, takes precedence over invisibleItemLabels. |
string[] | undefined | :x: |
| :large_blue_circle: | resetLabel |
Accessible label of the reset radio button. | string | Reset Rating |
:x: |
Type Definition
type RatingChange =
| React.Dispatch>
| ((selectedValue: number) => void | Promise)If your app doesn't require any custom logic/state to set the rating, you can simply pass the setter to onChange:
function App() {
const [rating, setRating] = useState(0)
return
}If you need to perform actions while setting the rating (like calling an API) or you need to update a complex state, onChange accepts a callback whose only parameter equals to the selected rating:
function App() {
const [state, setState] = useState({
review: '',
rating: 0 // Initial value
})
function handleChange(selectedValue) {
// 1. Logs the selected rating (1, 2, 3...)
console.log(selectedValue)
// 2. Do something with or without the value...
// 3. Update Rating UI
setState((prevState) => ({
...prevState,
rating: selectedValue
}))
}
return
}By default, the user is able to reset the rating (from 1-5 to 0 and vice versa):
| Interaction | Reset action | Preview |
|---|---|---|
| Mouse | By clicking again on the selected rating item | |
| Keyboard | By navigating to an invisible reset radio |
:bulb: Don't like the default focus style? Check here how to customize it.
There could be scenarios where you want to force the user to express a rating (e.g. review page, post-service rating).
In such cases, set isRequired to true:
It is not possible to reset by clicking again on the selected rating or by using the invisible radio.
It is
暂无开放 Issues,或尚未同步最近议题。