InputRating 입력 평점
사용자로부터 평점을 표시하고 수집하는 컴포넌트예요. 별의 개수·단계(0.5 단위)·색상·크기·방향 등을 조절할 수 있고, 지우기·호버 미리보기·읽기 전용·비활성화를 지원해요.
출처: Nuxt UI
본문
사용자로부터 평점을 표시하고 수집하는 컴포넌트예요.
사용법 (Usage)
v-model 지시자로 InputRating 컴포넌트의 평점 값을 제어해요.
<script setup lang="ts">
const value = ref(3)
</script>
<template>
<UInputRating v-model="value" />
</template>
상태를 제어할 필요가 없을 때는 default-value prop으로 초기 값을 설정해요.
<template>
<UInputRating :default-value="3" />
</template>
Step
step prop으로 별 하나의 세분화(granularity)를 제어해요. 0.5로 설정하면 반 별 평점을 허용해요.
<template>
<UInputRating :step="0.5" :default-value="3.5" />
</template>
Length
length prop으로 별의 개수를 설정해요. 기본값은 5예요.
<template>
<UInputRating :length="10" :step="0.5" :default-value="7.5" />
</template>
Clearable
clearable prop을 사용하면 현재 선택된 값을 클릭해 평점을 지울 수 있어요. 기본값은 false예요.
<template>
<UInputRating clearable :default-value="3" />
</template>
Hoverable
hoverable prop으로 별 위에 호버할 때 평점을 미리 보여줄지 제어해요. 기본값은 false예요.
<template>
<UInputRating hoverable :default-value="3" />
</template>
Icon
icon prop으로 별에 사용하는 아이콘을 커스터마이즈할 수 있어요. 기본값은 i-lucide-star예요.
<template>
<UInputRating icon="i-lucide-heart" :default-value="4" />
</template>
Nuxt:
[!TIP] See: /docs/getting-started/integrations/icons/nuxt#theme
app.config.ts의ui.icons.star키 아래에서 기본 별 아이콘을 전역으로 커스터마이즈할 수 있어요.
Vue:
[!TIP] See: /docs/getting-started/integrations/icons/vue#theme
vite.config.ts의ui.icons.star키 아래에서 기본 별 아이콘을 전역으로 커스터마이즈할 수 있어요.
Empty Icon
empty-icon prop으로 빈 별에 사용하는 아이콘을 커스터마이즈할 수 있어요. 제공하지 않으면 icon과 같은 아이콘을 사용해요.
<template>
<UInputRating empty-icon="i-lucide-circle" icon="i-lucide-circle-check" :default-value="3" />
</template>
Color
color prop으로 채워진 별의 색상을 바꿀 수 있어요.
<template>
<UInputRating color="neutral" :default-value="4" />
</template>
Size
size prop으로 별의 크기를 바꿀 수 있어요.
<template>
<UInputRating size="xl" :default-value="4" />
</template>
Orientation
orientation prop으로 평점의 방향을 바꿀 수 있어요. 기본값은 horizontal이에요.
<template>
<UInputRating orientation="vertical" :default-value="4" />
</template>
Disabled
disabled prop으로 InputRating 컴포넌트를 비활성화할 수 있어요. 비활성화되면 컴포넌트가 불투명도를 낮추고(75%) not-allowed 커서를 표시해 상호작용할 수 없음을 알려줘요.
<template>
<UInputRating disabled :default-value="3" />
</template>
Readonly
readonly prop으로 사용자 상호작용 없이 평점만 표시할 수 있어요. disabled와 달리 일반적인 모양(전체 불투명도, 기본 커서)을 유지해요. 변경할 수 없지만 정상적으로 보여야 하는 평점을 표시할 때 사용하세요.
<template>
<UInputRating readonly :default-value="4.5" />
</template>
API
Props
/**
* Props for the InputRating component
*/
interface InputRatingProps {
/**
* The element or component this component should render as.
* @default 'div'
*/
as?: any;
/**
* The id of the rating.
*/
id?: string | undefined;
/**
* Make the rating readonly (non-interactive).
* @default false
*/
readonly?: boolean | undefined;
/**
* The icon displayed for each rating value.
* @default appConfig.ui.icons.star
*/
icon?: any;
/**
* The icon displayed for empty rating values. Defaults to `icon` when not provided.
*/
emptyIcon?: any;
/**
* @default 'primary'
*/
color?: "primary" | "secondary" | "success" | "info" | "warning" | "error" | "neutral" | undefined;
/**
* @default 'md'
*/
size?: "xs" | "sm" | "md" | "lg" | "xl" | undefined;
/**
* The orientation of the rating.
* @default 'horizontal'
*/
orientation?: "horizontal" | "vertical" | undefined;
ui?: { root?: SlotClass; item?: SlotClass; indicator?: SlotClass; icon?: SlotClass; emptyIcon?: SlotClass; } | undefined;
/**
* The granularity each rating item is divided into.
* @default 1
*/
step?: 1 | 0.5 | 0.25 | 0.1 | undefined;
/**
* When `true`, prevents the user from interacting with radio items.
*/
disabled?: boolean | undefined;
/**
* When `true`, indicates that the user must set the value before the owning form can be submitted.
*/
required?: boolean | undefined;
/**
* The name of the field. Submitted with its owning form as part of a name/value pair.
*/
name?: string | undefined;
/**
* The rating value when initially rendered. Use when you do not need to control the state of the rating.
* @default 0
*/
defaultValue?: number | undefined;
/**
* The controlled rating value. Can be bound with `v-model`.
*/
modelValue?: number | undefined;
/**
* The number of rating items to render.
* @default 5
*/
length?: number | undefined;
/**
* When `true`, clicking the currently selected rating resets the value to `0`.
* @default false
*/
clearable?: boolean | undefined;
/**
* When `true`, the rating previews the value under the pointer on hover.
* @default false
*/
hoverable?: boolean | undefined;
}
Slots
/**
* Slots for the InputRating component
*/
interface InputRatingSlots {
/**
* Rendered for each item. `filled` is `false` for the empty background layer and `true` for the highlighted overlay.
*/
item(): any;
}
Emits
/**
* Emitted events for the InputRating component
*/
interface InputRatingEmits {
update:modelValue: (payload: [payload: number]) => void;
change: (payload: [event: Event]) => void;
}
테마 (Theme)
export default defineAppConfig({
ui: {
inputRating: {
slots: {
root: '',
item: [
'relative inline-block cursor-pointer select-none rounded-sm has-focus-visible:outline-3',
'transition'
],
indicator: 'absolute inset-0 overflow-hidden outline-none text-transparent w-(--reka-rating-item-step-width) opacity-(--reka-rating-item-step-opacity) z-(--reka-rating-item-step-z-index)',
icon: 'block',
emptyIcon: 'block w-full h-full text-muted pointer-events-none'
},
variants: {
orientation: {
horizontal: {
root: 'inline-flex items-center gap-0.5'
},
vertical: {
root: 'inline-flex flex-col items-center gap-0.5'
}
},
size: {
xs: {
item: 'size-3',
icon: 'size-3'
},
sm: {
item: 'size-4',
icon: 'size-4'
},
md: {
item: 'size-5',
icon: 'size-5'
},
lg: {
item: 'size-6',
icon: 'size-6'
},
xl: {
item: 'size-7',
icon: 'size-7'
}
},
color: {
primary: {
indicator: 'data-[state=active]:text-primary',
item: 'outline-primary/25'
},
secondary: {
indicator: 'data-[state=active]:text-secondary',
item: 'outline-secondary/25'
},
success: {
indicator: 'data-[state=active]:text-success',
item: 'outline-success/25'
},
info: {
indicator: 'data-[state=active]:text-info',
item: 'outline-info/25'
},
warning: {
indicator: 'data-[state=active]:text-warning',
item: 'outline-warning/25'
},
error: {
indicator: 'data-[state=active]:text-error',
item: 'outline-error/25'
},
neutral: {
indicator: 'data-[state=active]:text-highlighted',
item: 'outline-inverted/25'
}
},
readonly: {
true: {
root: 'cursor-default',
item: 'cursor-default'
},
false: {}
},
disabled: {
true: {
root: 'opacity-75 cursor-not-allowed',
item: 'cursor-not-allowed pointer-events-none'
},
false: {}
}
},
compoundVariants: [
{
readonly: false,
disabled: false,
class: {
item: 'hover:scale-110'
}
}
],
defaultVariants: {
color: 'primary',
size: 'md'
}
}
}
})