Select 컴포넌트 (Vue Select Component)
Select는 옵션 컬렉션에서 항목을 하나 선택할 때 사용하는 PrimeVue 컴포넌트예요. 기본 사용법부터 필터, 그룹, 가상 스크롤 같은 주요 기능과 API 옵션을 차근차근 살펴봐요.
출처: 문서
본문
가져오기 (Import)
import Select from 'primevue/select';
접근성 (Accessibility)
스크린 리더 (Screen Reader)
컴포넌트를 설명하는 값은 aria-labelledby 또는 aria-label 프롭으로 제공할 수 있어요. select 요소는 aria-haspopup, aria-expanded 속성에 더해 combobox 역할(role)을 가지고, editable 옵션이 활성화되어 있으면 aria-autocomplete도 함께 추가돼요. combobox와 팝업 사이의 관계는 aria-controls로 만들어지고, 팝업 목록 안에서 키보드로 이동할 때 어떤 옵션을 읽어야 하는지는 aria-activedescendant 속성으로 스크린 리더에 알려줘요. 팝업 목록은 combobox 요소의 aria-controls 속성이 참조하는 id를 가지며, 역할로는 listbox를 사용해요. 각 목록 항목은 option 역할과 입력 요소의 aria-activedescendant와 일치하는 id, 그리고 aria-label, aria-selected, aria-disabled 속성을 가져요. 필터링이 활성화되어 있으면 filterInputProps를 정의해 필터 입력 요소에 aria-* 프롭을 전달할 수 있어요.
닫힌 상태 키보드 지원 (Closed State Keyboard Support)
| 키 | 기능 |
|---|---|
| tab | select 요소로 포커스를 이동해요. |
| space | 팝업을 열고 시각 포커스를 선택된 옵션으로 옮겨요. 선택된 옵션이 없으면 첫 번째 옵션이 포커스를 받아요. |
| enter | 팝업을 열고 시각 포커스를 선택된 옵션으로 옮겨요. 선택된 옵션이 없으면 첫 번째 옵션이 포커스를 받아요. |
| down arrow | 팝업을 열고 시각 포커스를 선택된 옵션으로 옮겨요. 선택된 옵션이 없으면 첫 번째 옵션이 포커스를 받아요. |
| up arrow | 팝업을 열고 시각 포커스를 선택된 옵션으로 옮겨요. 선택된 옵션이 없으면 마지막 옵션이 포커스를 받아요. |
| any printable character | 팝업을 열고 입력 중인 문자로 시작하는 라벨의 옵션으로 포커스를 옮겨요. 해당 옵션이 없고 select가 editable이 아니면 첫 번째 옵션이 포커스를 받아요. |
팝업 키보드 지원 (Popup Keyboard Support)
| 키 | 기능 |
|---|---|
| tab | 팝업 안에서 다음 포커스 가능 요소로 포커스를 옮겨요. 더 없으면 포커스 가능한 옵션이 선택되고 오버레이가 닫힌 뒤 페이지의 다음 요소로 포커스가 이동해요. |
| shift + tab | 팝업 안에서 이전 포커스 가능 요소로 포커스를 옮겨요. 더 없으면 포커스 가능한 옵션이 선택되고 오버레이가 닫힌 뒤 페이지의 다음 요소로 포커스가 이동해요. |
| enter | 포커스된 옵션을 선택하고 팝업을 닫은 뒤 select 요소로 포커스를 옮겨요. |
| space | 포커스된 옵션을 선택하고 팝업을 닫은 뒤 select 요소로 포커스를 옮겨요. |
| escape | 팝업을 닫은 뒤 select 요소로 포커스를 옮겨요. |
| down arrow | 다음 옵션으로 포커스를 옮겨요. 더 없으면 시각 포커스가 그대로 유지돼요. |
| up arrow | 이전 옵션으로 포커스를 옮겨요. 더 없으면 시각 포커스가 그대로 유지돼요. |
| alt + up arrow | 포커스된 옵션을 선택하고 팝업을 닫은 뒤 select 요소로 포커스를 옮겨요. |
| left arrow | select가 editable이면 현재 옵션의 시각 포커스를 해제하고 입력 커서를 한 글자 왼쪽으로 옮겨요. |
| right arrow | select가 editable이면 현재 옵션의 시각 포커스를 해제하고 입력 커서를 한 글자 오른쪽으로 옮겨요. |
| home | select가 editable이면 입력 커서를 끝으로 옮기고, 아니면 첫 번째 옵션으로 포커스를 옮겨요. |
| end | select가 editable이면 입력 커서를 시작으로 옮기고, 아니면 마지막 옵션으로 포커스를 옮겨요. |
| pageUp | 시각 포커스를 첫 번째 옵션으로 점프시켜요. |
| pageDown | 시각 포커스를 마지막 옵션으로 점프시켜요. |
| any printable character | select가 editable이 아니면 입력 중인 문자로 시작하는 라벨의 옵션으로 포커스를 옮겨요. |
필터 입력 키보드 지원 (Filter Input Keyboard Support)
| 키 | 기능 |
|---|---|
| down arrow | 다음 옵션으로 포커스를 옮겨요. 더 없으면 시각 포커스가 그대로 유지돼요. |
| up arrow | 이전 옵션으로 포커스를 옮겨요. 더 없으면 시각 포커스가 그대로 유지돼요. |
| left arrow | 현재 옵션의 시각 포커스를 해제하고 입력 커서를 한 글자 왼쪽으로 옮겨요. |
| right arrow | 현재 옵션의 시각 포커스를 해제하고 입력 커서를 한 글자 오른쪽으로 옮겨요. |
| home | 입력 커서를 끝으로 옮기고, 아니면 첫 번째 옵션으로 포커스를 옮겨요. |
| end | 입력 커서를 시작으로 옮기고, 아니면 마지막 옵션으로 포커스를 옮겨요. |
| enter | 팝업을 닫고 select 요소로 포커스를 옮겨요. |
| escape | 팝업을 닫고 select 요소로 포커스를 옮겨요. |
| tab | 팝업 안에서 다음 포커스 가능 요소로 포커스를 옮겨요. 더 없으면 포커스 가능한 옵션이 선택되고 오버레이가 닫힌 뒤 페이지의 다음 요소로 포커스가 이동해요. |
<span id="dd1"></span>Options</span>
<select aria-labelledby="dd1" />
<select aria-label="Options" />
기본 (Basic)
Select는 v-model 프롭으로 양방향 값 바인딩을 하고 options 컬렉션과 함께 사용해요. 옵션의 라벨과 값은 각각 optionLabel, optionValue 프롭으로 정의해요. 옵션이 문자열 배열처럼 단순한 원시 값이라면 optionLabel과 optionValue가 필요 없다는 점만 기억해 두세요.
<Select v-model="selectedCity" :options="cities" optionLabel="name" placeholder="Select a City" class="w-full md:w-56" />
체크마크 (Checkmark)
선택된 옵션을 강조하는 다른 방법으로 체크마크를 표시할 수도 있어요.
<Select v-model="selectedCity" :options="cities" optionLabel="name" placeholder="Select a City" checkmark :highlightOnSelect="false" class="w-full md:w-56" />
Composition API 예제
<template>
<div class="card flex justify-center">
<Select v-model="selectedCity" :options="cities" optionLabel="name" placeholder="Select a City" checkmark :highlightOnSelect="false" class="w-full md:w-56" />
</div>
</template>
<script setup>
import { ref } from "vue";
const selectedCity = ref();
const cities = ref([
{ name: 'New York', code: 'NY' },
{ name: 'Rome', code: 'RM' },
{ name: 'London', code: 'LDN' },
{ name: 'Istanbul', code: 'IST' },
{ name: 'Paris', code: 'PRS' }
]);
<\/script>
클리어 아이콘 (Clear Icon)
showClear가 활성화되면 Select를 초기화하는 클리어 아이콘이 추가돼요.
<Select v-model="selectedCity" :options="cities" showClear optionLabel="name" placeholder="Select a City" class="w-full md:w-56" />
Composition API 예제
<template>
<div class="card flex justify-center">
<Select v-model="selectedCity" :options="cities" showClear optionLabel="name" placeholder="Select a City" class="w-full md:w-56" />
</div>
</template>
<script setup>
import { ref } from "vue";
const selectedCity = ref();
const cities = ref([
{ name: 'New York', code: 'NY' },
{ name: 'Rome', code: 'RM' },
{ name: 'London', code: 'LDN' },
{ name: 'Istanbul', code: 'IST' },
{ name: 'Paris', code: 'PRS' }
]);
<\/script>
비활성화 (Disabled)
disabled가 있으면 요소를 편집하거나 포커스할 수 없어요.
<Select disabled placeholder="Select a City" class="w-full md:w-56" />
Composition API 예제
<template>
<div class="card flex justify-center">
<Select disabled placeholder="Select a City" class="w-full md:w-56" />
</div>
</template>
<script setup>
<\/script>
편집 가능 (Editable)
editable이 있으면 입력 필드에 직접 타이핑해서 값을 넣을 수도 있어요.
<Select v-model="selectedCity" editable :options="cities" optionLabel="name" placeholder="Select a City" class="w-full md:w-56" />
Composition API 예제
<template>
<div class="card flex justify-center">
<Select v-model="selectedCity" editable :options="cities" optionLabel="name" placeholder="Select a City" class="w-full md:w-56" />
</div>
</template>
<script setup>
import { ref } from "vue";
const selectedCity = ref();
const cities = ref([
{ name: 'New York', code: 'NY' },
{ name: 'Rome', code: 'RM' },
{ name: 'London', code: 'LDN' },
{ name: 'Istanbul', code: 'IST' },
{ name: 'Paris', code: 'PRS' }
]);
<\/script>
채움 스타일 (Filled)
variant 프롭을 filled로 지정하면 기본 outlined 스타일보다 시각적으로 더 강조된 모습으로 컴포넌트를 표시해요.
<Select v-model="selectedCity" variant="filled" :options="cities" optionLabel="name" placeholder="Select a City" class="w-full md:w-56" />
Composition API 예제
<template>
<div class="card flex justify-center">
<Select v-model="selectedCity" variant="filled" :options="cities" optionLabel="name" placeholder="Select a City" class="w-full md:w-56" />
</div>
</template>
<script setup>
import { ref } from "vue";
const selectedCity = ref();
const cities = ref([
{ name: 'New York', code: 'NY' },
{ name: 'Rome', code: 'RM' },
{ name: 'London', code: 'LDN' },
{ name: 'Istanbul', code: 'IST' },
{ name: 'Paris', code: 'PRS' }
]);
<\/script>
필터 (Filter)
Select는 filter 프롭을 추가하면 활성화되는 내장 필터링 기능을 제공해요.
<Select v-model="selectedCountry" :options="countries" filter optionLabel="name" placeholder="Select a Country" class="w-full md:w-56">
<template #value="slotProps">
<div v-if="slotProps.value" class="flex items-center">
<img :alt="slotProps.value.label" src="https://primefaces.org/cdn/primevue/images/flag/flag_placeholder.png" :class="\`mr-2 flag flag-\${slotProps.value.code.toLowerCase()}\`" style="width: 18px" />
<div>{{ slotProps.value.name }}</div>
</div>
<span v-else>
{{ slotProps.placeholder }}
</span>
</template>
<template #option="slotProps">
<div class="flex items-center">
<img :alt="slotProps.option.label" src="https://primefaces.org/cdn/primevue/images/flag/flag_placeholder.png" :class="\`mr-2 flag flag-\${slotProps.option.code.toLowerCase()}\`" style="width: 18px" />
<div>{{ slotProps.option.name }}</div>
</div>
</template>
</Select>
Composition API 예제
<template>
<div class="card flex justify-center">
<Select v-model="selectedCountry" :options="countries" filter optionLabel="name" placeholder="Select a Country" class="w-full md:w-56">
<template #value="slotProps">
<div v-if="slotProps.value" class="flex items-center">
<img :alt="slotProps.value.label" src="https://primefaces.org/cdn/primevue/images/flag/flag_placeholder.png" :class="\`mr-2 flag flag-\${slotProps.value.code.toLowerCase()}\`" style="width: 18px" />
<div>{{ slotProps.value.name }}</div>
</div>
<span v-else>
{{ slotProps.placeholder }}
</span>
</template>
<template #option="slotProps">
<div class="flex items-center">
<img :alt="slotProps.option.label" src="https://primefaces.org/cdn/primevue/images/flag/flag_placeholder.png" :class="\`mr-2 flag flag-\${slotProps.option.code.toLowerCase()}\`" style="width: 18px" />
<div>{{ slotProps.option.name }}</div>
</div>
</template>
</Select>
</div>
</template>
<script setup>
import { ref } from "vue";
const selectedCountry = ref();
const countries = ref([
{ name: 'Australia', code: 'AU' },
{ name: 'Brazil', code: 'BR' },
{ name: 'China', code: 'CN' },
{ name: 'Egypt', code: 'EG' },
{ name: 'France', code: 'FR' },
{ name: 'Germany', code: 'DE' },
{ name: 'India', code: 'IN' },
{ name: 'Japan', code: 'JP' },
{ name: 'Spain', code: 'ES' },
{ name: 'United States', code: 'US' }
]);
<\/script>
플로트 라벨 (Float Label)
포커스를 받으면 입력 필드 위로 떠오르는 플로트 라벨이 나타나요. 자세한 내용은 FloatLabel 문서를 참고하세요.
<FloatLabel class="w-full md:w-56">
<Select v-model="value1" inputId="over_label" :options="cities" optionLabel="name" class="w-full" />
<label for="over_label">Over Label</label>
</FloatLabel>
<FloatLabel class="w-full md:w-56" variant="in">
<Select v-model="value2" inputId="in_label" :options="cities" optionLabel="name" class="w-full" variant="filled" />
<label for="in_label">In Label</label>
</FloatLabel>
<FloatLabel class="w-full md:w-56" variant="on">
<Select v-model="value3" inputId="on_label" :options="cities" optionLabel="name" class="w-full" />
<label for="on_label">On Label</label>
</FloatLabel>
Composition API 예제
<template>
<div class="card flex flex-wrap justify-center items-stretch gap-4">
<FloatLabel class="w-full md:w-56">
<Select v-model="value1" inputId="over_label" :options="cities" optionLabel="name" class="w-full" />
<label for="over_label">Over Label</label>
</FloatLabel>
<FloatLabel class="w-full md:w-56" variant="in">
<Select v-model="value2" inputId="in_label" :options="cities" optionLabel="name" class="w-full" variant="filled" />
<label for="in_label">In Label</label>
</FloatLabel>
<FloatLabel class="w-full md:w-56" variant="on">
<Select v-model="value3" inputId="on_label" :options="cities" optionLabel="name" class="w-full" />
<label for="on_label">On Label</label>
</FloatLabel>
</div>
</template>
<script setup>
import { ref } from "vue";
const value1 = ref(null);
const value2 = ref(null);
const value3 = ref(null);
const cities = ref([
{ name: 'New York', code: 'NY' },
{ name: 'Rome', code: 'RM' },
{ name: 'London', code: 'LDN' },
{ name: 'Istanbul', code: 'IST' },
{ name: 'Paris', code: 'PRS' }
]);
<\/script>
플루이드 (Fluid)
fluid 프롭을 true로 설정하면 컴포넌트가 컨테이너의 전체 너비를 차지해요.
<Select v-model="selectedCity" :options="cities" optionLabel="name" placeholder="Select a City" fluid />
Composition API 예제
<template>
<div class="card">
<Select v-model="selectedCity" :options="cities" optionLabel="name" placeholder="Select a City" fluid />
</div>
</template>
<script setup>
import { ref } from "vue";
const selectedCity = ref();
const cities = ref([
{ name: 'New York', code: 'NY' },
{ name: 'Rome', code: 'RM' },
{ name: 'London', code: 'LDN' },
{ name: 'Istanbul', code: 'IST' },
{ name: 'Paris', code: 'PRS' }
]);
<\/script>
폼 (Forms)
<Form v-slot="$form" :resolver="resolver" :initialValues="initialValues" @submit="onFormSubmit" class="flex flex-col gap-4 w-full md:w-56">
<div class="flex flex-col gap-1">
<Select name="city.name" :options="cities" optionLabel="name" placeholder="Select a City" fluid />
<Message v-if="$form.city?.name?.invalid" severity="error" size="small" variant="simple">{{ $form.city.name.error?.message }}</Message>
</div>
<Button type="submit" severity="secondary" label="Submit" />
</Form>
Composition API 예제
<template>
<div class="card flex justify-center">
<Form v-slot="$form" :resolver="resolver" :initialValues="initialValues" @submit="onFormSubmit" class="flex flex-col gap-4 w-full md:w-56">
<div class="flex flex-col gap-1">
<Select name="city.name" :options="cities" optionLabel="name" placeholder="Select a City" fluid />
<Message v-if="$form.city?.name?.invalid" severity="error" size="small" variant="simple">{{ $form.city.name.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({
city: { name: '' }
});
const resolver = ref(zodResolver(
z.object({
city: z.union([
z.object({
name: z.string().min(1, 'City is required.')
}),
z.any().refine((val) => false, { message: 'City is required.' })
])
})
));
const cities = ref([
{ name: 'New York', code: 'NY' },
{ name: 'Rome', code: 'RM' },
{ name: 'London', code: 'LDN' },
{ name: 'Istanbul', code: 'IST' },
{ name: 'Paris', code: 'PRS' }
]);
const onFormSubmit = ({ valid }) => {
if (valid) {
toast.add({ severity: 'success', summary: 'Form is submitted.', life: 3000 });
}
};
<\/script>
그룹 (Group)
중첩된 데이터 구조를 제공하면 옵션을 그룹으로 묶을 수 있어요. 그룹의 라벨은 optionGroupLabel 프롭으로 정의하고, 그룹의 자식들을 가리키는 속성은 optionGroupChildren으로 지정해요.
<Select v-model="selectedCity" :options="groupedCities" optionLabel="label" optionGroupLabel="label" optionGroupChildren="items" placeholder="Select a City" class="w-full md:w-56">
<template #optiongroup="slotProps">
<div class="flex items-center">
<img :alt="slotProps.option.label" src="https://primefaces.org/cdn/primevue/images/flag/flag_placeholder.png" :class="\`mr-2 flag flag-\${slotProps.option.code.toLowerCase()}\`" style="width: 18px" />
<div>{{ slotProps.option.label }}</div>
</div>
</template>
</Select>
Composition API 예제
<template>
<div class="card flex justify-center">
<Select v-model="selectedCity" :options="groupedCities" optionLabel="label" optionGroupLabel="label" optionGroupChildren="items" placeholder="Select a City" class="w-full md:w-56">
<template #optiongroup="slotProps">
<div class="flex items-center">
<img :alt="slotProps.option.label" src="https://primefaces.org/cdn/primevue/images/flag/flag_placeholder.png" :class="\`mr-2 flag flag-\${slotProps.option.code.toLowerCase()}\`" style="width: 18px" />
<div>{{ slotProps.option.label }}</div>
</div>
</template>
</Select>
</div>
</template>
<script setup>
import { ref } from "vue";
const selectedCity = ref();
const groupedCities = ref([
{
label: 'Germany',
code: 'DE',
items: [
{ label: 'Berlin', value: 'Berlin' },
{ label: 'Frankfurt', value: 'Frankfurt' },
{ label: 'Hamburg', value: 'Hamburg' },
{ label: 'Munich', value: 'Munich' }
]
},
{
label: 'USA',
code: 'US',
items: [
{ label: 'Chicago', value: 'Chicago' },
{ label: 'Los Angeles', value: 'Los Angeles' },
{ label: 'New York', value: 'New York' },
{ label: 'San Francisco', value: 'San Francisco' }
]
},
{
label: 'Japan',
code: 'JP',
items: [
{ label: 'Kyoto', value: 'Kyoto' },
{ label: 'Osaka', value: 'Osaka' },
{ label: 'Tokyo', value: 'Tokyo' },
{ label: 'Yokohama', value: 'Yokohama' }
]
}
]);
<\/script>
Ifta 라벨 (Ifta Label)
IftaLabel은 필드 안쪽 상단에 정렬된 라벨을 만들 때 사용해요. 자세한 내용은 IftaLabel 문서를 참고하세요.
<IftaLabel>
<Select v-model="selectedCity" inputId="dd-city" :options="cities" optionLabel="name" class="w-full" variant="filled" />
<label for="dd-city">City</label>
</IftaLabel>
Composition API 예제
<template>
<div class="card flex justify-center">
<IftaLabel class="w-full md:w-56">
<Select v-model="selectedCity" inputId="dd-city" :options="cities" optionLabel="name" class="w-full" variant="filled" />
<label for="dd-city">City</label>
</IftaLabel>
</div>
</template>
<script setup>
import { ref } from "vue";
const selectedCity = ref();
const cities = ref([
{ name: 'New York', code: 'NY' },
{ name: 'Rome', code: 'RM' },
{ name: 'London', code: 'LDN' },
{ name: 'Istanbul', code: 'IST' },
{ name: 'Paris', code: 'PRS' }
]);
<\/script>
유효하지 않은 상태 (Invalid)
invalid 프롭을 사용하면 유효성 검사에 실패했을 때 invalid 상태가 표시돼요. 폼 유효성 검사 라이브러리와 연동할 때 이 스타일을 활용하면 좋아요.
<Select v-model="selectedCity1" :options="cities" optionLabel="name" placeholder="Select a City" :invalid="!selectedCity1" class="w-full md:w-56" />
<Select v-model="selectedCity2" :options="cities" optionLabel="name" placeholder="Select a City" :invalid="!selectedCity2" class="w-full md:w-56" variant="filled" />
Composition API 예제
<template>
<div class="card flex flex-wrap justify-center gap-4">
<Select v-model="selectedCity1" :options="cities" optionLabel="name" placeholder="Select a City" :invalid="!selectedCity1" class="w-full md:w-56" />
<Select v-model="selectedCity2" :options="cities" optionLabel="name" placeholder="Select a City" :invalid="!selectedCity2" class="w-full md:w-56" variant="filled" />
</div>
</template>
<script setup>
import { ref } from "vue";
const selectedCity1 = ref(null);
const selectedCity2 = ref(null);
const cities = ref([
{ name: 'New York', code: 'NY' },
{ name: 'Rome', code: 'RM' },
{ name: 'London', code: 'LDN' },
{ name: 'Istanbul', code: 'IST' },
{ name: 'Paris', code: 'PRS' }
]);
<\/script>
지연 가상 스크롤 (Lazy Virtual Scroll)
가상 스크롤은 필요할 때 항목을 동적으로 불러오는 방식도 지원해요.
<Select
v-model="selectedItem"
:options="items"
optionLabel="label"
optionValue="value"
class="w-full md:w-56"
:virtualScrollerOptions="{ lazy: true, onLazyLoad: onLazyLoad, itemSize: 38, showLoader: true, loading: loading, delay: 250 }"
placeholder="Select Item"
/>
Composition API 예제
<template>
<div class="card flex justify-center">
<Select v-model="selectedItem" :options="items" optionLabel="label" optionValue="value" class="w-full md:w-56"
:virtualScrollerOptions="{ lazy: true, onLazyLoad: onLazyLoad, itemSize: 38, showLoader: true, loading: loading, delay: 250 }" placeholder="Select Item" />
</div>
</template>
<script setup>
import { ref } from "vue";
const selectedItem = ref();
const items = ref(Array.from({ length: 100000 }));
const loading = ref(false);
const loadLazyTimeout = ref();
const onLazyLoad = (event) => {
loading.value = true;
if (loadLazyTimeout.value) {
clearTimeout(loadLazyTimeout.value);
}
//imitate delay of a backend call
loadLazyTimeout.value = setTimeout(() => {
const { first, last } = event;
const _items = [...items.value];
for (let i = first; i < last; i++) {
_items[i] = { label: \`Item #\${i}\`, value: i };
}
items.value = _items;
loading.value = false;
}, Math.random() * 1000 + 250);
}
<\/script>
로딩 상태 (Loading State)
loading 프롭으로 로딩 상태를 활성화해요.
<Select placeholder="Loading..." loading class="w-full md:w-56" />
Composition API 예제
<template>
<div class="card flex justify-center">
<Select placeholder="Loading..." loading class="w-full md:w-56" />
</div>
</template>
<script setup>
<\/script>
크기 (Sizes)
Select는 기본 크기 외에 small, large 크기를 대안으로 제공해요.
<Select v-model="value1" :options="cities" optionLabel="name" size="small" placeholder="Small" class="w-full md:w-56" />
<Select v-model="value2" :options="cities" optionLabel="name" placeholder="Normal" class="w-full md:w-56" />
<Select v-model="value3" :options="cities" optionLabel="name" size="large" placeholder="Large" class="w-full md:w-56" />
Composition API 예제
<template>
<div class="card flex flex-col items-center gap-4">
<Select v-model="value1" :options="cities" optionLabel="name" size="small" placeholder="Small" class="w-full md:w-56" />
<Select v-model="value2" :options="cities" optionLabel="name" placeholder="Normal" class="w-full md:w-56" />
<Select v-model="value3" :options="cities" optionLabel="name" size="large" placeholder="Large" class="w-full md:w-56" />
</div>
</template>
<script setup>
import { ref } from 'vue';
const value1 = ref(null);
const value2 = ref(null);
const value3 = ref(null);
const cities = ref([
{ name: 'New York', code: 'NY' },
{ name: 'Rome', code: 'RM' },
{ name: 'London', code: 'LDN' },
{ name: 'Istanbul', code: 'IST' },
{ name: 'Paris', code: 'PRS' }
]);
<\/script>
템플릿 (Template)
Select는 템플릿으로 커스터마이징할 수 있는 여러 슬롯을 제공해요.
<Select v-model="selectedCountry" :options="countries" optionLabel="name" placeholder="Select a Country" class="w-full md:w-56">
<template #value="slotProps">
<div v-if="slotProps.value" class="flex items-center">
<img :alt="slotProps.value.label" src="https://primefaces.org/cdn/primevue/images/flag/flag_placeholder.png" :class="\`mr-2 flag flag-\${slotProps.value.code.toLowerCase()}\`" style="width: 18px" />
<div>{{ slotProps.value.name }}</div>
</div>
<span v-else>
{{ slotProps.placeholder }}
</span>
</template>
<template #option="slotProps">
<div class="flex items-center">
<img :alt="slotProps.option.label" src="https://primefaces.org/cdn/primevue/images/flag/flag_placeholder.png" :class="\`mr-2 flag flag-\${slotProps.option.code.toLowerCase()}\`" style="width: 18px" />
<div>{{ slotProps.option.name }}</div>
</div>
</template>
<template #dropdownicon>
<i class="pi pi-map" />
</template>
<template #header>
<div class="font-medium p-3">Available Countries</div>
</template>
<template #footer>
<div class="p-3">
<Button label="Add New" fluid severity="secondary" variant="text" size="small" icon="pi pi-plus" />
</div>
</template>
</Select>
Composition API 예제
<template>
<div class="card flex justify-center">
<Select v-model="selectedCountry" :options="countries" optionLabel="name" placeholder="Select a Country" class="w-full md:w-56">
<template #value="slotProps">
<div v-if="slotProps.value" class="flex items-center">
<img :alt="slotProps.value.label" src="https://primefaces.org/cdn/primevue/images/flag/flag_placeholder.png" :class="\`mr-2 flag flag-\${slotProps.value.code.toLowerCase()}\`" style="width: 18px" />
<div>{{ slotProps.value.name }}</div>
</div>
<span v-else>
{{ slotProps.placeholder }}
</span>
</template>
<template #option="slotProps">
<div class="flex items-center">
<img :alt="slotProps.option.label" src="https://primefaces.org/cdn/primevue/images/flag/flag_placeholder.png" :class="\`mr-2 flag flag-\${slotProps.option.code.toLowerCase()}\`" style="width: 18px" />
<div>{{ slotProps.option.name }}</div>
</div>
</template>
<template #dropdownicon>
<i class="pi pi-map" />
</template>
<template #header>
<div class="font-medium p-3">Available Countries</div>
</template>
<template #footer>
<div class="p-3">
<Button label="Add New" fluid severity="secondary" variant="text" size="small" icon="pi pi-plus" />
</div>
</template>
</Select>
</div>
</template>
<script setup>
import { ref } from "vue";
const selectedCountry = ref();
const countries = ref([
{ name: 'Australia', code: 'AU' },
{ name: 'Brazil', code: 'BR' },
{ name: 'China', code: 'CN' },
{ name: 'Egypt', code: 'EG' },
{ name: 'France', code: 'FR' },
{ name: 'Germany', code: 'DE' },
{ name: 'India', code: 'IN' },
{ name: 'Japan', code: 'JP' },
{ name: 'Spain', code: 'ES' },
{ name: 'United States', code: 'US' }
]);
<\/script>
가상 스크롤 (Virtual Scroll)
VirtualScroller를 사용하면 이 데모의 10만 건 레코드처럼 긴 옵션 목록도 효율적으로 렌더링할 수 있어요. 설정은 virtualScrollerOptions 프롭으로 하고, 사용할 수 있는 옵션에 대한 자세한 내용은 Select가 내부적으로 사용하는 VirtualScroller 문서를 참고하세요.
<Select v-model="selectedItem" :options="items" optionLabel="label" optionValue="value" :virtualScrollerOptions="{ itemSize: 38 }" placeholder="Select Item" class="w-full md:w-56" />
Composition API 예제
<template>
<div class="card flex justify-center">
<Select v-model="selectedItem" :options="items" optionLabel="label" optionValue="value"
:virtualScrollerOptions="{ itemSize: 38 }" placeholder="Select Item" class="w-full md:w-56" />
</div>
</template>
<script setup>
import { ref } from "vue";
const selectedItem = ref();
const items = ref(Array.from({ length: 100000 }, (_, i) => ({ label: \`Item #\${i}\`, value: i })));
<\/script>
Select
프롭 (Props)
| 이름 | 타입 | 기본값 | 설명 |
|---|---|---|---|
| modelValue | any | - | 컴포넌트의 값이에요. |
| defaultValue | any | - | modelValue로 제어하지 않을 때 입력에 적용되는 기본값이에요. |
| name | string | - | 요소의 name 속성이에요. 주로 폼 제출에 사용해요. |
| options | any[] | - | 선택 가능한 옵션으로 표시할 항목 배열이에요. |
| optionLabel | string | Function | - | 옵션의 라벨로 사용할 속성 이름 또는 게터 함수예요. |
| optionValue | string | Function | - | 옵션의 값으로 사용할 속성 이름 또는 게터 함수예요. 정의하지 않으면 옵션 자체가 값이 돼요. |
| optionDisabled | string | Function | - | 옵션의 disabled 플래그로 사용할 속성 이름 또는 게터 함수예요. 정의하지 않으면 false가 기본값이에요. |
| optionGroupLabel | string | Function | - | 옵션 그룹의 라벨로 사용할 속성 이름 또는 게터 함수예요. |
| optionGroupChildren | string | Function | - | 옵션 그룹의 자식 옵션을 가리키는 속성 이름 또는 게터 함수예요. |
| scrollHeight | string | 14rem | 뷰포트 높이예요. 목록 높이가 이 값을 넘으면 스크롤바가 표시돼요. |
| filter | boolean | false | 지정하면 헤더에 필터 입력란을 표시해요. |
| filterPlaceholder | string | - | 필터 입력란이 비어 있을 때 표시할 플레이스홀더 텍스트예요. |
| filterLocale | string | - | 필터링에 사용할 로케일이에요. 기본값은 호스트 환경의 현재 로케일을 따라요. |
| filterMatchMode | HintedString<"startsWith" | "contains" | "endsWith"> | contains | 옵션을 검색할 때 사용할 필터링 알고리즘을 정의해요. |
| filterFields | string[] | - | 옵션을 필터링할 때 사용할 필드예요. 기본값은 optionLabel이에요. |
| editable | boolean | false | 있으면 미리 정의된 옵션 대신 입력 필드에 직접 값을 타이핑할 수 있어요. |
| placeholder | string | - | 선택된 옵션이 없을 때 표시할 기본 텍스트예요. |
| size | HintedString<"small" | "large"> | - | 컴포넌트의 크기를 정의해요. |
| invalid | boolean | false | 있으면 컴포넌트에 invalid 상태 스타일이 적용돼요. |
| disabled | boolean | false | 있으면 컴포넌트가 비활성화돼요. |
| variant | null | HintedString<"outlined" | "filled"> | null | 컴포넌트의 입력 변형(variant)을 지정해요. |
| dataKey | string | - | 옵션을 고유하게 식별하는 속성이에요. |
| showClear | boolean | false | 활성화하면 값을 지우는 클리어 아이콘이 표시돼요. |
| fluid | boolean | null | 활성화하면 컨테이너 너비의 100%를 차지해요. |
| inputId | string | - | |
| inputStyle | object | - | |
| inputClass | string | object | - | |
| labelId | string | - | 내부 label 요소의 식별자예요. |
| labelStyle | object | - | 라벨 필드의 인라인 스타일이에요. |
| labelClass | string | object | - | 라벨 필드의 스타일 클래스예요. |
| panelStyle | object | - | |
| panelClass | string | object | - | |
| overlayStyle | object | - | 오버레이의 인라인 스타일이에요. |
| overlayClass | string | object | - | 오버레이의 스타일 클래스예요. |
| appendTo | HTMLElement | HintedString<"body" | "self"> | body | 오버레이가 어디에 붙을지 지정하는 유효한 쿼리 셀렉터 또는 HTMLElement예요. |
| loading | boolean | false | Select가 로딩 상태인지 여부예요. |
| clearIcon | string | - | 클리어 버튼에 표시할 아이콘이에요. |
| dropdownIcon | string | - | Select에 표시할 아이콘이에요. |
| filterIcon | string | - | 필터 입력란에 표시할 아이콘이에요. |
| loadingIcon | string | - | 로딩 상태에서 표시할 아이콘이에요. |
| resetFilterOnHide | boolean | false | Select를 숨길 때 필터 값을 지워요. |
| resetFilterOnClear | boolean | false | 클리어 아이콘을 클릭할 때 필터 값을 지워요. |
| virtualScrollerOptions | any | - | virtualScroller 기능 사용 여부예요. VirtualScroller 컴포넌트의 속성을 객체 형태로 지정할 수 있어요. |
| autoOptionFocus | boolean | false | 오버레이 패널이 표시될 때 첫 번째 보이는 요소 또는 선택된 요소에 포커스를 둘지 여부예요. |
| autoFilterFocus | boolean | false | 오버레이 패널이 표시될 때 필터 요소에 포커스를 둘지 여부예요. |
| selectOnFocus | boolean | false | 활성화하면 포커스된 옵션이 선택돼요. |
| focusOnHover | boolean | true | 활성화하면 마우스가 올라간 옵션에 포커스가 놓여요. |
| highlightOnSelect | boolean | true | 선택된 옵션에 하이라이트 클래스를 추가할지 여부예요. |
| checkmark | boolean | false | 선택된 옵션을 체크마크와 함께 표시할지 여부예요. |
| filterMessage | string | '{0} results are available' | 필터링 결과가 있을 때 숨겨진 접근성 필드에 표시할 텍스트예요. 기본값은 PrimeVue 로케일 설정의 값을 따라요. |
| selectionMessage | string | '{0} items selected' | 옵션을 선택했을 때 숨겨진 접근성 필드에 표시할 텍스트예요. 기본값은 PrimeVue 로케일 설정의 값을 따라요. |
| emptySelectionMessage | string | No selected item | 선택된 옵션이 없을 때 숨겨진 접근성 필드에 표시할 텍스트예요. 기본값은 PrimeVue 로케일 설정의 값을 따라요. |
| emptyFilterMessage | string | No results found | 필터링 결과가 없을 때 표시할 텍스트예요. 기본값은 PrimeVue 로케일 설정의 값을 따라요. |
| emptyMessage | string | No available options | 사용 가능한 옵션이 없을 때 표시할 텍스트예요. 기본값은 PrimeVue 로케일 설정의 값을 따라요. |
| tabindex | string | number | - | 탭 이동 순서에서의 요소 인덱스예요. |
| ariaLabel | string | - | 대화형 요소의 라벨이 되는 문자열 값을 정의해요. |
| ariaLabelledby | string | - | 내부 입력 요소의 식별자예요. |
| formControl | Record<string, any> | - | 폼 컨트롤 객체예요. 주로 유효성 검사와 폼 상태 처리에 사용해요. |
| dt | any | - | 디자인 토큰으로 컴포넌트 범위의 CSS 변수를 생성해요. |
| pt | PassThrough<SelectPassThroughOptions> | - | 컴포넌트 내부 DOM 요소에 속성을 전달할 때 사용해요. |
| ptOptions | any | - | 컴포넌트의 패스스루(pt) 옵션을 설정할 때 사용해요. |
| unstyled | boolean | false | 활성화하면 코어에 있는 컴포넌트 관련 스타일이 제거돼요. |
패스 스루 옵션 (Pass Through Options)
| 이름 | 타입 | 설명 |
|---|---|---|
| root | SelectPassThroughOptionType | 루트 DOM 요소에 속성을 전달할 때 사용해요. |
| label | SelectPassThroughOptionType | 라벨 DOM 요소에 속성을 전달할 때 사용해요. |
| clearIcon | SelectPassThroughOptionType | 클리어 아이콘 DOM 요소에 속성을 전달할 때 사용해요. |
| dropdown | SelectPassThroughOptionType | 드롭다운 DOM 요소에 속성을 전달할 때 사용해요. |
| dropdownIcon | SelectPassThroughOptionType | 드롭다운 아이콘 DOM 요소에 속성을 전달할 때 사용해요. |
| loadingIcon | SelectPassThroughOptionType | 로딩 아이콘 DOM 요소에 속성을 전달할 때 사용해요. |
| overlay | SelectPassThroughOptionType | 오버레이 DOM 요소에 속성을 전달할 때 사용해요. |
| header | SelectPassThroughOptionType | 헤더 DOM 요소에 속성을 전달할 때 사용해요. |
| pcFilterContainer | any | IconField 컴포넌트에 속성을 전달할 때 사용해요. |
| pcFilter | any | InputText 컴포넌트에 속성을 전달할 때 사용해요. |
| pcFilterIconContainer | any | InputIcon 컴포넌트에 속성을 전달할 때 사용해요. |
| filterIcon | SelectPassThroughOptionType | 필터 아이콘 DOM 요소에 속성을 전달할 때 사용해요. |
| listContainer | SelectPassThroughOptionType | 목록 컨테이너 DOM 요소에 속성을 전달할 때 사용해요. |
| virtualScroller | any | VirtualScroller 컴포넌트에 속성을 전달할 때 사용해요. |
| list | SelectPassThroughOptionType | 목록 DOM 요소에 속성을 전달할 때 사용해요. |
| optionGroup | SelectPassThroughOptionType | 옵션 그룹 DOM 요소에 속성을 전달할 때 사용해요. |
| optionGroupLabel | SelectPassThroughOptionType | 옵션 그룹 라벨 DOM 요소에 속성을 전달할 때 사용해요. |
| option | SelectPassThroughOptionType | 옵션 DOM 요소에 속성을 전달할 때 사용해요. |
| optionLabel | SelectPassThroughOptionType | 옵션 라벨 DOM 요소에 속성을 전달할 때 사용해요. |
| optionCheckIcon | SelectPassThroughOptionType | 옵션 체크 아이콘 DOM 요소에 속성을 전달할 때 사용해요. |
| optionBlankIcon | SelectPassThroughOptionType | 옵션 빈 아이콘 DOM 요소에 속성을 전달할 때 사용해요. |
| emptyMessage | SelectPassThroughOptionType | 빈 메시지 DOM 요소에 속성을 전달할 때 사용해요. |
| hiddenFirstFocusableEl | SelectPassThroughOptionType | 숨겨진 첫 포커스 가능 요소의 DOM 요소에 속성을 전달할 때 사용해요. |
| hiddenFilterResult | SelectPassThroughOptionType | 숨겨진 필터 결과 DOM 요소에 속성을 전달할 때 사용해요. |
| hiddenEmptyMessage | SelectPassThroughOptionType | 숨겨진 빈 메시지 DOM 요소에 속성을 전달할 때 사용해요. |
| hiddenSelectedMessage | SelectPassThroughOptionType | 숨겨진 선택 메시지 DOM 요소에 속성을 전달할 때 사용해요. |
| hiddenLastFocusableEl | SelectPassThroughOptionType | 숨겨진 마지막 포커스 가능 요소의 DOM 요소에 속성을 전달할 때 사용해요. |
| hooks | any | 모든 라이프사이클 훅을 관리할 때 사용해요. |
| transition | SelectPassThroughTransitionType | Vue Transition API를 제어할 때 사용해요. |
테마 (Theming)
CSS 클래스 (CSS Classes)
| 클래스 | 설명 |
|---|---|
| p-select | 루트 요소의 클래스 이름이에요. |
| p-select-label | 라벨 요소의 클래스 이름이에요. |
| p-select-clear-icon | 클리어 아이콘 요소의 클래스 이름이에요. |
| p-select-dropdown | 드롭다운 요소의 클래스 이름이에요. |
| p-select-loading-icon | 로딩 아이콘 요소의 클래스 이름이에요. |
| p-select-dropdown-icon | 드롭다운 아이콘 요소의 클래스 이름이에요. |
| p-select-overlay | 오버레이 요소의 클래스 이름이에요. |
| p-select-header | 헤더 요소의 클래스 이름이에요. |
| p-select-filter | 필터 요소의 클래스 이름이에요. |
| p-select-list-container | 목록 컨테이너 요소의 클래스 이름이에요. |
| p-select-list | 목록 요소의 클래스 이름이에요. |
| p-select-option-group | 옵션 그룹 요소의 클래스 이름이에요. |
| p-select-option-group-label | 옵션 그룹 라벨 요소의 클래스 이름이에요. |
| p-select-option | 옵션 요소의 클래스 이름이에요. |
| p-select-option-label | 옵션 라벨 요소의 클래스 이름이에요. |
| p-select-option-check-icon | 옵션 체크 아이콘 요소의 클래스 이름이에요. |
| p-select-option-blank-icon | 옵션 빈 아이콘 요소의 클래스 이름이에요. |
| p-select-empty-message | 빈 메시지 요소의 클래스 이름이에요. |
디자인 토큰 (Design Tokens)
| 토큰 | CSS 변수 | 설명 |
|---|---|---|
| select.background | --p-select-background | 루트의 배경이에요. |
| select.disabled.background | --p-select-disabled-background | 루트의 비활성 상태 배경이에요. |
| select.filled.background | --p-select-filled-background | 루트의 filled 배경이에요. |
| select.filled.hover.background | --p-select-filled-hover-background | 루트의 filled 호버 배경이에요. |
| select.filled.focus.background | --p-select-filled-focus-background | 루트의 filled 포커스 배경이에요. |
| select.border.color | --p-select-border-color | 루트의 테두리 색이에요. |
| select.hover.border.color | --p-select-hover-border-color | 루트의 호버 테두리 색이에요. |
| select.focus.border.color | --p-select-focus-border-color | 루트의 포커스 테두리 색이에요. |
| select.invalid.border.color | --p-select-invalid-border-color | 루트의 invalid 테두리 색이에요. |
| select.color | --p-select-color | 루트의 색이에요. |
| select.disabled.color | --p-select-disabled-color | 루트의 비활성 상태 색이에요. |
| select.placeholder.color | --p-select-placeholder-color | 루트의 플레이스홀더 색이에요. |
| select.invalid.placeholder.color | --p-select-invalid-placeholder-color | 루트의 invalid 플레이스홀더 색이에요. |
| select.shadow | --p-select-shadow | 루트의 그림자예요. |
| select.padding.x | --p-select-padding-x | 루트의 가로 패딩이에요. |
| select.padding.y | --p-select-padding-y | 루트의 세로 패딩이에요. |
| select.border.radius | --p-select-border-radius | 루트의 테두리 반경이에요. |
| select.focus.ring.width | --p-select-focus-ring-width | 루트의 포커스 링 두께예요. |
| select.focus.ring.style | --p-select-focus-ring-style | 루트의 포커스 링 스타일이에요. |
| select.focus.ring.color | --p-select-focus-ring-color | 루트의 포커스 링 색이에요. |
| select.focus.ring.offset | --p-select-focus-ring-offset | 루트의 포커스 링 오프셋이에요. |
| select.focus.ring.shadow | --p-select-focus-ring-shadow | 루트의 포커스 링 그림자예요. |
| select.transition.duration | --p-select-transition-duration | 루트의 전환 지속 시간이에요. |
| select.sm.font.size | --p-select-sm-font-size | 루트의 small 폰트 크기예요. |
| select.sm.padding.x | --p-select-sm-padding-x | 루트의 small 가로 패딩이에요. |
| select.sm.padding.y | --p-select-sm-padding-y | 루트의 small 세로 패딩이에요. |
| select.lg.font.size | --p-select-lg-font-size | 루트의 large 폰트 크기예요. |
| select.lg.padding.x | --p-select-lg-padding-x | 루트의 large 가로 패딩이에요. |
| select.lg.padding.y | --p-select-lg-padding-y | 루트의 large 세로 패딩이에요. |
| select.dropdown.width | --p-select-dropdown-width | 드롭다운의 너비예요. |
| select.dropdown.color | --p-select-dropdown-color | 드롭다운의 색이에요. |
| select.overlay.background | --p-select-overlay-background | 오버레이의 배경이에요. |
| select.overlay.border.color | --p-select-overlay-border-color | 오버레이의 테두리 색이에요. |
| select.overlay.border.radius | --p-select-overlay-border-radius | 오버레이의 테두리 반경이에요. |
| select.overlay.color | --p-select-overlay-color | 오버레이의 색이에요. |
| select.overlay.shadow | --p-select-overlay-shadow | 오버레이의 그림자예요. |
| select.list.padding | --p-select-list-padding | 목록의 패딩이에요. |
| select.list.gap | --p-select-list-gap | 목록의 간격이에요. |
| select.list.header.padding | --p-select-list-header-padding | 목록 헤더의 패딩이에요. |
| select.option.focus.background | --p-select-option-focus-background | 옵션의 포커스 배경이에요. |
| select.option.selected.background | --p-select-option-selected-background | 옵션의 선택 배경이에요. |
| select.option.selected.focus.background | --p-select-option-selected-focus-background | 옵션의 선택+포커스 배경이에요. |
| select.option.color | --p-select-option-color | 옵션의 색이에요. |
| select.option.focus.color | --p-select-option-focus-color | 옵션의 포커스 색이에요. |
| select.option.selected.color | --p-select-option-selected-color | 옵션의 선택 색이에요. |
| select.option.selected.focus.color | --p-select-option-selected-focus-color | 옵션의 선택+포커스 색이에요. |
| select.option.padding | --p-select-option-padding | 옵션의 패딩이에요. |
| select.option.border.radius | --p-select-option-border-radius | 옵션의 테두리 반경이에요. |
| select.option.group.background | --p-select-option-group-background | 옵션 그룹의 배경이에요. |
| select.option.group.color | --p-select-option-group-color | 옵션 그룹의 색이에요. |
| select.option.group.font.weight | --p-select-option-group-font-weight | 옵션 그룹의 폰트 두께예요. |
| select.option.group.padding | --p-select-option-group-padding | 옵션 그룹의 패딩이에요. |
| select.clear.icon.color | --p-select-clear-icon-color | 클리어 아이콘의 색이에요. |
| select.checkmark.color | --p-select-checkmark-color | 체크마크의 색이에요. |
| select.checkmark.gutter.start | --p-select-checkmark-gutter-start | 체크마크 거터의 시작 위치예요. |
| select.checkmark.gutter.end | --p-select-checkmark-gutter-end | 체크마크 거터의 끝 위치예요. |
| select.empty.message.padding | --p-select-empty-message-padding | 빈 메시지의 패딩이에요. |
더 알아보기 (Learn more)
Select는 폼 연동이 필요하면 Form 컴포넌트와 함께 쓸 수 있고, 오버레이 위치나 스타일 조정은 Passthrough 문서를 참고하면 좋아요. 여러 항목을 고르거나 검색형 입력이 필요하다면 MultiSelect, AutoComplete 컴포넌트도 함께 살펴보세요.