본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

Rating — 별 기반 선택 입력 컴포넌트

원문 보기 위키 갱신

Rating은 별을 눌러 점수를 매기는 선택 입력 컴포넌트예요. 반 개 별점, 별 개수 조절, 세로 배치, 커스텀 아이콘, 이모지 표현까지 지원해서 평점 UI를 유연하게 만들 수 있어요.

출처: 문서

본문

Rating 컴포넌트는 별 기반 선택 입력(star based selection input)이에요.

사용법 (Usage)

import { Rating } from '@primereact/ui/rating';
<Rating.Root>
    <Rating.Option>
        <Rating.On />
        <Rating.Off />
    </Rating.Option>
</Rating.Root>

예시

기본 (Basic)

숫자 척도 위에 별점으로 사용자 피드백을 받아요.

반 개 별점 (Half Stars)

allowHalf 속성으로 반 개 별점을 허용할 수 있어요.

제어 (Controlled)

onValueChange를 사용해 값 변경을 감지해요.

별 개수 (Number of Stars)

표시할 별의 개수는 stars 속성으로 정해요.

세로 (Vertical)

orientation="vertical"을 사용하면 별점을 세로로 표시할 수 있어요.

템플릿 (Template)

onIcon과 offIcon 속성으로 기본 아이콘을 커스텀 아이콘으로 대체할 수 있어요.

이모지 (Emoji)

data-checked 속성이 있는 이모지를 사용해, 다른 항목은 회색조로 두고 선택된 항목만 강조할 수 있어요.

읽기 전용 (ReadOnly)

readOnly가 있으면 값을 편집할 수 없어요.

비활성화 (Disabled)

disabled가 있으면 값을 편집할 수 없어요.

접근성 (Accessibility)

스크린 리더 (Screen Reader)

Rating 컴포넌트는 내부적으로 스크린 리더에만 보이는 라디오 버튼을 사용해요. 각 항목에 대해 읽을 값은 locale API의 aria 속성에 있는 star와 stars에서 가져와요.

키보드 지원 (Keyboard Support)

키보드 상호작용은 그룹 안의 라디오 버튼에 대한 네이티브 브라우저 처리를 따르는 방식이에요.

Key Function
tab Moves focus to the star representing the value, if there is none then first star receives the focus.
left arrow up arrow Moves focus to the previous star, if there is none then last radio button receives the focus.
right arrow down arrow Moves focus to the next star, if there is none then first star receives the focus.
space If the focused star does not represent the value, changes the value to the star value.

Rating API

Rating 컴포넌트의 API 문서예요.

RatingRoot

Props

Name Type Default Description
ref Ref<unknown> null The reference to the component instance.
pIf boolean true Whether the component should be rendered.
style CSSProperties \| ((instance?: RatingRootInstance) => undefined \| CSSProperties) null The style to apply to the component.
className string \| ((instance?: RatingRootInstance) => undefined \| string) null The class name to apply to the component.
as null \| string \| number \| bigint \| boolean \| ComponentClass<any, any> \| FunctionComponent<any> \| ReactElement<unknown, string \| JSXElementConstructor<any>> \| Iterable<ReactNode, any, any> \| ReactPortal \| Promise<AwaitedReactNode> null The component type to render.
asChild boolean false Whether the component should be rendered as a child component.
instance RatingRootInstance null The instance to pass to the component.
pt SafeRecord<RatingRootPassThrough> null The pass-through props to pass to the component.
ptOptions PassThroughOptions null The pass-through options to pass to the component.
unstyled boolean null Whether the component should be rendered without classes.
dt unknown null The design token to use for the component.
styles StylesOptions<ComponentInstance> null The styles to use for the component.
render (instance: RatingRootInstance) => ReactNode null The render function to render the component with instance access.
children any null The children to render.

Accepts React.ReactNode for static content or a render function (instance: I) => React.ReactNode for instance access. Typed as any to avoid JSX type errors when used directly in templates. | | value | number | null | Value of the rating. | | defaultValue | number | 0 | The default value of the rating. | | allowHalf | boolean | true | Whether to allow half stars. | | name | string | null | Name used for the hidden radio inputs. Auto-generated if not provided. | | orientation | "horizontal" \| "vertical" | 'horizontal' | The orientation of the rating. | | disabled | boolean | false | When present, it specifies that the element should be disabled. | | readOnly | boolean | false | When present, it specifies that component is read-only. | | invalid | boolean | false | When present, it specifies that the component should have invalid state style. | | onValueChange | (event: UseRatingValueChangeEvent) => void | null | Callback fired when the value changes. | | [key: string] | any | null | | | pt-{optionName}-* | - | null | Pass through attributes for customizing component. For more info, see Pass Through tab. |

State

Name Type Default Description
value number null The current value of the rating.
hovering boolean null Whether the rating is in a hover state.
hoveringValue number null The value currently being hovered over.

Exposes

