태그 입력
입력한 텍스트를 값으로 바꾸어 오목한 영역 안에 칩으로 하나씩 표시하는 필드입니다.
이 페이지의 내용
태그 입력은 입력한 텍스트를 값으로 바꾸고, 각 값을 오목한 영역 안에 칩으로 표시하는 필드입니다. input-group-wrap과 tags 클래스를 적용한 입력 그룹으로 구성합니다. 오목한 영역과 그 상태, 영역과 둥근 모서리의 중심이 일치하는 칩은 모두 입력 그룹에서 제공하며, 칩 뒤에 오는 필드가 다음 값을 입력받습니다. tags 동작은 텍스트를 칩으로 바꾸고 칩 사이에서 포커스를 이동합니다.
태그 입력
<div class="field max-w(420) mx-auto"> <label for="labels">Labels</label> <div class="tags input-group input-group-wrap"> <span class="chip" data-value="design"> design<input type="hidden" name="labels" value="design"><button type="button" class="chip-remove" tabindex="-1" aria-label="Remove design"> <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> <span class="chip" data-value="research"> research<input type="hidden" name="labels" value="research"><button type="button" class="chip-remove" tabindex="-1" aria-label="Remove research"> <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> <span class="chip" data-value="accessibility"> accessibility<input type="hidden" name="labels" value="accessibility"><button type="button" class="chip-remove" tabindex="-1" aria-label="Remove accessibility"> <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="labels" placeholder="Add a label" autocomplete="off" aria-describedby="labels-hint"> </div> <p class="field-hint" id="labels-hint">Press Enter or type a comma after each label.</p></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'export function Example() { const root = useRef<HTMLDivElement>(null) useEffect(() => attach(root.current!), []) return ( <div ref={root} className="field max-w(420) mx-auto"> <label htmlFor="labels">Labels</label> <div className="tags input-group input-group-wrap"> <span className="chip" data-value="design"> design<input type="hidden" name="labels" value="design" /><button type="button" className="chip-remove" tabIndex={-1} aria-label="Remove design"> <XIcon className="icon" /> </button> </span> <span className="chip" data-value="research"> research<input type="hidden" name="labels" value="research" /><button type="button" className="chip-remove" tabIndex={-1} aria-label="Remove research"> <XIcon className="icon" /> </button> </span> <span className="chip" data-value="accessibility"> accessibility<input type="hidden" name="labels" value="accessibility" /><button type="button" className="chip-remove" tabIndex={-1} aria-label="Remove accessibility"> <XIcon className="icon" /> </button> </span> <input className="input-group-field" id="labels" placeholder="Add a label" autoComplete="off" aria-describedby="labels-hint" /> </div> <p className="field-hint" id="labels-hint">Press Enter or type a comma after each label.</p> </div> )}<script> import { attach } from '@master/ui/behaviors' import XIcon from '@master/icons-svelte/native/16/soft/x'</script><div class="field max-w(420) mx-auto" {@attach attach}> <label for="labels">Labels</label> <div class="tags input-group input-group-wrap"> <span class="chip" data-value="design"> design<input type="hidden" name="labels" value="design"><button type="button" class="chip-remove" tabindex="-1" aria-label="Remove design"> <XIcon class="icon" /> </button> </span> <span class="chip" data-value="research"> research<input type="hidden" name="labels" value="research"><button type="button" class="chip-remove" tabindex="-1" aria-label="Remove research"> <XIcon class="icon" /> </button> </span> <span class="chip" data-value="accessibility"> accessibility<input type="hidden" name="labels" value="accessibility"><button type="button" class="chip-remove" tabindex="-1" aria-label="Remove accessibility"> <XIcon class="icon" /> </button> </span> <input class="input-group-field" id="labels" placeholder="Add a label" autocomplete="off" aria-describedby="labels-hint"> </div> <p class="field-hint" id="labels-hint">Press Enter or type a comma after each label.</p></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'const root = ref(null)let detachonMounted(() => { detach = attach(root.value) })onUnmounted(() => detach?.())</script><template> <div ref="root" class="field max-w(420) mx-auto"> <label for="labels">Labels</label> <div class="tags input-group input-group-wrap"> <span class="chip" data-value="design"> design<input type="hidden" name="labels" value="design"><button type="button" class="chip-remove" tabindex="-1" aria-label="Remove design"> <XIcon class="icon" /> </button> </span> <span class="chip" data-value="research"> research<input type="hidden" name="labels" value="research"><button type="button" class="chip-remove" tabindex="-1" aria-label="Remove research"> <XIcon class="icon" /> </button> </span> <span class="chip" data-value="accessibility"> accessibility<input type="hidden" name="labels" value="accessibility"><button type="button" class="chip-remove" tabindex="-1" aria-label="Remove accessibility"> <XIcon class="icon" /> </button> </span> <input class="input-group-field" id="labels" placeholder="Add a label" autocomplete="off" aria-describedby="labels-hint"> </div> <p class="field-hint" id="labels-hint">Press Enter or type a comma after each label.</p> </div></template>#입력 그룹에 더해지는 기능
- 값 추가. 새 칩은 갑자기 나타나지 않고, 캐럿이 있던 자리에서 80% 크기부터 짧은 지속 시간에 걸쳐 커지며 나타납니다. 동작 줄이기 설정이 켜져 있으면 서서히 나타나기만 합니다.
- 단어를 입력할 공간. 필드는 최소
--tags-field-min(기본값 6rem)의 너비를 유지하며, 그보다 좁아지면 아래쪽에 한 줄을 따로 차지합니다. 따라서 캐럿이 줄 끝의 좁은 틈에 끼는 일이 없습니다. - 키보드 포커스가 있는 값에는 칩 전체에 포커스 링이 표시됩니다. 그 상태에서 Backspace 키를 누르면 값 전체가 삭제되기 때문입니다. 삭제 버튼의 X 표시는 눌린 상태의 색조로 바뀝니다. 이 포커스 링은 칩이 그리는 것이므로,
chip-remove가 있는 칩이라면 어디에 있든 표시됩니다. - 거부된 값이 있으면 필드에
aria-invalid="true"가 설정되고, 입력 그룹은 텍스트가 바뀔 때까지 위험 색상 테두리를 표시합니다.
@starting-style을 지원하지 않는 브라우저에서는 새 값이 바로 나타납니다.
#동작
tags 동작은 tags 클래스가 있는 입력 그룹을 루트 요소로 받아, 연결한 동작을 해제하는 함수를 반환합니다.
import { tags } from '@master/ui/behaviors/tags.js'const detach = tags(document.querySelector('.tags'), { max: 10 })- 추가. Enter 키나 구분 문자(기본값은 쉼표)를 입력하면 필드의 텍스트가 값이 됩니다. 필드 앞에 칩이 작성되고 필드는 비워집니다. 구분 문자, 줄바꿈, 탭이 들어 있는 텍스트를 붙여 넣으면 이를 경계로 나뉜 각 항목이 값으로 추가되며, 마지막 구분 문자 뒤의 텍스트는 필드에 남습니다.
- 거부. 비어 있는 값, 이미 있는 값,
max를 넘는 값,validate가 거부한 값은 추가되지 않습니다. 입력한 내용이 사라지지 않도록 필드는 텍스트를 그대로 유지하며, 텍스트가 바뀔 때까지aria-invalid="true"상태가 됩니다. - 삭제. 칩의 삭제 버튼을 클릭하면 칩이 삭제되고 포커스는 필드로 돌아갑니다.
- 폼. 기존 칩에
name이 있는 숨겨진 입력 요소가 이미 들어 있거나name옵션을 지정하면, 각 칩에 값을 담은 숨겨진 입력 요소가 포함됩니다. 따라서 폼은 모든 값을 해당 이름으로 제출합니다. 폼을 초기화하면 필드에 처음 있던 값으로 되돌아갑니다. - 이벤트. 값을 추가하거나 삭제할 때마다 루트 요소에서
input과change이벤트를 발생시킵니다. 반환된 함수에는 값을 순서대로 담은value가 있습니다. 이 속성에 값을 설정하면 기존 값이 대체되며, 이때는 이벤트가 발생하지 않습니다.
| 옵션 | 기본값 | |
|---|---|---|
delimiters | [','] | Enter 키처럼 값을 추가하는 키입니다. []로 지정하면 Enter 키만 남습니다. |
max | 없음 | 필드에 담을 수 있는 최대 값 개수입니다. |
validate | 없음 | 값을 추가할 수 있는지 판단하는 함수입니다. |
name | 기존 칩의 숨겨진 입력 요소 이름 | 새 칩에 포함되는 숨겨진 입력 요소의 이름입니다. |
removeLabel | Remove <value> | 페이지 언어로 지정하는 칩 삭제 버튼의 접근 가능한 이름입니다. |
draw | 없음 | 칩 작성을 대신 맡습니다. 자체 목록을 바탕으로 칩을 작성하는 렌더러에 사용합니다. |
칩은 data-value 값을 나타내며, 이 속성이 없으면 칩의 텍스트를 값으로 사용합니다. 동작을 연결하지 않으면 필드는 일반 텍스트 입력란이 되고 칩은 고정됩니다. 이때도 폼은 칩에 포함된 숨겨진 입력 요소를 제출합니다.
#추천 항목
<datalist>를 사용하면 별도의 스크립트 없이 브라우저 자체의 추천 항목을 필드에 제공할 수 있습니다. 사용자가 추천 항목을 고른 다음 Enter 키를 눌러 값으로 추가합니다. 값을 반드시 목록에서 골라야 한다면 여러 값을 선택하는 콤보박스를 사용하세요.
추천 항목
<div class="field max-w(420) mx-auto"> <label for="recipients">Recipients</label> <div class="tags input-group input-group-wrap"> <span class="chip" data-value="ada@example.com"> ada@example.com<input type="hidden" name="to" value="ada@example.com"><button type="button" class="chip-remove" tabindex="-1" aria-label="Remove ada@example.com"> <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="recipients" type="email" list="recipient-suggestions" placeholder="Add a recipient" autocomplete="off"> </div> <datalist id="recipient-suggestions"> <option value="grace@example.com"></option> <option value="linus@example.com"></option> <option value="margaret@example.com"></option> </datalist></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'export function Example() { const root = useRef<HTMLDivElement>(null) useEffect(() => attach(root.current!), []) return ( <div ref={root} className="field max-w(420) mx-auto"> <label htmlFor="recipients">Recipients</label> <div className="tags input-group input-group-wrap"> <span className="chip" data-value="ada@example.com"> ada@example.com<input type="hidden" name="to" value="ada@example.com" /><button type="button" className="chip-remove" tabIndex={-1} aria-label="Remove ada@example.com"> <XIcon className="icon" /> </button> </span> <input className="input-group-field" id="recipients" type="email" list="recipient-suggestions" placeholder="Add a recipient" autoComplete="off" /> </div> <datalist id="recipient-suggestions"> <option value="grace@example.com"></option> <option value="linus@example.com"></option> <option value="margaret@example.com"></option> </datalist> </div> )}<script> import { attach } from '@master/ui/behaviors' import XIcon from '@master/icons-svelte/native/16/soft/x'</script><div class="field max-w(420) mx-auto" {@attach attach}> <label for="recipients">Recipients</label> <div class="tags input-group input-group-wrap"> <span class="chip" data-value="ada@example.com"> ada@example.com<input type="hidden" name="to" value="ada@example.com"><button type="button" class="chip-remove" tabindex="-1" aria-label="Remove ada@example.com"> <XIcon class="icon" /> </button> </span> <input class="input-group-field" id="recipients" type="email" list="recipient-suggestions" placeholder="Add a recipient" autocomplete="off"> </div> <datalist id="recipient-suggestions"> <option value="grace@example.com"></option> <option value="linus@example.com"></option> <option value="margaret@example.com"></option> </datalist></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'const root = ref(null)let detachonMounted(() => { detach = attach(root.value) })onUnmounted(() => detach?.())</script><template> <div ref="root" class="field max-w(420) mx-auto"> <label for="recipients">Recipients</label> <div class="tags input-group input-group-wrap"> <span class="chip" data-value="ada@example.com"> ada@example.com<input type="hidden" name="to" value="ada@example.com"><button type="button" class="chip-remove" tabindex="-1" aria-label="Remove ada@example.com"> <XIcon class="icon" /> </button> </span> <input class="input-group-field" id="recipients" type="email" list="recipient-suggestions" placeholder="Add a recipient" autocomplete="off"> </div> <datalist id="recipient-suggestions"> <option value="grace@example.com"></option> <option value="linus@example.com"></option> <option value="margaret@example.com"></option> </datalist> </div></template>#키보드
탭 순서에서 멈추는 곳은 필드 하나뿐입니다. 칩에는 필드에서 이동하며, 칩의 삭제 버튼은 tabindex="-1"로 탭 순서에서 제외됩니다. 입력기로 문자를 조합하는 동안에는 입력기가 Enter 키를 처리합니다.
| 키 | 동작 |
|---|---|
| Enter, Comma | 필드의 텍스트를 값으로 바꿉니다. 빈 필드에서 누른 Enter는 폼이 처리합니다. |
| Backspace | 빈 필드에서는 마지막 값으로 이동합니다. 값에서는 해당 값을 삭제하고 이전 값으로 이동합니다. |
| Delete | 값에서는 해당 값을 삭제하고 다음 값으로 이동합니다. |
| ArrowLeft, ArrowRight | 필드의 시작 위치에서는 마지막 값으로 이동합니다. 값 사이에서는 이전 또는 다음 값으로 이동하고, 마지막 값을 지나면 필드로 돌아갑니다. 오른쪽에서 왼쪽으로 읽는 레이아웃에서는 방향이 반대입니다. |
| Home, End | 값에서는 첫 번째 값으로 이동하거나 필드로 돌아갑니다. |
| Escape | 값에서는 필드로 돌아갑니다. |
#접근성
<label>이나aria-label로 필드에 레이블을 지정하세요. 쉼표나 Enter 키로 값을 추가한다는 사실은 바로 알기 어려우므로, 필드 아래의 힌트로 값을 추가하는 방법을 알려 주세요.- 각 삭제 버튼의 이름에는 삭제할 값을 페이지 언어로 포함하세요. 동작이 작성하는 칩에는
removeLabel이 이름을 지정합니다. - 거부된 값은 필드에 텍스트로 남고 필드는 유효하지 않은 상태로 표시됩니다. 다른 필드와 마찬가지로 필드의 힌트나 오류 메시지로 그 이유를 알려 주세요.
#React, Svelte, Vue
tags 컴포넌트는 값을 바탕으로 칩을 작성하고, 입력한 텍스트를 값으로 바꾸는 동작을 연결합니다. npx master-ui add tags 명령을 실행해 프로젝트에 복사하세요. 컴포넌트의 구성과 사용 방법은 React, Svelte, Vue에서 확인할 수 있습니다.
#클래스
| 클래스 | 설명 |
|---|---|
tags | input-group-wrap을 적용한 입력 그룹에 지정합니다. 입력 그룹의 칩이 값이 되고 필드는 다음 값을 입력받으며, 필드는 단어 하나를 입력할 공간을 확보하다가 부족해지면 별도의 줄로 넘어갑니다. |
input-group, input-group-wrap, input-group-field, 그리고 값마다 chip-remove를 포함한 chip과 함께 사용합니다.
#사용자 지정 속성
| 속성 | 기본값 | 설명 |
|---|---|---|
--tags-field-min | 6rem | 필드가 별도의 줄로 넘어가기 전까지 줄어들 수 있는 최소 너비 |
#관련 항목
- 입력 필드표면을 파낸 듯한 텍스트 입력 필드와 레이블, 힌트, 오류, 입력 그룹.
- 칩필터나 삭제할 수 있는 값을 나타내는 작은 알약 모양의 컴포넌트입니다.
- 콤보박스입력할 때 일치하는 옵션의 목록 상자를 여는 텍스트 필드로, 직접 입력할 수 있는 선택 상자입니다.