跳转到内容

Rating API

API documentation for the React Rating component. Learn about the available props, and the CSS API.

Import

import Rating from '@material-ui/core/Rating';
// 或
import { Rating } from '@material-ui/core';
You can learn about the difference by reading this guide on minimizing bundle size.

Component name

The name MuiRating can be used when providing default props or style overrides in the theme.

属性

名称类型默认值描述
classesobjectOverride or extend the styles applied to the component. See CSS API below for more details.
defaultValuenumbernullThe default input value. Use when the component is not controlled.
disabledboolfalse如果被设置为 true,那么该组件将会被禁用。
emptyIconnode<StarBorder fontSize="inherit" />The icon to display when empty.
emptyLabelTextnode'Empty'The label read when the rating input is empty.
getLabelTextfuncfunction defaultLabelText(value) { return `${value} Star${value !== 1 ? 's' : ''}`; }Accepts a function which returns a string value that provides a user-friendly name for the current value of the rating.
For localization purposes, you can use the provided translations.

Signature:
function(value: number) => string
value: The rating label's value to format.
highlightSelectedOnlyboolfalse
iconnode<Star fontSize="inherit" />The icon element.
IconContainerComponentelementTypefunction IconContainer(props) { const { value, ...other } = props; return <span {...other} />; }The component containing the icon.
maxnumber5Maximum rating.
namestringThe name attribute of the radio input elements. This input name should be unique within the page. Being unique within a form is insufficient since the name is used to generated IDs.
onChangefuncCallback fired when the value changes.

Signature:
function(event: object, value: number) => void
event: The event source of the callback.
value: The new value.
onChangeActivefuncCallback function that is fired when the hover state changes.

Signature:
function(event: object, value: number) => void
event: The event source of the callback.
value: The new value.
precisionnumber1The minimum increment value change allowed.
readOnlyboolfalseRemoves all hover effects and pointer events.
size'large'
| 'medium'
| 'small'
| string
'medium'The size of the autocomplete.
sxobject
valuenumberThe rating value.

ref 则会被传递到根元素中。
Any other props supplied will be provided to the root element (native element).

CSS

Rule nameGlobal class描述
root.MuiRating-rootStyles applied to the root element.
sizeSmall.MuiRating-sizeSmallStyles applied to the root element if size="small".
sizeMedium.MuiRating-sizeMedium
sizeLarge.MuiRating-sizeLargeStyles applied to the root element if size="large".
readOnly.MuiRating-readOnlyStyles applied to the root element if readOnly={true}.
disabled.Mui-disabledPseudo-class applied to the root element if disabled={true}.
focusVisible.Mui-focusVisiblePseudo-class applied to the root element if keyboard focused.
visuallyHidden.MuiRating-visuallyHiddenVisually hide an element.
label.MuiRating-labelStyles applied to the label elements.
labelEmptyValueActive.MuiRating-labelEmptyValueActiveStyles applied to the label of the "no value" input when it is active.
icon.MuiRating-iconStyles applied to the icon wrapping elements.
iconEmpty.MuiRating-iconEmptyStyles applied to the icon wrapping elements when 空白的文本框.
iconFilled.MuiRating-iconFilledStyles applied to the icon wrapping elements when filled(填充).
iconHover.MuiRating-iconHoverStyles applied to the icon wrapping elements when hover.
iconFocus.MuiRating-iconFocusStyles applied to the icon wrapping elements when focus.
iconActive.MuiRating-iconActiveStyles applied to the icon wrapping elements when active.
decimal.MuiRating-decimalStyles applied to the icon wrapping elements when decimals are necessary.

You can override the style of the component using one of these customization options:

Demos