Name Type Default Description
state UseRatingState null Current state of the rating.
inputName string null The computed name used for radio inputs (from props.name or auto-generated).
Named 'inputName' to avoid collision with withComponent's common.name.
rootProps UseRatingRootProps null Pre-built props for the root element (data attributes + event handlers).
updateValue (event: SyntheticEvent, value: number) => void null Update the rating value.
setHoveringValue (value: number) => void null Set the hover value (called by On/Off on pointer move).
resolvePointerValue (e: MouseEvent<Element, MouseEvent> \| PointerEvent<Element>, optionEl: null \| Element, idx: number) => number null Resolves the value to set from a pointer event, taking allowHalf and element position into account.
optionProps UseRatingRootProps null Pre-built data attribute props for the option element.
onProps UseRatingRootProps null Pre-built data attribute props for the on-icon element.
offProps UseRatingRootProps null Pre-built data attribute props for the off-icon element.

Interfaces

Label Description Data
PassThroughOptions Defines passthrough(pt) options of Rating component. [object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

Types

Label Description Data
Instance Instance of Rating component. [object Object]

RatingOption

Props

Name Type Default Description
ref Ref<unknown> null The reference to the component instance.
pIf boolean true Whether the component should be rendered.
style CSSProperties \| ((instance?: RatingOptionInstance) => undefined \| CSSProperties) null The style to apply to the component.
className string \| ((instance?: RatingOptionInstance) => undefined \| string) null The class name to apply to the component.
as null \| string \| number \| bigint \| boolean \| ComponentClass<any, any> \| FunctionComponent<any> \| ReactElement<unknown, string \| JSXElementConstructor<any>> \| Iterable<ReactNode, any, any> \| ReactPortal \| Promise<AwaitedReactNode> null The component type to render.
asChild boolean false Whether the component should be rendered as a child component.
instance RatingOptionInstance null The instance to pass to the component.
pt SafeRecord<RatingOptionPassThrough> null The pass-through props to pass to the component.
ptOptions PassThroughOptions null The pass-through options to pass to the component.
unstyled boolean null Whether the component should be rendered without classes.
dt unknown null The design token to use for the component.
styles StylesOptions<ComponentInstance> null The styles to use for the component.
render (instance: RatingOptionInstance) => ReactNode null The render function to render the component with instance access.
children any null The children to render.

Accepts React.ReactNode for static content or a render function (instance: I) => React.ReactNode for instance access. Typed as any to avoid JSX type errors when used directly in templates. | | index | number | null | The zero-based index of this option within the rating. | | [key: string] | any | null | | | pt-{optionName}-* | - | null | Pass through attributes for customizing component. For more info, see Pass Through tab. |

Exposes

Name Type Default Description
state UseRatingOptionState null Current state of this option.
optionRef RefObject<null \| Element> null Ref to the option's root DOM element, used internally for pointer position calculations.
optionProps Record<string, string \| number> null Pre-built data attribute props for the option element.
onIconProps { onMouseDown: (e: MouseEvent) => void; onPointerMove: (e: PointerEvent) => void; onClick: (e: MouseEvent) => void } null Pre-built interaction props for the on-icon element.
offIconProps { onMouseDown: (e: MouseEvent) => void; onPointerMove: (e: PointerEvent) => void; onClick: (e: MouseEvent) => void } null Pre-built interaction props for the off-icon element.
onPointerMove (e: PointerEvent) => void null Handle pointer move on On/Off elements (resolves half/full value and sets hovering value).
onClick (e: MouseEvent) => void null Handle click on On/Off elements (resolves half/full value and updates value).
onMouseDown (e: MouseEvent) => void null Prevent default on mouse down (avoids focus shift).
halfInputProps null \| InputHTMLAttributes<HTMLInputElement> null Pre-built props for the half-value sr-only radio input. Null when allowHalf is false.
fullInputProps InputHTMLAttributes<HTMLInputElement> null Pre-built props for the full-value sr-only radio input.
rating undefined \| null \| RatingRootInstance null The parent Rating component instance.

Interfaces

Label Description Data
PassThroughOptions Defines passthrough(pt) options of RatingOption component. [object Object]

Types

Label Description Data
Instance Instance of RatingOption component. [object Object]

useRating

Props

State

Exposes

Interfaces

Events

Id Label Description RelatedProp Data
api.userating.events.UseRatingValueChangeEvent UseRatingValueChangeEvent Event fired when the rating value changes. [object Object],[object Object]

Types

테마 (Theming)

CSS 클래스 (CSS Classes)

styled 모드에서 사용하는 클래스 이름 목록이에요.

ClassName Description
p-rating Class name of the root element
p-rating-option Class name of the option element
p-rating-on-icon Class name of the on icon element
p-rating-off-icon Class name of the off icon element

디자인 토큰 (Design Tokens)

디자인 토큰 목록이에요.

Token CSS Variable Description
rating.gap --p-rating-gap Gap of root
rating.transition.duration --p-rating-transition-duration Transition duration of root
rating.focus.ring.width --p-rating-focus-ring-width Focus ring width of root
rating.focus.ring.style --p-rating-focus-ring-style Focus ring style of root
rating.focus.ring.color --p-rating-focus-ring-color Focus ring color of root
rating.focus.ring.offset --p-rating-focus-ring-offset Focus ring offset of root
rating.focus.ring.shadow --p-rating-focus-ring-shadow Focus ring shadow of root
rating.icon.size --p-rating-icon-size Size of icon
rating.icon.color --p-rating-icon-color Color of icon
rating.icon.hover.color --p-rating-icon-hover-color Hover color of icon
rating.icon.active.color --p-rating-icon-active-color Active color of icon

더 알아보기 (Learn more)