InputTags
InputTags는 여러 콘텐츠를 항목(tag) 단위로 묶어 주는 컴포넌트예요. 이메일 주소나 기술 스택처럼 여러 값을 개별 태그로 입력받고 싶을 때 쓰기 좋아요.
출처: 문서
본문
사용법 (Usage)
import { InputTags } from '@primereact/ui/inputtags';
import { InputText } from '@primereact/ui/inputtext';
import { Chip } from '@primereact/ui/chip';
InputTags.Items는 태그마다 { item, index, remove, itemProps }를 받는 render-prop child를 가져요. itemProps를 렌더링하는 chip에 펼쳐서 ARIA 역할과 data-selected 포커스 마커를 적용해요. InputTags.Control은 텍스트 입력 props와 태그 변경 헬퍼를 노출하며, controlProps를 어떤 입력 요소에든 펼칠 수 있어요.
<InputTags.Root value={tags} onValueChange={(e) => setTags(e.value)}>
<InputTags.Items>
{({ item, remove, itemProps }) => (
<Chip.Root {...itemProps}>
<Chip.Label>{item}</Chip.Label>
<Chip.Remove onClick={remove}>×</Chip.Remove>
</Chip.Root>
)}
</InputTags.Items>
<InputTags.Control>{({ controlProps }) => <InputText {...controlProps} />}</InputTags.Control>
</InputTags.Root>
타입어헤드(typeahead) 지원을 위해선 <AutoComplete.Root> 트리를 <InputTags.Control> 안에 렌더링하고, autocomplete의 onValueChange에서 add를 호출하면 돼요. add는 문자열 하나 또는 문자열 배열을 받아요. InputTags 루트 요소는 콜백 ref + state로 추적하고 <AutoComplete.Positioner anchor={...}>에 전달해서, 팝업이 입력 필드만이 아니라 전체 필드 너비에 맞도록 해요.
const [rootEl, setRootEl] = React.useState<HTMLElement | null>(null);
<InputTags.Root ref={setRootEl} value={skills} onValueChange={(e) => setSkills(e.value)}>
<InputTags.Items>
{({ item, remove, itemProps }) => (
<Chip.Root {...itemProps}>
<Chip.Label>{item}</Chip.Label>
<Chip.Remove onClick={remove}>×</Chip.Remove>
</Chip.Root>
)}
</InputTags.Items>
<InputTags.Control>
{({ controlProps, add }) => (
<AutoComplete.Root
options={items}
optionLabel="label"
onComplete={onSearch}
onValueChange={(e) => {
if (e.option) add(e.option.label);
}}
>
<AutoComplete.Input {...controlProps} />
<AutoComplete.Portal>
<AutoComplete.Positioner anchor={rootEl}>
<AutoComplete.Popup>
<AutoComplete.List />
</AutoComplete.Popup>
</AutoComplete.Positioner>
</AutoComplete.Portal>
</AutoComplete.Root>
)}
</InputTags.Control>
</InputTags.Root>;
예시 (Examples)
기본 (Basic)
여러 값을 제거 가능한 태그로 입력할 수 있어요.
구분자 (Delimiter)
enter 키를 누르면 새 태그가 추가되고, delimiter 속성으로 추가 키를 정의할 수 있어요. 현재 유효한 값은 ,뿐이라서, 콤마 키를 누르면 새 항목을 만들어요.
중복 허용 (Allow Duplicate)
allowDuplicate를 활성화하면 같은 값을 별도의 태그로 여러 번 추가할 수 있어요.
최대 (Max)
max 속성은 추가할 수 있는 태그 수를 제한해요. 한도에 도달하면 더 이상 태그를 입력할 수 없어요.
항목 (Item)
InputTags.Items는 태그마다 { item, index, remove, itemProps }를 받는 render-prop child를 가져요. 안에 Chip.*, Tag, 일반 <span> 등 원하는 chip류 JSX를 조합하면 돼요.
타입어헤드 (Typeahead)
InputTags는 AutoComplete와 조합해서 타입어헤드 기반 태그 입력을 만들 수 있어요. <InputTags.Control> 안에 <AutoComplete.Root>를 렌더링하고, controlProps를 <AutoComplete.Input>에 펼친 다음, 옵션이 선택되면 AC의 onValueChange에서 add(label)을 호출해요. AC의 onComplete로 필터링을, optionGroupLabel / optionGroupChildren으로 그룹화된 제안을 처리해요.
이벤트 (Events)
onAdd와 onRemove 콜백은 태그가 추가되거나 제거될 때 실행돼요. 로깅, 분석, 검증 같은 커스텀 처리를 위해 태그 값과 인덱스를 제공해요.
플로팅 라벨 (Float Label)
입력이 포커스되거나 채워지면 플로팅 라벨이 표시돼요.
Ifta 라벨 (Ifta Label)
IftaLabel은 필드 안쪽 상단 정렬 라벨을 만들 때 사용해요. 더 자세한 내용은 IftaLabel 문서를 확인하세요.
채움 (Filled)
variant 속성을 filled로 지정하면 기본 outlined 스타일보다 시각적 강조가 높아져요.
무효 (Invalid)
invalid prop으로 무효 상태를 표시해서 검증 실패를 나타낼 수 있어요. 이 스타일은 폼 검증 라이브러리와 통합할 때 유용해요.
비활성화 (Disabled)
disabled가 있으면 요소를 편집하거나 포커스할 수 없어요.
관련 문서 (Related)
서브 컴포넌트 (Sub-Components)
InputTagsRoot, InputTagsItems, InputTagsControl 문서는 Primitive API에서 확인하세요.
훅 (Hooks)
useInputTags 훅 문서는 Headless API에서 확인하세요.
접근성 (Accessibility)
WAI-ARIA 준수 세부사항과 키보드 지원은 InputTags Primitive에서 확인하세요.
테마 (Theming)
CSS 클래스 (CSS Classes)
styled 모드에서 사용하는 클래스 이름 목록이에요.
| ClassName | Description |
|---|---|
| p-inputtags | Class name of the root element |
디자인 토큰 (Design Tokens)
디자인 토큰 목록이에요.
| Token | CSS Variable | Description |
|---|---|---|
| inputchips.background | --p-inputchips-background | Background of root |
| inputchips.disabled.background | --p-inputchips-disabled-background | Disabled background of root |
| inputchips.filled.background | --p-inputchips-filled-background | Filled background of root |
| inputchips.filled.focus.background | --p-inputchips-filled-focus-background | Filled focus background of root |
| inputchips.border.color | --p-inputchips-border-color | Border color of root |
| inputchips.hover.border.color | --p-inputchips-hover-border-color | Hover border color of root |
| inputchips.focus.border.color | --p-inputchips-focus-border-color | Focus border color of root |
| inputchips.invalid.border.color | --p-inputchips-invalid-border-color | Invalid border color of root |
| inputchips.color | --p-inputchips-color | Color of root |
| inputchips.disabled.color | --p-inputchips-disabled-color | Disabled color of root |
| inputchips.placeholder.color | --p-inputchips-placeholder-color | Placeholder color of root |
| inputchips.shadow | --p-inputchips-shadow | Shadow of root |
| inputchips.padding.x | --p-inputchips-padding-x | Padding x of root |
| inputchips.padding.y | --p-inputchips-padding-y | Padding y of root |
| inputchips.border.radius | --p-inputchips-border-radius | Border radius of root |
| inputchips.focus.ring.width | --p-inputchips-focus-ring-width | Focus ring width of root |
| inputchips.focus.ring.style | --p-inputchips-focus-ring-style | Focus ring style of root |
| inputchips.focus.ring.color | --p-inputchips-focus-ring-color | Focus ring color of root |
| inputchips.focus.ring.offset | --p-inputchips-focus-ring-offset | Focus ring offset of root |
| inputchips.focus.ring.shadow | --p-inputchips-focus-ring-shadow | Focus ring shadow of root |
| inputchips.transition.duration | --p-inputchips-transition-duration | Transition duration of root |
| inputchips.chip.border.radius | --p-inputchips-chip-border-radius | Border radius of chip |
| inputchips.chip.focus.background | --p-inputchips-chip-focus-background | Focus background of chip |
| inputchips.chip.color | --p-inputchips-chip-color | Color of chip |
더 알아보기 (Learn more)
- InputTags 서브 컴포넌트의 세부 API를 Primitive 문서에서 살펴보세요.
- AutoComplete와 조합해 타입어헤드 기반 태그 입력을 구성해 보세요.