Vue Rating 컴포넌트
Rating은 별 기반 선택 입력 컴포넌트예요. 사용자가 1점부터 N점까지 별을 클릭해 평점을 남길 때 사용해요.
출처: 문서
본문
Vue Rating Component
Rating component is a star based selection input.
Import
import Rating from 'primevue/rating';
Accessibility
Screen Reader Rating 컴포넌트는 내부적으로 스크린 리더에만 보이는 radio 버튼을 사용해요. 각 항목을 읽을 값은 로케일 API의 aria 프로퍼티에서 star와 stars로 가져와요. Keyboard Support 키보드 상호작용은 브라우저가 그룹 내 radio 버튼을 처리하는 방식을 따라가요. Key Function tab 값을 나타내는 별로 포커스를 옮기고, 없으면 첫 번째 별이 포커스를 받아요. left arrow up arrow 이전 별로 포커스를 옮기고, 없으면 마지막 radio 버튼이 포커스를 받아요. right arrow down arrow 다음 별로 포커스를 옮기고, 없으면 첫 번째 별이 포커스를 받아요. space 포커스된 별이 값을 나타내지 않는다면 값을 그 별의 값으로 바꿔요.
Basic
Rating은 양방향 값 바인딩을 위해 v-model 프로퍼티와 함께 사용해요.
<Rating v-model="value" />
Disabled
disabled가 있으면 Knob와 상호작용할 수 없다는 시각적 힌트가 적용돼요.
<Rating v-model="value" disabled />
Composition API Example
<template>
<div class="card flex justify-center">
<Rating v-model="value" disabled />
</div>
</template>
<script setup>
import { ref } from 'vue';
const value = ref(5);
<\/script>
Forms
Rating은 PrimeVue Forms 라이브러리와 매끄럽게 통합돼요.
<Form v-slot="$form" :resolver="resolver" :initialValues="initialValues" @submit="onFormSubmit" class="flex flex-col gap-4 w-40">
<div class="flex flex-col items-center gap-2">
<Rating name="rating" />
<Message v-if="$form.rating?.invalid" severity="error" size="small" variant="simple">{{ $form.rating.error?.message }}</Message>
</div>
<Button type="submit" severity="secondary" label="Submit" />
</Form>
Composition API Example
<template>
<div class="card flex justify-center">
<Form v-slot="$form" :resolver="resolver" :initialValues="initialValues" @submit="onFormSubmit" class="flex flex-col gap-4 w-40">
<div class="flex flex-col items-center gap-2">
<Rating name="rating" />
<Message v-if="$form.rating?.invalid" severity="error" size="small" variant="simple">{{ $form.rating.error?.message }}</Message>
</div>
<Button type="submit" severity="secondary" label="Submit" />
</Form>
</div>
</template>
<script setup>
import { ref } from 'vue';
import { zodResolver } from '@primevue/forms/resolvers/zod';
import { useToast } from "primevue/usetoast";
import { z } from 'zod';
const toast = useToast();
const initialValues = ref({
rating: null
});
const resolver = ref(zodResolver(
z.object({
rating: z.union([z.number(), z.literal(null)]).refine((value) => value !== null, { message: 'Rating is required.' })
})
));
const onFormSubmit = ({ valid }) => {
if (valid) {
toast.add({ severity: 'success', summary: 'Form is submitted.', life: 3000 });
}
};
<\/script>
Number of Stars
표시할 별의 개수는 stars 프로퍼티로 정의해요.
<Rating v-model="value" :stars="10" />
Composition API Example
<template>
<div class="card flex justify-center">
<Rating v-model="value" :stars="10" />
</div>
</template>
<script setup>
import { ref } from 'vue';
const value = ref(5);
<\/script>
ReadOnly
readOnly가 있으면 값을 수정할 수 없어요.
<Rating v-model="value" readonly />
Composition API Example
<template>
<div class="card flex justify-center">
<Rating v-model="value" readonly />
</div>
</template>
<script setup>
import { ref } from 'vue';
const value = ref(3);
<\/script>
Template
커스텀 아이콘은 onicon, officon, cancelicon 슬롯으로 기본 아이콘을 덮어써요.
<Rating v-model="value">
<template #onicon>
<img src="/images/rating/custom-onicon.png" height="24" width="24" />
</template>
<template #officon>
<img src="/images/rating/custom-officon.png" height="24" width="24" />
</template>
</Rating>
Composition API Example
<template>
<div class="card flex justify-center">
<Rating v-model="value">
<template #onicon>
<img src="https://primefaces.org/cdn/primevue/images/rating/custom-onicon.png" height="24" width="24" />
</template>
<template #officon>
<img src="https://primefaces.org/cdn/primevue/images/rating/custom-officon.png" height="24" width="24" />
</template>
</Rating>
</div>
</template>
<script setup>
import { ref } from 'vue';
const value = ref(null);
<\/script>
Rating
Props
| Name | Type | Default | Description |
|---|---|---|---|
| modelValue | number | - | Value of the rating. |
| defaultValue | number | - | The default value for the input when not controlled by modelValue . |
| name | string | - | Name of the element. |
| invalid | boolean | false | When present, it specifies that the component should have invalid state style. |
| 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. |
| stars | number | 5 | Number of stars. |
| onIcon | string | - | Icon for the on state. |
| offIcon | string | - | Icon for the off state. |
| formControl | Record<string, any> | - | Form control object, typically used for handling validation and form state. |
| dt | any | - | It generates scoped CSS variables using design tokens for the component. |
| pt | PassThrough | - | Used to pass attributes to DOM elements inside the component. |
| ptOptions | any | - | Used to configure passthrough(pt) options of the component. |
| unstyled | boolean | false | When enabled, it removes component related styles in the core. |
Pass Through Options
| Name | Type | Description |
|---|---|---|
| root | RatingPassThroughOptionType | Used to pass attributes to the root's DOM element. |
| option | RatingPassThroughOptionType | Used to pass attributes to the option's DOM element. |
| onIcon | RatingPassThroughOptionType | Used to pass attributes to the on icon's DOM element. |
| offIcon | RatingPassThroughOptionType | Used to pass attributes to the off icon's DOM element. |
| hiddenOptionInputContainer | RatingPassThroughOptionType | Used to pass attributes to the hidden option input container's DOM element. |
| hiddenOptionInput | RatingPassThroughOptionType | Used to pass attributes to the hidden option input's DOM element. |
| hooks | any | Used to manage all lifecycle hooks. |
Theming
CSS Classes
| Class | 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 |