Rating
별 기반 선택 입력을 위한 복합 구성(compound composition)을 가진 스타일 없고 접근 가능한 평점(rating) 컴포넌트예요.
출처: 문서
본문
특징 (Features)
- 네 개의 서브 컴포넌트(
Root,Option,On,Off)를 가진 복합 컴포넌트 API allowHalfprop으로 반쪽 별 지원- 가로 및 세로 방향
- 제어 및 비제어 값 관리
사용법 (Usage)
import { Rating } from 'primereact/rating';
<Rating.Root defaultValue={3}>
{Array(5)
.fill(null)
.map((_, i) => (
<Rating.Option key={i} index={i}>
<Rating.On>
<StarFill />
</Rating.On>
<Rating.Off>
<Star />
</Rating.Off>
</Rating.Option>
))}
</Rating.Root>
동작 (Behavior)
다형 렌더링 (Polymorphic Rendering)
어떤 서브 컴포넌트든 as를 사용해 렌더링되는 HTML 요소를 바꿀 수 있어요.
<Rating.Root as="div">
<Rating.Option as="div" index={0}>
<Rating.On as="div" />
<Rating.Off as="div" />
</Rating.Option>
</Rating.Root>
기본 요소: 모든 서브 컴포넌트가 span.
렌더 함수 자식 (Render Function Children)
Rating.On과 Rating.Off는 자식으로 렌더 함수를 받아 컴포넌트 인스턴스에 접근할 수 있게 해줘요.
<Rating.On>{(instance) => <span>{instance.ratingOption?.state.highlighted ? 'filled' : ''}</span>}</Rating.On>
API
RatingRoot
| Attribute | Value |
|---|---|
data-scope |
"rating" |
data-part |
"root" |
data-orientation |
"horizontal" or "vertical" |
data-disabled |
Present when disabled |
data-readonly |
Present when read-only |
data-invalid |
Present when invalid |
RatingOption
| Attribute | Value |
|---|---|
data-scope |
"rating" |
data-part |
"option" |
data-orientation |
"horizontal" or "vertical" |
data-index |
Zero-based option index |
data-highlighted |
Present when filled |
data-half |
Present when half-filled |
data-checked |
Present when active |
data-disabled |
Present when disabled |
data-readonly |
Present when read-only |
RatingOn
| Attribute | Value |
|---|---|
data-scope |
"rating" |
data-part |
"option-on" |
data-orientation |
"horizontal" or "vertical" |
data-disabled |
Present when disabled |
data-readonly |
Present when read-only |
RatingOff
| Attribute | Value |
|---|---|
data-scope |
"rating" |
data-part |
"option-off" |
data-orientation |
"horizontal" or "vertical" |
data-disabled |
Present when disabled |
data-readonly |
Present when read-only |
접근성 (Accessibility)
스크린 리더 (Screen Reader)
Rating.Option은 라디오 그룹을 이루는 숨겨진 네이티브 <input type="radio"> 요소를 렌더링해요. 스크린 리더는 각 옵션을 값과 함께 라디오 버튼으로 알려줘요. allowHalf가 활성화되면 옵션마다 반값과 전체값 라디오가 모두 렌더링돼요.
키보드 지원 (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. |
right arrow down arrow |
Moves focus to the next star. |
space |
If the focused star does not represent the value, changes the value to the star value. |