콤보박스
입력할 때 일치하는 옵션의 목록 상자를 여는 텍스트 필드로, 직접 입력할 수 있는 선택 상자입니다.
이 페이지의 내용
콤보박스는 입력한 텍스트와 일치하는 옵션을 보여 주는 목록 상자를 여는 텍스트 필드로, 직접 입력할 수 있는 선택 컨트롤입니다. 기존 입력 그룹과 플로팅 목록 상자를 combobox 래퍼로 감싸 하나로 묶습니다. 스타일시트는 위치와 모양을 지정하고, combobox 동작은 옵션을 필터링하고 옵션 사이를 이동하며 선택한 값을 필드에 입력합니다.
콤보박스
<div class="field max-w(320) mx-auto"> <label for="region">Region</label> <div class="combobox"> <div class="input-group"> <input class="input-group-field" id="region" role="combobox" aria-expanded="false" aria-controls="region-list" aria-autocomplete="list" autocomplete="off" placeholder="Search regions"> <button type="button" class="combobox-toggle" tabindex="-1" aria-label="Show regions"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" transform="rotate(90 8 8)" /> </svg> </button> </div> <div id="region-list" popover="manual" class="listbox" role="listbox" aria-label="Regions"> <div class="listbox-option" role="option" id="region-hnd" aria-checked="false">Tokyo</div> <div class="listbox-option" role="option" id="region-sin" aria-checked="false">Singapore</div> <div class="listbox-option" role="option" id="region-fra" aria-checked="false">Frankfurt</div> <div class="listbox-option" role="option" id="region-lhr" aria-checked="false">London</div> <div class="listbox-option" role="option" id="region-iad" aria-checked="false">Virginia</div> <div class="listbox-option" role="option" id="region-pdx" aria-checked="false" aria-disabled="true"> Oregon </div> <div class="listbox-empty">No regions match.</div> </div> </div></div><script type="module"> import { attach } from '@master/ui/behaviors' attach()</script>import { useEffect, useRef } from 'react'import { attach } from '@master/ui/behaviors'import ChevronDownIcon from '@master/icons-react/native/16/soft/chevron-down'export function Example() { const root = useRef<HTMLDivElement>(null) useEffect(() => attach(root.current!), []) return ( <div ref={root} className="field max-w(320) mx-auto"> <label htmlFor="region">Region</label> <div className="combobox"> <div className="input-group"> <input className="input-group-field" id="region" role="combobox" aria-expanded="false" aria-controls="region-list" aria-autocomplete="list" autoComplete="off" placeholder="Search regions" /> <button type="button" className="combobox-toggle" tabIndex={-1} aria-label="Show regions"> <ChevronDownIcon className="icon" /> </button> </div> <div id="region-list" popover="manual" className="listbox" role="listbox" aria-label="Regions"> <div className="listbox-option" role="option" id="region-hnd" aria-checked="false">Tokyo</div> <div className="listbox-option" role="option" id="region-sin" aria-checked="false"> Singapore </div> <div className="listbox-option" role="option" id="region-fra" aria-checked="false"> Frankfurt </div> <div className="listbox-option" role="option" id="region-lhr" aria-checked="false"> London </div> <div className="listbox-option" role="option" id="region-iad" aria-checked="false"> Virginia </div> <div className="listbox-option" role="option" id="region-pdx" aria-checked="false" aria-disabled="true"> Oregon </div> <div className="listbox-empty">No regions match.</div> </div> </div> </div> )}<script> import { attach } from '@master/ui/behaviors' import ChevronDownIcon from '@master/icons-svelte/native/16/soft/chevron-down'</script><div class="field max-w(320) mx-auto" {@attach attach}> <label for="region">Region</label> <div class="combobox"> <div class="input-group"> <input class="input-group-field" id="region" role="combobox" aria-expanded="false" aria-controls="region-list" aria-autocomplete="list" autocomplete="off" placeholder="Search regions"> <button type="button" class="combobox-toggle" tabindex="-1" aria-label="Show regions"> <ChevronDownIcon class="icon" /> </button> </div> <div id="region-list" popover="manual" class="listbox" role="listbox" aria-label="Regions"> <div class="listbox-option" role="option" id="region-hnd" aria-checked="false">Tokyo</div> <div class="listbox-option" role="option" id="region-sin" aria-checked="false">Singapore</div> <div class="listbox-option" role="option" id="region-fra" aria-checked="false">Frankfurt</div> <div class="listbox-option" role="option" id="region-lhr" aria-checked="false">London</div> <div class="listbox-option" role="option" id="region-iad" aria-checked="false">Virginia</div> <div class="listbox-option" role="option" id="region-pdx" aria-checked="false" aria-disabled="true"> Oregon </div> <div class="listbox-empty">No regions match.</div> </div> </div></div><script setup>import { onMounted, onUnmounted, ref } from 'vue'import { attach } from '@master/ui/behaviors'import ChevronDownIcon from '@master/icons-vue/native/16/soft/chevron-down'const root = ref(null)let detachonMounted(() => { detach = attach(root.value) })onUnmounted(() => detach?.())</script><template> <div ref="root" class="field max-w(320) mx-auto"> <label for="region">Region</label> <div class="combobox"> <div class="input-group"> <input class="input-group-field" id="region" role="combobox" aria-expanded="false" aria-controls="region-list" aria-autocomplete="list" autocomplete="off" placeholder="Search regions"> <button type="button" class="combobox-toggle" tabindex="-1" aria-label="Show regions"> <ChevronDownIcon class="icon" /> </button> </div> <div id="region-list" popover="manual" class="listbox" role="listbox" aria-label="Regions"> <div class="listbox-option" role="option" id="region-hnd" aria-checked="false">Tokyo</div> <div class="listbox-option" role="option" id="region-sin" aria-checked="false"> Singapore </div> <div class="listbox-option" role="option" id="region-fra" aria-checked="false"> Frankfurt </div> <div class="listbox-option" role="option" id="region-lhr" aria-checked="false">London</div> <div class="listbox-option" role="option" id="region-iad" aria-checked="false"> Virginia </div> <div class="listbox-option" role="option" id="region-pdx" aria-checked="false" aria-disabled="true"> Oregon </div> <div class="listbox-empty">No regions match.</div> </div> </div> </div></template>#스타일시트의 역할
- 배치.
combobox래퍼는 자신을 앵커로 지정하고 앵커 이름의 범위를 래퍼 자체로 제한합니다. 목록 상자는 캐럿이나 버튼이 아닌 필드 전체를 기준으로 그 아래 6px에 열리며,anchor-size()를 사용해 목록 상자의 너비를 최소한 필드 너비만큼 확보합니다. 별도의 측정이 필요 없으며 한 페이지에 콤보박스가 여러 개 있어도 서로 영향을 주지 않습니다. - 꺾쇠 아이콘.
combobox-toggle은 필드 끝에 있는 작은 버튼으로, 꺾쇠 아이콘을 담습니다. 필드가aria-expanded="true"상태인 동안 꺾쇠 아이콘이 반대 방향을 향합니다. - 빈 상태. 모든 옵션이
hidden상태가 되면listbox-empty가 자동으로 표시됩니다. - 필드 상태. 호버, 포커스, 유효하지 않음, 비활성화 상태는 입력 그룹에서 처리합니다.
앵커 위치 지정을 지원하지 않는 브라우저에서도 목록 상자는 열리며, 마크업에 놓인 위치대로 필드 바로 아래에 목록 상자 자체의 너비로 표시됩니다.
#동작
combobox 동작은 combobox 클래스가 있는 루트 요소와 옵션 객체를 받아, 연결한 동작을 해제하는 함수를 반환합니다. role="combobox"로 필드를 찾고 role="listbox"로 목록을 찾습니다. filter 옵션의 기본값은 true입니다. filter: false를 지정하면 자동 완성에서 보듯이 옵션을 페이지가 처리합니다. name, removeLabel, draw는 여러 값 선택에 사용합니다.
import { combobox } from '@master/ui/behaviors/combobox.js'const box = combobox(document.querySelector('.combobox'))box.value // what the chosen option stands for, or nullbox.query // the text in the field반환된 함수에는 외부에서 읽고 설정할 수 있는 속성이 두 개 있습니다. 어느 속성을 설정해도 이벤트는 발행하지 않습니다.
-
value속성은 선택한 옵션이 나타내는 값으로, 옵션의data-value또는 텍스트입니다. 필드에 텍스트를 입력한 뒤 옵션을 선택하지 않은 상태에서는 null입니다. 값을 설정하면 해당 옵션에 체크 표시를 하고 그 텍스트를 필드에 입력합니다. -
query속성은 필드의 텍스트, 즉 사용자가 찾고 있는 내용입니다. 선택 값과는 다릅니다. 키를 누를 때마다 바뀌며, 옵션을 선택하면 그 옵션의 텍스트가 됩니다. 값을 설정하면 필드에 입력하고, 직접 입력할 때와 마찬가지로 옵션을 필터링합니다. -
텍스트를 입력하면 대소문자를 구분하지 않고 입력한 문자열이 포함되어 있는지 확인합니다. 텍스트에 해당 문자열이 없는 옵션에
hidden속성을 설정하고 목록을 연 뒤, 첫 번째 일치 항목을 활성 옵션으로 지정합니다. -
활성 옵션에는
aria-selected="true"가 설정되며, 필드의aria-activedescendant속성이 해당 옵션을 가리킵니다. 포커스는 필드에 유지됩니다. -
Enter 키나 포인터로 옵션을 선택하면 옵션에서
listbox-description과listbox-end를 제외한 텍스트를 필드에 입력하고, 선택한 옵션은aria-checked="true"로, 나머지 옵션은false로 표시한 뒤 목록을 닫습니다. 선택한 값을 직접 수정하면 해당 옵션의 체크 표시가 사라집니다. -
열기와 닫기. 목록 상자는
popover="manual"이므로 필드에 입력하거나 필드를 클릭해도 닫히지 않습니다. 이 동작은 필드를 클릭하거나 토글 버튼을 조작하거나 방향키를 누르면 목록을 열고, Esc 키를 누르거나 포커스가 콤보박스를 벗어나면 목록을 닫습니다.aria-expanded는 목록의 열림 상태에 맞춰 갱신합니다. -
이벤트. 옵션을 선택하면 필드에 직접 입력할 때와 마찬가지로 필드에서
input,change순서로 이벤트를 발생시킵니다. 값은 필드의value입니다.
비활성화되거나 숨겨진 옵션은 건너뜁니다. 입력기로 문자를 조합하는 동안에는 입력기가 Enter 키와 방향키를 처리합니다.
동작을 연결하지 않으면 필드는 일반 텍스트 입력란으로 작동합니다. 값을 직접 입력할 수 있으며 목록은 열리지 않습니다.
#여러 값 선택
aria-multiselectable="true"가 설정된 목록 상자에는 여러 값을 담을 수 있습니다. 이때 필드는 태그 입력과 마찬가지로 input-group-wrap을 적용한 입력 그룹이 되며, 선택한 옵션은 각각 필드 앞에 칩으로 표시됩니다. 옵션을 선택하면 체크 표시가 켜지거나 꺼지고, 필드가 비워지며, 목록은 열린 상태로 유지됩니다. 따라서 다음 값도 키 몇 번으로 바로 고를 수 있습니다. 옵션의 체크 표시도 유지되므로, 칩과 마찬가지로 목록에서도 무엇을 선택했는지 알 수 있습니다.
여러 값 선택
<div class="field max-w(360) mx-auto"> <label for="regions">Regions</label> <div class="combobox"> <div class="input-group input-group-wrap"> <span class="chip" data-value="hnd"> Tokyo<input type="hidden" name="regions" value="hnd"><button type="button" class="chip-remove" tabindex="-1" aria-label="Remove Tokyo"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M4 4L12 12" /><path d="M4 12L12 4" /> </svg> </button> </span> <input class="input-group-field" id="regions" role="combobox" aria-expanded="false" aria-controls="regions-list" aria-autocomplete="list" autocomplete="off" placeholder="Add a region"> <button type="button" class="combobox-toggle" tabindex="-1" aria-label="Show regions"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" transform="rotate(90 8 8)" /> </svg> </button> </div> <div id="regions-list" popover="manual" class="listbox" role="listbox" aria-label="Regions" aria-multiselectable="true"> <div class="listbox-option" role="option" data-value="hnd" aria-checked="true">Tokyo</div> <div class="listbox-option" role="option" data-value="sin" aria-checked="false"> Singapore </div> <div class="listbox-option" role="option" data-value="fra" aria-checked="false"> Frankfurt </div> <div class="listbox-option" role="option" data-value="lhr" aria-checked="false">London</div> <div class="listbox-option" role="option" data-value="iad" aria-checked="false">Virginia</div> <div class="listbox-empty">No regions match.</div> </div> </div></div><script type="module"> import { attach } from '@master/ui/behaviors' attach()</script>import { useEffect, useRef } from 'react'import { attach } from '@master/ui/behaviors'import XIcon from '@master/icons-react/native/16/soft/x'import ChevronDownIcon from '@master/icons-react/native/16/soft/chevron-down'export function Example() { const root = useRef<HTMLDivElement>(null) useEffect(() => attach(root.current!), []) return ( <div ref={root} className="field max-w(360) mx-auto"> <label htmlFor="regions">Regions</label> <div className="combobox"> <div className="input-group input-group-wrap"> <span className="chip" data-value="hnd"> Tokyo<input type="hidden" name="regions" value="hnd" /><button type="button" className="chip-remove" tabIndex={-1} aria-label="Remove Tokyo"> <XIcon className="icon" /> </button> </span> <input className="input-group-field" id="regions" role="combobox" aria-expanded="false" aria-controls="regions-list" aria-autocomplete="list" autoComplete="off" placeholder="Add a region" /> <button type="button" className="combobox-toggle" tabIndex={-1} aria-label="Show regions"> <ChevronDownIcon className="icon" /> </button> </div> <div id="regions-list" popover="manual" className="listbox" role="listbox" aria-label="Regions" aria-multiselectable="true"> <div className="listbox-option" role="option" data-value="hnd" aria-checked="true">Tokyo</div> <div className="listbox-option" role="option" data-value="sin" aria-checked="false"> Singapore </div> <div className="listbox-option" role="option" data-value="fra" aria-checked="false"> Frankfurt </div> <div className="listbox-option" role="option" data-value="lhr" aria-checked="false"> London </div> <div className="listbox-option" role="option" data-value="iad" aria-checked="false"> Virginia </div> <div className="listbox-empty">No regions match.</div> </div> </div> </div> )}<script> import { attach } from '@master/ui/behaviors' import XIcon from '@master/icons-svelte/native/16/soft/x' import ChevronDownIcon from '@master/icons-svelte/native/16/soft/chevron-down'</script><div class="field max-w(360) mx-auto" {@attach attach}> <label for="regions">Regions</label> <div class="combobox"> <div class="input-group input-group-wrap"> <span class="chip" data-value="hnd"> Tokyo<input type="hidden" name="regions" value="hnd"><button type="button" class="chip-remove" tabindex="-1" aria-label="Remove Tokyo"> <XIcon class="icon" /> </button> </span> <input class="input-group-field" id="regions" role="combobox" aria-expanded="false" aria-controls="regions-list" aria-autocomplete="list" autocomplete="off" placeholder="Add a region"> <button type="button" class="combobox-toggle" tabindex="-1" aria-label="Show regions"> <ChevronDownIcon class="icon" /> </button> </div> <div id="regions-list" popover="manual" class="listbox" role="listbox" aria-label="Regions" aria-multiselectable="true"> <div class="listbox-option" role="option" data-value="hnd" aria-checked="true">Tokyo</div> <div class="listbox-option" role="option" data-value="sin" aria-checked="false"> Singapore </div> <div class="listbox-option" role="option" data-value="fra" aria-checked="false"> Frankfurt </div> <div class="listbox-option" role="option" data-value="lhr" aria-checked="false">London</div> <div class="listbox-option" role="option" data-value="iad" aria-checked="false">Virginia</div> <div class="listbox-empty">No regions match.</div> </div> </div></div><script setup>import { onMounted, onUnmounted, ref } from 'vue'import { attach } from '@master/ui/behaviors'import XIcon from '@master/icons-vue/native/16/soft/x'import ChevronDownIcon from '@master/icons-vue/native/16/soft/chevron-down'const root = ref(null)let detachonMounted(() => { detach = attach(root.value) })onUnmounted(() => detach?.())</script><template> <div ref="root" class="field max-w(360) mx-auto"> <label for="regions">Regions</label> <div class="combobox"> <div class="input-group input-group-wrap"> <span class="chip" data-value="hnd"> Tokyo<input type="hidden" name="regions" value="hnd"><button type="button" class="chip-remove" tabindex="-1" aria-label="Remove Tokyo"> <XIcon class="icon" /> </button> </span> <input class="input-group-field" id="regions" role="combobox" aria-expanded="false" aria-controls="regions-list" aria-autocomplete="list" autocomplete="off" placeholder="Add a region"> <button type="button" class="combobox-toggle" tabindex="-1" aria-label="Show regions"> <ChevronDownIcon class="icon" /> </button> </div> <div id="regions-list" popover="manual" class="listbox" role="listbox" aria-label="Regions" aria-multiselectable="true"> <div class="listbox-option" role="option" data-value="hnd" aria-checked="true">Tokyo</div> <div class="listbox-option" role="option" data-value="sin" aria-checked="false"> Singapore </div> <div class="listbox-option" role="option" data-value="fra" aria-checked="false"> Frankfurt </div> <div class="listbox-option" role="option" data-value="lhr" aria-checked="false">London</div> <div class="listbox-option" role="option" data-value="iad" aria-checked="false"> Virginia </div> <div class="listbox-empty">No regions match.</div> </div> </div> </div></template>- 필드는 검색에만 사용합니다. 텍스트를 입력하면 목록이 필터링될 뿐, 체크 표시가 해제되지는 않습니다. 선택한 값은 칩이 유지합니다.
- 칩. 탭 순서에서 멈추는 곳은 필드 하나뿐입니다. 빈 필드에서 Backspace 키를 누르거나 뒤쪽을 가리키는 방향키를 누르면 칩으로 이동하며, 칩에서 사용하는 키는 태그 입력과 같습니다. 칩의 버튼이나 Backspace 키로 칩을 삭제하면 해당 옵션의 체크 표시도 해제됩니다.
- 폼. 기존 칩에
name이 있는 숨겨진 입력 요소가 이미 들어 있거나name옵션을 지정하면, 동작이 작성하는 칩에도 값을 담은 숨겨진 입력 요소가 포함됩니다.removeLabel은 페이지 언어로 칩의 삭제 버튼 이름을 지정합니다. - 값은 선택한 값을 선택한 순서대로 담은 목록입니다.
목록에서 고르지 않고 값을 자유롭게 입력해야 한다면 태그 입력을 사용하세요.
#자동 완성
일치 항목을 서버에서 가져오거나 페이지 자체의 검색으로 찾는다면 filter: false로 동작을 연결하세요. 동작은 아무것도 숨기지 않습니다. 필드에서 input이 발생할 때마다 query를 읽어 검색하고, 일치 항목을 목록의 옵션으로 작성하세요. 입력에 대한 응답이 늦게 도착해도 도착한 옵션 중 하나가 활성 옵션으로 지정되므로, Enter 키와 방향키로 조작할 수 있습니다.
자동 완성
<div class="field max-w(320) mx-auto"> <label for="city">City</label> <div class="combobox" id="city-combobox"> <div class="input-group"> <input class="input-group-field" id="city" role="combobox" aria-expanded="false" aria-controls="city-list" aria-autocomplete="list" autocomplete="off" placeholder="Start typing a city"> <button type="button" class="combobox-toggle" tabindex="-1" aria-label="Show cities"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" transform="rotate(90 8 8)" /> </svg> </button> </div> <div id="city-list" popover="manual" class="listbox" role="listbox" aria-label="Cities"> <div class="listbox-empty">No cities start with that.</div> </div> </div></div><script>addEventListener('DOMContentLoaded', function () {var root = document.getElementById('city-combobox')var list = document.getElementById('city-list')var cities = ['Amsterdam', 'Athens', 'Auckland', 'Bangkok', 'Barcelona', 'Berlin', 'Bogotá', 'Boston', 'Brussels', 'Buenos Aires', 'Cairo', 'Cape Town', 'Chicago', 'Copenhagen', 'Dublin', 'Frankfurt', 'Helsinki', 'Hong Kong', 'Istanbul', 'Lisbon', 'London', 'Los Angeles', 'Madrid', 'Melbourne', 'Mexico City', 'Montreal', 'Mumbai', 'Nairobi', 'New York', 'Oslo', 'Paris', 'Prague', 'Rome', 'San Francisco', 'Santiago', 'São Paulo', 'Seoul', 'Singapore', 'Stockholm', 'Sydney', 'Taipei', 'Tokyo', 'Toronto', 'Vienna', 'Warsaw', 'Zurich']var box = MasterUI.combobox(root, { filter: false })var asked = 0// Stands for a request to a server: the cities that start with the query, a moment later.function search(query) { return new Promise(function (resolve) { setTimeout(function () { var text = query.trim().toLowerCase() resolve(cities.filter(function (city) { return city.toLowerCase().startsWith(text) }).slice(0, 6)) }, 120) })}function write(matches) { list.querySelectorAll('.listbox-option').forEach(function (option) { option.remove() }) matches.forEach(function (city) { var option = document.createElement('div') option.className = 'listbox-option' option.setAttribute('role', 'option') option.setAttribute('aria-checked', 'false') option.textContent = city list.insertBefore(option, list.querySelector('.listbox-empty')) })}root.addEventListener('input', function (event) { // A choice says input too; its text is the option's, with nothing more to look for. if (event.target.getAttribute('role') !== 'combobox' || box.value !== null) return var ticket = ++asked search(box.query).then(function (matches) { // An answer to an older query that arrives late is dropped. if (ticket === asked) write(matches) })})search('').then(write)})</script><script type="module"> import { attach } from '@master/ui/behaviors' attach()</script>- 이벤트가 아니라
query를 읽으세요. 검색어는 필드의 텍스트입니다. 사용자가 입력했든 프레임워크가 작성했든query는 같은 방식으로 이 텍스트를 읽습니다. - 옵션을 선택해도
input이 발생합니다. 이때 텍스트는 선택한 옵션의 텍스트이며,value는 더 이상 null이 아닙니다. 검색할 내용이 없으며, 이때 새 옵션을 작성하면 선택한 옵션이 사라집니다. - 응답은 순서가 뒤바뀌어 도착할 수 있습니다. 가장 최근 검색어에 대한 응답만 사용하세요.
- 빈 상태는 스타일시트가 처리합니다. 목록에 옵션이 없는 동안
listbox-empty가 자동으로 표시됩니다.
React, Svelte, Vue에서는 컴포넌트의 query와 onQueryChange(Svelte에서는 바인딩 가능한 query, Vue에서는 v-model:query)가 같은 텍스트를 전달합니다. 이 텍스트로 검색하고, filter를 false로 설정한 뒤 일치 항목을 options로 전달하세요.
#컨트롤 선택 기준
단순한 옵션이 12개 이하라면 셀렉트에서 스크립트 없이 플랫폼 기본 기능으로 글자를 입력해 옵션을 찾을 수 있습니다. 목록이 길어 검색이 필요하거나, 입력에 따라 옵션을 불러오거나, 각 행에 부가 설명 등 다양한 콘텐츠를 표시해야 할 때는 콤보박스를 사용하세요.
#키보드 조작
키보드 입력은 연결한 동작에서 처리합니다. 목록이 닫혀 있거나 Shift 키를 누른 상태에서는 Home과 End 키가 일반 입력 필드에서처럼 캐럿을 이동합니다. 활성 옵션이 없으면 Enter 키의 기본 동작을 그대로 두므로 양식을 제출할 수 있습니다.
| 키 | 동작 |
|---|---|
| ArrowDown, ArrowUp | 목록을 열고 활성 항목을 바꿉니다. 양 끝을 지나면 반대쪽 끝으로 이동합니다. |
| Home, End | 목록이 열려 있는 동안 첫 번째 또는 마지막 옵션으로 이동합니다. |
| Enter | 활성 옵션을 선택하고 목록을 닫습니다. 여러 값을 선택하는 경우에는 옵션의 체크 상태를 전환하고 목록을 열어 둡니다. |
| Escape | 목록을 닫습니다. 입력 필드의 텍스트는 그대로 유지됩니다. |
| Typing | 옵션을 필터링하고 첫 번째 일치 항목을 활성 상태로 두어 목록을 엽니다. |
| Backspace | 여러 값을 선택하는 경우, 빈 필드에서는 마지막 칩으로 이동합니다. 칩에서는 해당 옵션의 체크를 해제하고 이전 칩으로 이동합니다. 첫 번째 칩이었다면 다음 칩으로, 남은 칩이 없으면 필드로 이동합니다. |
| Delete | 여러 값을 선택하는 경우, 칩에서는 해당 옵션의 체크를 해제하고 다음 칩으로 이동합니다. 마지막 칩이었다면 필드로 이동합니다. |
| ArrowLeft, ArrowRight | 여러 값을 선택하는 경우, 태그 입력과 마찬가지로 필드의 시작 위치에서 칩 사이를 이동합니다. |
| Home, End | 여러 값을 선택하는 경우, 칩에서는 첫 번째 칩으로 이동하거나 필드로 돌아갑니다. |
| Escape | 여러 값을 선택하는 경우, 칩에서는 필드로 돌아갑니다. |
#접근성
- 입력 요소에는
role="combobox", 목록 상자를 가리키는aria-controls,aria-autocomplete="list",aria-expanded속성이 설정됩니다. - 포커스는 입력 요소에 유지되며, 스크린 리더는
aria-activedescendant속성을 통해 활성 옵션을 읽어 줍니다. - 필드에서 방향키로 목록을 열 수 있으므로
tabindex="-1"을 설정해 토글 버튼을 탭 순서에서 제외하세요. - 브라우저의 자체 추천 항목이 목록을 가리지 않도록
autocomplete="off"로 설정하세요.
#React, Svelte, Vue
combobox 컴포넌트는 옵션 목록을 바탕으로 이 마크업을 렌더링하고 동작을 연결하며, 선택한 값을 받아 외부로 전달할 수 있습니다. multiple을 지정하면 값이 목록이 되고, 컴포넌트가 그 값으로 칩을 작성합니다. group이 있는 옵션은 해당 캡션 아래, listbox-label로 레이블이 지정되고 role="group"이 있는 listbox-group 안에 작성됩니다. 그룹의 옵션이 모두 필터링되면 그룹도 함께 숨겨집니다. 컴포넌트는 값 외에 검색어도 전달합니다. React에서는 query와 onQueryChange, Svelte에서는 onQueryChange와 함께 쓰는 바인딩 가능한 query, Vue에서는 v-model:query를 사용합니다. 검색어는 사용자가 입력할 때마다 전달되며, 옵션을 선택하면 선택 값을 전달한 뒤 검색어를 전달합니다. 외부에서 지정한 검색어는 필드에 반영되며, 그 검색어로 옵션을 필터링합니다. npx master-ui add combobox 명령을 실행해 프로젝트에 복사하세요. 컴포넌트의 구성과 사용 방법은 React, Svelte, Vue에서 확인할 수 있습니다.
#클래스
| 클래스 | 설명 |
|---|---|
combobox | 입력 필드를 기준으로 목록 상자의 위치를 잡는 래퍼 |
combobox-toggle | 필드 끝에서 목록을 열고 닫는 버튼으로, 꺾쇠 아이콘을 담습니다 |
input-group, input-group-field, 그리고 popover 속성이 있는 listbox와 함께 사용합니다.
#관련 컴포넌트
콤보박스는 다음 컴포넌트로 구성되며, 각 컴포넌트는 자체 클래스와 상태를 유지합니다.
- 입력 필드표면을 파낸 듯한 텍스트 입력 필드와 레이블, 힌트, 오류, 입력 그룹.
- 목록 상자콤보 상자나 명령 팔레트가 여는 선택용 옵션 목록입니다.
- 태그 입력입력한 텍스트를 값으로 바꾸어 오목한 영역 안에 칩으로 하나씩 표시하는 필드입니다.
- 셀렉트인터페이스의 다른 부분과 같은 표면 스타일로 옵션 목록을 표시하는 네이티브 셀렉트입니다.