Vue MultiSelect 컴포넌트
MultiSelect는 여러 항목을 담은 컬렉션에서 값을 여러 개 골라 담는 선택 컴포넌트예요. 기본 드롭다운이 하나만 고르게 하는 것과 달리, 체크박스가 붙은 팝업 목록에서 원하는 만큼 여러 개를 고를 수 있다는 점이 핵심이에요.
본문
가져오기 (Import)
MultiSelect는 별도 설치 없이 PrimeVue에서 바로 가져와 쓸 수 있어요.
import MultiSelect from 'primevue/multiselect';
접근성 (Accessibility)
스크린 리더
컴포넌트를 설명하는 값은 aria-labelledby나 aria-label props로 제공할 수 있어요. multiselect 컴포넌트에는 combobox role이 붙고, 여기에 aria-haspopup과 aria-expanded 속성이 함께 들어가요. combobox와 팝업 사이의 관계는 팝업 listbox의 id를 가리키는 aria-controls 속성으로 만들어요. 팝업 listbox는 listbox role을 쓰되 aria-multiselectable이 켜져 있어요. 목록의 각 항목은 option role과 함께 aria-label, aria-selected, aria-disabled 속성을 가져요. 헤더의 Checkbox 컴포넌트는 내부에 숨겨진 네이티브 checkbox 엘리먼트를 쓰는데, 이 엘리먼트는 스크린 리더에만 보여요. 읽어 줄 값은 locale API의 aria 속성에서 selectAll과 unselectAll 키로 정의해요. 필터링을 켜면 filterInputProps를 정의해 입력 엘리먼트에 aria-* props를 줄 수 있어요. 닫기 버튼은 기본적으로 locale API의 aria 속성에서 close 키를 aria-label로 쓰는데, 이 값은 closeButtonProps로 바꿀 수 있어요.
닫힌 상태 키보드 지원
| 키 | 동작 |
|---|---|
| tab | MultiSelect 엘리먼트로 포커스를 옮겨요. |
| space | 팝업을 열고 시각 포커스를 선택된 옵션으로 옮겨요. 선택된 옵션이 없으면 첫 번째 옵션이 포커스를 받아요. |
| enter | 팝업을 열고 시각 포커스를 선택된 옵션으로 옮겨요. 선택된 옵션이 없으면 첫 번째 옵션이 포커스를 받아요. |
| 아래 화살표 | 팝업을 열고 시각 포커스를 선택된 옵션으로 옮겨요. 선택된 옵션이 없으면 첫 번째 옵션이 포커스를 받아요. |
| 위 화살표 | 팝업을 열고 시각 포커스를 선택된 옵션으로 옮겨요. 선택된 옵션이 없으면 첫 번째 옵션이 포커스를 받아요. |
| 출력 가능한 아무 문자 | 팝업을 열고 타이핑한 문자로 시작하는 라벨의 옵션으로 포커스를 옮겨요. 해당 옵션이 없으면 첫 번째 옵션이 포커스를 받아요. |
팝업 키보드 지원
| 키 | 동작 |
|---|---|
| tab | 팝업 안의 다음 포커스 가능한 엘리먼트로 포커스를 옮겨요. 더 없으면 첫 번째 포커스 가능한 엘리먼트가 포커스를 받아요. |
| shift + tab | 팝업 안의 이전 포커스 가능한 엘리먼트로 포커스를 옮겨요. 더 없으면 마지막 포커스 가능한 엘리먼트가 포커스를 받아요. |
| enter | 포커스된 옵션의 선택 상태를 토글한 뒤 MultiSelect 엘리먼트로 포커스를 옮겨요. |
| space | 포커스된 옵션의 선택 상태를 토글한 뒤 MultiSelect 엘리먼트로 포커스를 옮겨요. |
| escape | 팝업을 닫고 MultiSelect 엘리먼트로 포커스를 옮겨요. |
| 아래 화살표 | 다음 옵션으로 포커스를 옮겨요. 더 없으면 시각 포커스는 그대로 유지돼요. |
| 위 화살표 | 이전 옵션으로 포커스를 옮겨요. 더 없으면 시각 포커스는 그대로 유지돼요. |
| alt + 위 화살표 | 포커스된 옵션을 선택하고 팝업을 닫은 뒤 MultiSelect 엘리먼트로 포커스를 옮겨요. |
| shift + 아래 화살표 | 다음 옵션으로 포커스를 옮기고 선택 상태를 토글해요. |
| shift + 위 화살표 | 이전 옵션으로 포커스를 옮기고 선택 상태를 토글해요. |
| shift + space | 가장 최근에 선택한 옵션과 포커스된 옵션 사이의 항목들을 선택해요. |
| home | 첫 번째 옵션으로 포커스를 옮겨요. |
| end | 마지막 옵션으로 포커스를 옮겨요. |
| control + shift + home | 포커스된 옵션부터 첫 번째 옵션까지 모두 선택해요. |
| control + shift + end | 포커스된 옵션부터 마지막 옵션까지 모두 선택해요. |
| control + a | 모든 옵션을 선택해요. |
| pageUp | 시각 포커스를 첫 번째 옵션으로 점프시켜요. |
| pageDown | 시각 포커스를 마지막 옵션으로 점프시켜요. |
| 출력 가능한 아무 문자 | 타이핑한 문자로 시작하는 라벨의 옵션으로 포커스를 옮겨요. |
모두 선택 체크박스 키보드 지원
| 키 | 동작 |
|---|---|
| space | 체크 상태를 토글해요. |
| escape | 팝업을 닫고 MultiSelect 엘리먼트로 포커스를 옮겨요. |
필터 입력 키보드 지원
| 키 | 동작 |
|---|---|
| 아래 화살표 | 다음 옵션으로 포커스를 옮겨요. 더 없으면 시각 포커스는 그대로 유지돼요. |
| 위 화살표 | 이전 옵션으로 포커스를 옮겨요. 더 없으면 시각 포커스는 그대로 유지돼요. |
| 왼쪽 화살표 | 현재 옵션에서 시각 포커스를 치우고 입력 커서를 한 글자 왼쪽으로 옮겨요. |
| 오른쪽 화살표 | 현재 옵션에서 시각 포커스를 치우고 입력 커서를 한 글자 오른쪽으로 옮겨요. |
| home | 커서가 이미 끝에 있지 않다면 입력 커서를 끝으로 옮기고, 그렇지 않으면 첫 번째 옵션으로 포커스를 옮겨요. |
| end | 커서가 이미 처음에 있지 않다면 입력 커서를 처음으로 옮기고, 그렇지 않으면 마지막 옵션으로 포커스를 옮겨요. |
| enter | 팝업을 닫고 MultiSelect 엘리먼트로 포커스를 옮겨요. |
| escape | 팝업을 닫고 MultiSelect 엘리먼트로 포커스를 옮겨요. |
| tab | 팝업 안의 다음 포커스 가능한 엘리먼트로 포커스를 옮겨요. 더 없으면 포커스 가능한 옵션을 선택하고 오버레이를 닫은 뒤, 페이지의 다음 엘리먼트로 포커스를 옮겨요. |
<span id="dd1"></span>Options</span>
<MultiSelect aria-labelledby="dd1" />
<MultiSelect aria-label="Options" />
기본 (Basic)
MultiSelect는 v-model prop으로 양방향 값을 묶고, options 컬렉션으로 선택지를 넘겨 줘요. 옵션의 라벨과 값은 각각 optionLabel과 optionValue props로 정의해요. 다만 옵션이 문자열 배열처럼 단순한 원시 값이면 optionLabel과 optionValue가 필요 없다는 점은 기억해 두면 좋아요.
<MultiSelect v-model="selectedCities" :options="cities" optionLabel="name" filter placeholder="Select Cities" :maxSelectedLabels="3" class="w-full md:w-80" />
칩 (Chips)
선택된 값은 기본적으로 쉼표로 구분된 목록으로 표시되는데, display를 chip으로 설정하면 칩 형태로 보여 줘요.
<MultiSelect v-model="selectedCities" display="chip" :options="cities" optionLabel="name" filter placeholder="Select Cities" :maxSelectedLabels="3" class="w-full md:w-80" />
Composition API Example
<template>
<div class="card flex justify-center">
<MultiSelect v-model="selectedCities" display="chip" :options="cities" optionLabel="name" filter placeholder="Select Cities"
:maxSelectedLabels="3" class="w-full md:w-80" />
</div>
</template>
<script setup>
import { ref } from "vue";
const selectedCities = 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를 켜면 MultiSelect를 초기화할 수 있는 지우기 아이콘이 추가돼요.
<MultiSelect v-model="selectedCities" showClear :options="cities" optionLabel="name" filter placeholder="Select Cities" :maxSelectedLabels="3" class="w-full md:w-80" />
Composition API Example
<template>
<div class="card flex justify-center">
<MultiSelect v-model="selectedCities" showClear :options="cities" optionLabel="name" filter placeholder="Select Cities" :maxSelectedLabels="3" class="w-full md:w-80" />
</div>
</template>
<script setup>
import { ref } from "vue";
const selectedCities = 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가 있으면 엘리먼트를 편집하거나 포커스할 수 없어요.
<MultiSelect disabled placeholder="Select Cities" class="w-full md:w-80" />
Composition API Example
<template>
<div class="flex justify-center">
<MultiSelect disabled placeholder="Select Cities" class="w-full md:w-80" />
</div>
</template>
<script setup>
<\/script>
채움 스타일 (Filled)
variant 속성을 filled로 지정하면 기본 outlined 스타일보다 시각적으로 강조된 모습으로 컴포넌트를 표시해요.
<MultiSelect v-model="selectedCities" variant="filled" :options="cities" optionLabel="name" filter placeholder="Select Cities" :maxSelectedLabels="3" class="w-full md:w-80" />
Composition API Example
<template>
<div class="card flex justify-center">
<MultiSelect v-model="selectedCities" variant="filled" :options="cities" optionLabel="name" filter placeholder="Select Cities"
:maxSelectedLabels="3" class="w-full md:w-80" />
</div>
</template>
<script setup>
import { ref } from "vue";
const selectedCities = 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)
필터링을 쓰면 헤더의 입력 칸으로 목록의 항목을 검색할 수 있어요. 필터링을 쓰려면 filter 속성을 켜면 돼요. 기본적으로 검색할 때 optionLabel을 사용하고, filterFields로 검색에 쓰일 필드를 직접 정할 수도 있어요. 여기에 filterMatchMode로 검색 알고리즘을 정할 수 있는데, 유효한 값은 "contains"(기본값), "startsWith", "endsWith"예요.
<MultiSelect v-model="selectedCities" :options="cities" filter optionLabel="name" placeholder="Select Cities" :maxSelectedLabels="3" class="w-full md:w-80" />
Composition API Example
<template>
<div class="card flex justify-center">
<MultiSelect v-model="selectedCities" :options="cities" filter optionLabel="name" placeholder="Select Cities"
:maxSelectedLabels="3" class="w-full md:w-80" />
</div>
</template>
<script setup>
import { ref } from "vue";
const selectedCities = 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>
플로트 라벨 (Float Label)
플로팅 라벨은 입력 칸에 포커스가 가면 입력 칸 위로 떠 오르는 라벨이에요. 자세한 내용은 FloatLabel 문서를 참고하세요.
<FloatLabel class="w-full md:w-80">
<MultiSelect id="over_label" v-model="value1" :options="cities" optionLabel="name" filter :maxSelectedLabels="3" class="w-full" />
<label for="over_label">Over Label</label>
</FloatLabel>
<FloatLabel class="w-full md:w-80" variant="in">
<MultiSelect id="in_label" v-model="value2" :options="cities" optionLabel="name" filter :maxSelectedLabels="3" class="w-full" variant="filled" />
<label for="in_label">In Label</label>
</FloatLabel>
<FloatLabel class="w-full md:w-80" variant="on">
<MultiSelect id="on_label" v-model="value3" :options="cities" optionLabel="name" filter :maxSelectedLabels="3" class="w-full" />
<label for="on_label">On Label</label>
</FloatLabel>
Composition API Example
<template>
<div class="card flex flex-wrap justify-center items-end gap-4">
<FloatLabel class="w-full md:w-80">
<MultiSelect id="over_label" v-model="value1" :options="cities" optionLabel="name" filter :maxSelectedLabels="3" class="w-full" />
<label for="over_label">Over Label</label>
</FloatLabel>
<FloatLabel class="w-full md:w-80" variant="in">
<MultiSelect id="in_label" v-model="value2" :options="cities" optionLabel="name" filter :maxSelectedLabels="3" class="w-full" variant="filled" />
<label for="in_label">In Label</label>
</FloatLabel>
<FloatLabel class="w-full md:w-80" variant="on">
<MultiSelect id="on_label" v-model="value3" :options="cities" optionLabel="name" filter :maxSelectedLabels="3" 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>
폼 (Forms)
<Form v-slot="$form" :resolver="resolver" :initialValues="initialValues" @submit="onFormSubmit" class="flex justify-center flex-col gap-4">
<div class="flex flex-col gap-1">
<MultiSelect name="city" :options="cities" optionLabel="name" filter placeholder="Select Cities" :maxSelectedLabels="3" class="w-full md:w-80" />
<Message v-if="$form.city?.invalid" severity="error" size="small" variant="simple">{{ $form.city.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 justify-center flex-col gap-4">
<div class="flex flex-col gap-1">
<MultiSelect name="city" :options="cities" optionLabel="name" filter placeholder="Select Cities" :maxSelectedLabels="3" class="w-full md:w-80" />
<Message v-if="$form.city?.invalid" severity="error" size="small" variant="simple">{{ $form.city.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: []
});
const resolver = ref(zodResolver(
z.object({
city: z
.array(
z.object({
name: z.string().min(1, 'City is required.')
})
)
.min(1, '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도 함께 필요해요.
<MultiSelect v-model="selectedCities" :options="groupedCities" optionLabel="label" filter optionGroupLabel="label" optionGroupChildren="items" display="chip" placeholder="Select Cities" class="w-full md:w-80">
<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="\`flag flag-\${slotProps.option.code.toLowerCase()} mr-2\`" style="width: 18px" />
<div>{{ slotProps.option.label }}</div>
</div>
</template>
</MultiSelect>
Composition API Example
<template>
<div class="card flex justify-center">
<MultiSelect v-model="selectedCities" :options="groupedCities" optionLabel="label" filter optionGroupLabel="label" optionGroupChildren="items" display="chip" placeholder="Select Cities" class="w-full md:w-80">
<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="\`flag flag-\${slotProps.option.code.toLowerCase()} mr-2\`" style="width: 18px" />
<div>{{ slotProps.option.label }}</div>
</div>
</template>
</MultiSelect>
</div>
</template>
<script setup>
import { ref } from "vue";
const selectedCities = 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>
<MultiSelect v-model="selectedCities" inputId="ms_cities" :options="cities" optionLabel="name" filter :maxSelectedLabels="3" class="w-full" variant="filled" />
<label for="ms_cities">Cities</label>
</IftaLabel>
Composition API Example
<template>
<div class="card flex justify-center">
<IftaLabel class="w-full md:w-80">
<MultiSelect v-model="selectedCities" inputId="ms_cities" :options="cities" optionLabel="name" filter :maxSelectedLabels="3" class="w-full"variant="filled" />
<label for="ms_cities">Cities</label>
</IftaLabel>
</div>
</template>
<script setup>
import { ref } from "vue";
const selectedCities = 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>
유효하지 않은 상태 (Invalid)
유효성 검사가 실패했음을 알리는 invalid 상태는 invalid prop으로 표시해요. 폼 유효성 검사 라이브러리와 연동할 때 이 스타일을 쓸 수 있어요.
<MultiSelect v-model="selectedCities1" :options="cities" optionLabel="name" filter placeholder="Select Cities" :maxSelectedLabels="3" :invalid="selectedCities1?.length === 0" class="w-full md:w-80" />
<MultiSelect v-model="selectedCities2" :options="cities" optionLabel="name" filter placeholder="Select Cities" :maxSelectedLabels="3" :invalid="selectedCities2?.length === 0" class="w-full md:w-80" variant="filled" />
Composition API Example
<template>
<div class="card flex flex-wrap justify-center gap-4">
<MultiSelect v-model="selectedCities1" :options="cities" optionLabel="name" filter placeholder="Select Cities"
:maxSelectedLabels="3" :invalid="selectedCities1?.length === 0" class="w-full md:w-80" />
<MultiSelect v-model="selectedCities2" :options="cities" optionLabel="name" filter placeholder="Select Cities"
:maxSelectedLabels="3" :invalid="selectedCities2?.length === 0" class="w-full md:w-80" variant="filled" />
</div>
</template>
<script setup>
import { ref } from "vue";
const selectedCities1 = ref([]);
const selectedCities2 = 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>
로딩 상태 (Loading State)
로딩 상태는 loading 속성으로 쓸 수 있어요.
<MultiSelect placeholder="Loading..." loading class="w-full md:w-80"></MultiSelect>
Composition API Example
<template>
<div class="card flex justify-center">
<MultiSelect placeholder="Loading..." loading class="w-full md:w-80"></MultiSelect>
</div>
</template>
<script setup>
<\/script>
크기 (Sizes)
MultiSelect는 기본 크기 외에 small과 large 크기를 대안으로 제공해요.
<MultiSelect v-model="value1" :options="cities" optionLabel="name" :maxSelectedLabels="3" class="w-full md:w-80" size="small" placeholder="Small" />
<MultiSelect v-model="value2" :options="cities" optionLabel="name" :maxSelectedLabels="3" class="w-full md:w-80" placeholder="Normal" />
<MultiSelect v-model="value3" :options="cities" optionLabel="name" :maxSelectedLabels="3" class="w-full md:w-80" size="large" placeholder="Large" />
Composition API Example
<template>
<div class="card flex flex-col items-center gap-4">
<MultiSelect v-model="value1" :options="cities" optionLabel="name" :maxSelectedLabels="3" class="w-full md:w-80" size="small" placeholder="Small" />
<MultiSelect v-model="value2" :options="cities" optionLabel="name" :maxSelectedLabels="3" class="w-full md:w-80" placeholder="Normal" />
<MultiSelect v-model="value3" :options="cities" optionLabel="name" :maxSelectedLabels="3" class="w-full md:w-80" size="large" placeholder="Large" />
</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)
MultiSelect는 템플릿으로 자유롭게 꾸밀 수 있도록 여러 슬롯을 제공해요.
<MultiSelect v-model="selectedCountries" :options="countries" optionLabel="name" filter placeholder="Select Countries" display="chip" class="w-full md:w-80">
<template #option="slotProps">
<div class="flex items-center">
<img :alt="slotProps.option.name" src="https://primefaces.org/cdn/primevue/images/flag/flag_placeholder.png" :class="\`flag flag-\${slotProps.option.code.toLowerCase()} mr-2\`" style="width: 18px" />
<div>{{ slotProps.option.name }}</div>
</div>
</template>
<template #dropdownicon>
<i class="pi pi-map" />
</template>
<template #filtericon>
<i class="pi pi-map-marker" />
</template>
<template #header>
<div class="font-medium px-3 py-2">Available Countries</div>
</template>
<template #footer>
<div class="p-3 flex justify-between">
<Button label="Add New" severity="secondary" variant="text" size="small" icon="pi pi-plus" />
<Button label="Remove All" severity="danger" variant="text" size="small" icon="pi pi-times" />
</div>
</template>
</MultiSelect>
Composition API Example
<template>
<div class="card flex justify-center">
<MultiSelect v-model="selectedCountries" :options="countries" optionLabel="name" filter placeholder="Select Countries" display="chip" class="w-full md:w-80">
<template #option="slotProps">
<div class="flex items-center">
<img :alt="slotProps.option.name" src="https://primefaces.org/cdn/primevue/images/flag/flag_placeholder.png" :class="\`flag flag-\${slotProps.option.code.toLowerCase()} mr-2\`" style="width: 18px" />
<div>{{ slotProps.option.name }}</div>
</div>
</template>
<template #dropdownicon>
<i class="pi pi-map" />
</template>
<template #filtericon>
<i class="pi pi-map-marker" />
</template>
<template #header>
<div class="font-medium px-3 py-2">Available Countries</div>
</template>
<template #footer>
<div class="p-3 flex justify-between">
<Button label="Add New" severity="secondary" variant="text" size="small" icon="pi pi-plus" />
<Button label="Remove All" severity="danger" variant="text" size="small" icon="pi pi-times" />
</div>
</template>
</MultiSelect>
</div>
</template>
<script setup>
import { ref } from "vue";
const selectedCountries = 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>
가상 스크롤 (VirtualScroll)
VirtualScroller를 쓰면 이 데모의 10만 건 기록처럼 긴 옵션 목록을 효율적으로 렌더링할 수 있어요. 설정은 virtualScrollerOptions 속성으로 하고, 사용 가능한 옵션에 대한 자세한 내용은 VirtualScroller 문서를 참고하세요. VirtualScroller는 MultiSelect가 내부적으로 사용하는 컴포넌트거든요.
<MultiSelect
v-model="selectedItems"
:options="items"
:maxSelectedLabels="3"
:selectAll="selectAll"
optionLabel="label"
optionValue="value"
@selectall-change="onSelectAllChange($event)"
@change="onChange($event)"
:virtualScrollerOptions="{ itemSize: 44 }"
filter
placeholder="Select Item"
class="w-full md:w-80"
/>
Composition API Example
<template>
<div class="card flex justify-center">
<MultiSelect v-model="selectedItems" :options="items" :maxSelectedLabels="3" :selectAll="selectAll" optionLabel="label" optionValue="value"
@selectall-change="onSelectAllChange($event)" @change="onChange($event)" :virtualScrollerOptions="{ itemSize: 44 }" filter placeholder="Select Item" class="w-full md:w-80" />
</div>
</template>
<script setup>
import { ref } from 'vue';
const selectedItems = ref();
const selectAll = ref(false);
const items = ref(Array.from({ length: 100000 }, (_, i) => ({ label: \`Item #\${i}\`, value: i })));
const onSelectAllChange = (event) => {
selectedItems.value = event.checked ? items.value.map((item) => item.value) : [];
selectAll.value = event.checked;
};
const onChange = (event) => {
selectAll.value = event.value.length === items.value.length;
}
<\/script>
멀티 셀렉트 (Multi Select)
Props
| 이름 | 타입 | 기본값 | 설명 |
|---|---|---|---|
| modelValue | any | - | 컴포넌트의 값이에요. |
| defaultValue | any | - | modelValue로 제어하지 않을 때 입력의 기본값이에요. |
| name | string | - | 엘리먼트의 name 속성이에요. 주로 폼 제출에 써요. |
| options | any[] | - | 선택 가능한 옵션으로 표시할 항목 배열이에요. |
| optionLabel | string | Function | - | 옵션의 라벨로 쓸 속성 이름이나 getter 함수예요. |
| optionValue | string | Function | - | 옵션의 값으로 쓸 속성 이름이나 getter 함수예요. 정의하지 않으면 옵션 자체가 값이 돼요. |
| optionDisabled | string | Function | - | 옵션의 disabled 플래그로 쓸 속성 이름이나 getter 함수예요. 정의하지 않으면 false가 기본값이에요. |
| optionGroupLabel | string | Function | - | 옵션 그룹의 라벨로 쓸 속성 이름이나 getter 함수예요. |
| optionGroupChildren | string | Function | - | 옵션 그룹의 자식 옵션을 가리키는 속성 이름이나 getter 함수예요. |
| scrollHeight | string | 14rem | 뷰포트 높이예요. 목록 높이가 이 값을 넘으면 스크롤바가 생겨요. |
| placeholder | string | - | 아무것도 선택하지 않았을 때 표시할 라벨이에요. |
| size | HintedString<"small" | "large"> | - | 컴포넌트의 크기를 정의해요. |
| invalid | boolean | false | 있으면 컴포넌트에 invalid 상태 스타일이 적용돼야 함을 지정해요. |
| disabled | boolean | false | 있으면 컴포넌트가 비활성화돼야 함을 지정해요. |
| variant | null | HintedString<"outlined" | "filled"> | null | 컴포넌트의 입력 variant를 지정해요. |
| fluid | boolean | null | 켜면 컨테이너 너비의 100%를 차지해요. |
| inputId | string | - | 내부 입력 엘리먼트의 식별자예요. |
| panelStyle | any | - | |
| panelClass | any | - | |
| overlayStyle | any | - | 오버레이의 인라인 스타일이에요. |
| overlayClass | any | - | 오버레이의 스타일 클래스예요. |
| dataKey | string | - | 옵션을 고유하게 식별하는 속성이에요. |
| showClear | boolean | false | 켜면 값을 지울 수 있는 지우기 아이콘을 표시해요. |
| clearIcon | string | - | 지우기 버튼에 표시할 아이콘이에요. |
| resetFilterOnClear | boolean | false | 지우기 아이콘을 클릭하면 필터 값을 지워요. |
| filter | boolean | false | 지정하면 헤더에 필터 입력 칸을 표시해요. |
| filterPlaceholder | string | - | 필터 입력 칸이 비어 있을 때 표시할 플레이스홀더 텍스트예요. |
| filterLocale | string | - | 필터링에 쓸 로케일이에요. 기본 로케일은 호스트 환경의 현재 로케일이에요. |
| filterMatchMode | HintedString<"startsWith" | "contains" | "endsWith"> | contains | 옵션을 검색할 때 쓸 필터링 알고리즘을 정의해요. |
| filterFields | string[] | - | 옵션을 필터링할 때 쓰는 필드예요. 기본값은 optionLabel이에요. |
| appendTo | HTMLElement | HintedString<"body" | "self"> | body | 오버레이를 어디에 붙일지 지정하는 유효한 쿼리 셀렉터나 HTMLElement예요. 특별 키워드로는 문서 body를 뜻하는 'body'와 엘리먼트 자신을 뜻하는 'self'가 있어요. |
| display | HintedString<"comma" | "chip"> | comma | 선택된 항목을 어떻게 표시할지 정의해요. |
| selectedItemsLabel | string | null | 최대 선택 라벨 수를 넘겼을 때 표시할 라벨이에요. |
| maxSelectedLabels | number | - | 선택 항목 라벨을 최대 몇 개까지 보여 줄지 정해요. |
| selectionLimit | number | - | 선택할 수 있는 항목의 최대 개수예요. |
| showToggleAll | boolean | true | 모든 항목 선택을 한 번에 토글하는 헤더 체크박스를 표시할지 여부예요. |
| loading | boolean | false | multiselect가 로딩 상태인지 여부예요. |
| checkboxIcon | string | - | 체크박스에 표시할 아이콘이에요. |
| dropdownIcon | string | - | 드롭다운에 표시할 아이콘이에요. |
| filterIcon | string | - | 필터 입력 칸에 표시할 아이콘이에요. |
| loadingIcon | string | - | 로딩 상태에 표시할 아이콘이에요. |
| removeTokenIcon | string | - | 칩 제거 동작에 표시할 아이콘이에요. |
| chipIcon | string | - | 칩 제거 동작에 표시할 아이콘이에요. |
| selectAll | boolean | false | 모든 데이터가 선택됐는지 여부예요. |
| resetFilterOnHide | boolean | false | 드롭다운을 숨길 때 필터 값을 지워요. |
| virtualScrollerOptions | any | - | virtualScroller 기능을 쓸지 여부예요. VirtualScroller 컴포넌트의 속성을 객체 형태로 그대로 쓸 수 있어요. |
| autoOptionFocus | boolean | false | 오버레이 패널이 열릴 때 보이는 첫 번째 요소나 선택된 요소에 포커스를 줄지 여부예요. |
| autoFilterFocus | boolean | false | 오버레이 패널이 열릴 때 필터 엘리먼트에 포커스를 줄지 여부예요. |
| focusOnHover | boolean | true | 켜면 마우스를 올린 옵션에 포커스가 놓여요. |
| highlightOnSelect | boolean | false | 첫 번째 항목을 자동으로 강조해요. |
| filterMessage | string | '{0} results are available' | 필터링에서 결과가 있을 때 숨겨진 접근성 필드에 표시할 텍스트예요. 기본값은 PrimeVue locale 설정의 값이에요. |
| selectionMessage | string | null | 옵션을 선택했을 때 숨겨진 접근성 필드에 표시할 텍스트예요. 기본값은 PrimeVue locale 설정의 값이에요. |
| emptySelectionMessage | string | No selected item | 아무 옵션도 선택하지 않았을 때 숨겨진 접근성 필드에 표시할 텍스트예요. 기본값은 PrimeVue locale 설정의 값이에요. |
| emptyFilterMessage | string | No results found | 필터링에서 결과가 없을 때 표시할 텍스트예요. 기본값은 PrimeVue locale 설정의 값이에요. |
| emptyMessage | string | No available options' | 사용 가능한 옵션이 없을 때 표시할 텍스트예요. 기본값은 PrimeVue locale 설정의 값이에요. |
| tabindex | string | number | - | 탭 순서에서 엘리먼트의 인덱스예요. |
| ariaLabel | string | - | 대화형 엘리먼트의 라벨로 쓰는 문자열 값을 정의해요. |
| ariaLabelledby | string | - | 내부 입력 엘리먼트의 식별자예요. |
| formControl | Record<string, any> | - | 폼 컨트롤 객체예요. 주로 유효성 검사와 폼 상태를 다룰 때 써요. |
| dt | any | - | 컴포넌트에 디자인 토큰으로 스코프 CSS 변수를 만들어 줘요. |
| pt | PassThrough | - | 컴포넌트 내부 DOM 엘리먼트에 속성을 전달할 때 써요. |
| ptOptions | any | - | 컴포넌트의 passthrough(pt) 옵션을 설정할 때 써요. |
| unstyled | boolean | false | 켜면 코어에서 컴포넌트 관련 스타일을 제거해요. |
Pass Through Options (패스 스루 옵션)
| 이름 | 타입 | 설명 |
|---|---|---|
| root | MultiSelectPassThroughOptionType | root의 DOM 엘리먼트에 속성을 전달할 때 써요. |
| labelContainer | MultiSelectPassThroughOptionType | label container의 DOM 엘리먼트에 속성을 전달할 때 써요. |
| label | MultiSelectPassThroughOptionType | label의 DOM 엘리먼트에 속성을 전달할 때 써요. |
| clearIcon | MultiSelectPassThroughOptionType | label의 DOM 엘리먼트에 속성을 전달할 때 써요. |
| chipItem | MultiSelectPassThroughOptionType | chip의 DOM 엘리먼트에 속성을 전달할 때 써요. |
| pcChip | any | Chip에 속성을 전달할 때 써요. |
| dropdown | MultiSelectPassThroughOptionType | dropdown의 DOM 엘리먼트에 속성을 전달할 때 써요. |
| loadingIcon | MultiSelectPassThroughOptionType | loading icon의 DOM 엘리먼트에 속성을 전달할 때 써요. |
| dropdownIcon | MultiSelectPassThroughOptionType | dropdown icon의 DOM 엘리먼트에 속성을 전달할 때 써요. |
| overlay | MultiSelectPassThroughOptionType | overlay의 DOM 엘리먼트에 속성을 전달할 때 써요. |
| header | MultiSelectPassThroughOptionType | header의 DOM 엘리먼트에 속성을 전달할 때 써요. |
| pcHeaderCheckbox | MultiSelectPassThroughOptionType | header checkbox의 DOM 엘리먼트에 속성을 전달할 때 써요. |
| pcFilterContainer | any | IconField 컴포넌트에 속성을 전달할 때 써요. |
| pcFilter | any | InputText 컴포넌트에 속성을 전달할 때 써요. |
| pcFilterIconContainer | any | InputIcon 컴포넌트에 속성을 전달할 때 써요. |
| filterIcon | MultiSelectPassThroughOptionType | filter icon의 DOM 엘리먼트에 속성을 전달할 때 써요. |
| listContainer | MultiSelectPassThroughOptionType | list container의 DOM 엘리먼트에 속성을 전달할 때 써요. |
| virtualScroller | any | VirtualScroller 컴포넌트에 속성을 전달할 때 써요. |
| list | MultiSelectPassThroughOptionType | list의 DOM 엘리먼트에 속성을 전달할 때 써요. |
| optionGroup | MultiSelectPassThroughOptionType | option group의 DOM 엘리먼트에 속성을 전달할 때 써요. |
| option | MultiSelectPassThroughOptionType | option의 DOM 엘리먼트에 속성을 전달할 때 써요. |
| optionLabel | MultiSelectPassThroughOptionType | option label의 DOM 엘리먼트에 속성을 전달할 때 써요. |
| pcOptionCheckbox | MultiSelectPassThroughOptionType | option checkbox의 DOM 엘리먼트에 속성을 전달할 때 써요. |
| emptyMessage | MultiSelectPassThroughOptionType | emptyMessage의 DOM 엘리먼트에 속성을 전달할 때 써요. |
| hiddenInputContainer | MultiSelectPassThroughOptionType | hidden input container의 DOM 엘리먼트에 속성을 전달할 때 써요. |
| hiddenInput | MultiSelectPassThroughOptionType | hidden input의 DOM 엘리먼트에 속성을 전달할 때 써요. |
| hiddenFirstFocusableEl | MultiSelectPassThroughOptionType | hidden first focusable element의 DOM 엘리먼트에 속성을 전달할 때 써요. |
| hiddenFilterResult | MultiSelectPassThroughOptionType | hidden filter result의 DOM 엘리먼트에 속성을 전달할 때 써요. |
| hiddenSelectedMessage | MultiSelectPassThroughOptionType | hidden selected message의 DOM 엘리먼트에 속성을 전달할 때 써요. |
| hiddenLastFocusableEl | MultiSelectPassThroughOptionType | hidden last focusable element의 DOM 엘리먼트에 속성을 전달할 때 써요. |
| hooks | any | 모든 라이프사이클 훅을 관리할 때 써요. |
| transition | MultiSelectPassThroughTransitionType | Vue Transition API를 제어할 때 써요. |
테마 (Theming)
CSS 클래스 (CSS Classes)
| 클래스 | 설명 |
|---|---|
| p-multiselect | root 엘리먼트의 클래스 이름이에요. |
| p-multiselect-label-container | label container 엘리먼트의 클래스 이름이에요. |
| p-multiselect-label | label 엘리먼트의 클래스 이름이에요. |
| p-multiselect-clear-icon | clear icon 엘리먼트의 클래스 이름이에요. |
| p-multiselect-chip-item | chip item 엘리먼트의 클래스 이름이에요. |
| p-multiselect-chip | chip 엘리먼트의 클래스 이름이에요. |
| p-multiselect-chip-icon | chip icon 엘리먼트의 클래스 이름이에요. |
| p-multiselect-dropdown | dropdown 엘리먼트의 클래스 이름이에요. |
| p-multiselect-loading-icon | loading icon 엘리먼트의 클래스 이름이에요. |
| p-multiselect-dropdown-icon | dropdown icon 엘리먼트의 클래스 이름이에요. |
| p-multiselect-overlay | overlay 엘리먼트의 클래스 이름이에요. |
| p-multiselect-header | header 엘리먼트의 클래스 이름이에요. |
| p-multiselect-filter-container | filter container 엘리먼트의 클래스 이름이에요. |
| p-multiselect-filter | filter 엘리먼트의 클래스 이름이에요. |
| p-multiselect-list-container | list container 엘리먼트의 클래스 이름이에요. |
| p-multiselect-list | list 엘리먼트의 클래스 이름이에요. |
| p-multiselect-option-group | option group 엘리먼트의 클래스 이름이에요. |
| p-multiselect-option | option 엘리먼트의 클래스 이름이에요. |
| p-multiselect-empty-message | empty message 엘리먼트의 클래스 이름이에요. |
디자인 토큰 (Design Tokens)
| 토큰 | CSS 변수 | 설명 |
|---|---|---|
| multiselect.background | --p-multiselect-background | root의 배경이에요. |
| multiselect.disabled.background | --p-multiselect-disabled-background | root의 비활성 배경이에요. |
| multiselect.filled.background | --p-multiselect-filled-background | root의 filled 배경이에요. |
| multiselect.filled.hover.background | --p-multiselect-filled-hover-background | root의 filled hover 배경이에요. |
| multiselect.filled.focus.background | --p-multiselect-filled-focus-background | root의 filled focus 배경이에요. |
| multiselect.border.color | --p-multiselect-border-color | root의 테두리 색이에요. |
| multiselect.hover.border.color | --p-multiselect-hover-border-color | root의 hover 테두리 색이에요. |
| multiselect.focus.border.color | --p-multiselect-focus-border-color | root의 focus 테두리 색이에요. |
| multiselect.invalid.border.color | --p-multiselect-invalid-border-color | root의 invalid 테두리 색이에요. |
| multiselect.color | --p-multiselect-color | root의 색이에요. |
| multiselect.disabled.color | --p-multiselect-disabled-color | root의 비활성 색이에요. |
| multiselect.placeholder.color | --p-multiselect-placeholder-color | root의 placeholder 색이에요. |
| multiselect.invalid.placeholder.color | --p-multiselect-invalid-placeholder-color | root의 invalid placeholder 색이에요. |
| multiselect.shadow | --p-multiselect-shadow | root의 그림자예요. |
| multiselect.padding.x | --p-multiselect-padding-x | root의 가로 패딩이에요. |
| multiselect.padding.y | --p-multiselect-padding-y | root의 세로 패딩이에요. |
| multiselect.border.radius | --p-multiselect-border-radius | root의 테두리 반경이에요. |
| multiselect.focus.ring.width | --p-multiselect-focus-ring-width | root의 focus ring 너비예요. |
| multiselect.focus.ring.style | --p-multiselect-focus-ring-style | root의 focus ring 스타일이에요. |
| multiselect.focus.ring.color | --p-multiselect-focus-ring-color | root의 focus ring 색이에요. |
| multiselect.focus.ring.offset | --p-multiselect-focus-ring-offset | root의 focus ring 오프셋이에요. |
| multiselect.focus.ring.shadow | --p-multiselect-focus-ring-shadow | root의 focus ring 그림자예요. |
| multiselect.transition.duration | --p-multiselect-transition-duration | root의 전환 지속 시간이에요. |
| multiselect.sm.font.size | --p-multiselect-sm-font-size | root의 sm 폰트 크기예요. |
| multiselect.sm.padding.x | --p-multiselect-sm-padding-x | root의 sm 가로 패딩이에요. |
| multiselect.sm.padding.y | --p-multiselect-sm-padding-y | root의 sm 세로 패딩이에요. |
| multiselect.lg.font.size | --p-multiselect-lg-font-size | root의 lg 폰트 크기예요. |
| multiselect.lg.padding.x | --p-multiselect-lg-padding-x | root의 lg 가로 패딩이에요. |
| multiselect.lg.padding.y | --p-multiselect-lg-padding-y | root의 lg 세로 패딩이에요. |
| multiselect.dropdown.width | --p-multiselect-dropdown-width | dropdown의 너비예요. |
| multiselect.dropdown.color | --p-multiselect-dropdown-color | dropdown의 색이에요. |
| multiselect.overlay.background | --p-multiselect-overlay-background | overlay의 배경이에요. |
| multiselect.overlay.border.color | --p-multiselect-overlay-border-color | overlay의 테두리 색이에요. |
| multiselect.overlay.border.radius | --p-multiselect-overlay-border-radius | overlay의 테두리 반경이에요. |
| multiselect.overlay.color | --p-multiselect-overlay-color | overlay의 색이에요. |
| multiselect.overlay.shadow | --p-multiselect-overlay-shadow | overlay의 그림자예요. |
| multiselect.list.padding | --p-multiselect-list-padding | list의 패딩이에요. |
| multiselect.list.gap | --p-multiselect-list-gap | list의 간격이에요. |
| multiselect.list.header.padding | --p-multiselect-list-header-padding | list의 header 패딩이에요. |
| multiselect.option.focus.background | --p-multiselect-option-focus-background | option의 focus 배경이에요. |
| multiselect.option.selected.background | --p-multiselect-option-selected-background | option의 selected 배경이에요. |
| multiselect.option.selected.focus.background | --p-multiselect-option-selected-focus-background | option의 selected focus 배경이에요. |
| multiselect.option.color | --p-multiselect-option-color | option의 색이에요. |
| multiselect.option.focus.color | --p-multiselect-option-focus-color | option의 focus 색이에요. |
| multiselect.option.selected.color | --p-multiselect-option-selected-color | option의 selected 색이에요. |
| multiselect.option.selected.focus.color | --p-multiselect-option-selected-focus-color | option의 selected focus 색이에요. |
| multiselect.option.padding | --p-multiselect-option-padding | option의 패딩이에요. |
| multiselect.option.border.radius | --p-multiselect-option-border-radius | option의 테두리 반경이에요. |
| multiselect.option.gap | --p-multiselect-option-gap | option의 간격이에요. |
| multiselect.option.group.background | --p-multiselect-option-group-background | option group의 배경이에요. |
| multiselect.option.group.color | --p-multiselect-option-group-color | option group의 색이에요. |
| multiselect.option.group.font.weight | --p-multiselect-option-group-font-weight | option group의 폰트 굵기예요. |
| multiselect.option.group.padding | --p-multiselect-option-group-padding | option group의 패딩이에요. |
| multiselect.clear.icon.color | --p-multiselect-clear-icon-color | clear icon의 색이에요. |
| multiselect.chip.border.radius | --p-multiselect-chip-border-radius | chip의 테두리 반경이에요. |
| multiselect.empty.message.padding | --p-multiselect-empty-message-padding | empty message의 패딩이에요. |
더 알아보기 (Learn more)
v-model로 값을 묶고 options에 선택지를 넘기는 기본 조합만 잡아 두면, 나머지는 필요할 때 하나씩 붙여 가면 돼요. 칩 표시는 display="chip", 검색은 filter, 성능이 중요한 긴 목록은 virtualScrollerOptions로 각각 확장돼요. 플로트 라벨은 FloatLabel 문서에서, Ifta 라벨은 IftaLabel 문서에서, 가상 스크롤 설정은 VirtualScroller 문서에서 더 깊게 살펴볼 수 있어요.