TagGroup
TagGroup은 키보드 탐색, 선택, 제거를 지원하는 포커스 가능한 태그 목록 컴포넌트예요.
출처: HeroUI TagGroup
본문
사용법 (Usage)
import { TagGroup } from '@heroui/react';
{
"name": "tag-group-basic"
}
구조 (Anatomy)
import { TagGroup, Tag, Label, Description, ErrorMessage } from '@heroui/react';
export default () => (
<TagGroup>
<Label />
<TagGroup.List>
<Tag>
<Tag.RemoveButton />
</Tag>
</TagGroup.List>
<Description />
<ErrorMessage />
</TagGroup>
)
예시 (Examples)
크기 (Sizes)
{
"name": "tag-group-sizes"
}
변형 (Variants)
{
"name": "tag-group-variants"
}
비활성화 (Disabled)
{
"name": "tag-group-disabled"
}
선택 모드 (Selection Modes)
{
"name": "tag-group-selection-modes"
}
제어 (Controlled)
{
"name": "tag-group-controlled"
}
오류 메시지 사용 (With Error Message)
{
"name": "tag-group-with-error-message"
}
목록 데이터 사용 (With List Data)
{
"name": "tag-group-with-list-data"
}
접두어 사용 (With Prefix)
{
"name": "tag-group-with-prefix"
}
제거 버튼 사용 (With Remove Button)
{
"name": "tag-group-with-remove-button"
}
렌더 함수 (Render Function)
{
"name": "tag-group-render-function"
}
커스터마이징 (Customization)
Tailwind CSS
{
"name": "tag-group-custom-styles"
}
Global CSS
TagGroup 컴포넌트 클래스를 커스터마이징하려면 @layer components 지시어를 사용할 수 있어요.
더 알아보기.
@layer components {
.tag-group {
@apply flex flex-col gap-2;
}
.tag-group__list {
@apply flex flex-wrap gap-2;
}
.tag {
@apply rounded-full px-3 py-1;
}
.tag__remove-button {
@apply ml-1;
}
}
스타일 참조 (Styling Reference)
HeroUI는 컴포넌트 변형과 상태를 재사용하고 커스터마이징하기 쉽게 만들기 위해 BEM 방법론을 따르고 있어요.
CSS 클래스 (CSS Classes)
TagGroup 컴포넌트는 다음 CSS 클래스를 사용해요 (소스 스타일 보기 및 tag.css):
기본 클래스 (Base Classes)
.tag-group- 태그 그룹 기본 컨테이너.tag-group__list- 태그 목록 컨테이너.tag- 기본 태그 스타일.tag__remove-button- 제거 버튼 트리거
슬롯 클래스 (Slot Classes)
.tag-group [slot="description"]- 설명 슬롯 스타일.tag-group [slot="errorMessage"]- 오류 메시지 슬롯 스타일
크기 클래스 (Size Classes)
.tag--sm- 작은 크기 태그.tag--md- 중간 크기 태그 (기본값).tag--lg- 큰 크기 태그
변형 클래스 (Variant Classes)
.tag--default- 기본 변형.tag--surface- surface 배경을 가진 표면 변형
상태 클래스 (State Classes)
.tag[data-selected="true"]- 선택된 태그 상태.tag[data-disabled="true"]- 비활성화된 태그 상태.tag[data-hovered="true"]- 호버된 태그 상태.tag[data-pressed="true"]- 눌린 태그 상태.tag[data-focus-visible="true"]- 포커스된 태그 상태 (키보드 포커스)
상호작용 상태 (Interactive States)
컴포넌트는 유연성을 위해 CSS 가상 클래스와 데이터 속성을 모두 지원해요:
- 호버 (Hover): 태그의
:hover또는[data-hovered="true"] - 포커스 (Focus): 태그의
:focus-visible또는[data-focus-visible="true"] - 누름 (Pressed): 태그의
:active또는[data-pressed="true"] - 선택 (Selected): 태그의
[data-selected="true"]또는[aria-selected="true"] - 비활성화 (Disabled): 태그의
:disabled또는[data-disabled="true"]
API 참조 (API Reference)
TagGroup
| Prop | Type | Default | Description |
|---|---|---|---|
selectionMode |
"none" | "single" | "multiple" |
"none" |
허용되는 선택 유형 |
selectedKeys |
Selection |
- | 현재 선택된 키 (제어) |
defaultSelectedKeys |
Selection |
- | 초기 선택 키 (비제어) |
onSelectionChange |
(keys: Selection) => void |
- | 선택이 변경될 때 호출되는 핸들러 |
disabledKeys |
Iterable<Key> |
- | 비활성화된 태그의 키 |
isDisabled |
boolean |
- | 태그 그룹이 비활성화되었는지 여부 |
onRemove |
(keys: Set<Key>) => void |
- | 태그가 제거될 때 호출되는 핸들러 |
size |
"sm" | "md" | "lg" |
"md" |
그룹 내 태그의 크기 |
variant |
"default" | "surface" |
"default" |
태그의 시각적 변형 |
className |
string |
- | 추가 CSS 클래스 |
children |
ReactNode | RenderFunction |
- | TagGroup 콘텐츠 또는 렌더 함수 |
render |
DOMRenderFunction<keyof React.JSX.IntrinsicElements, undefined> |
- | 기본 DOM 요소를 커스텀 렌더 함수로 대체합니다. |
TagGroup.List
| Prop | Type | Default | Description |
|---|---|---|---|
items |
Iterable<T> |
- | 태그 목록에 표시할 항목 |
renderEmptyState |
() => ReactNode |
- | 목록이 비어 있을 때 렌더링할 함수 |
className |
string |
- | 추가 CSS 클래스 |
children |
ReactNode | RenderFunction |
- | TagList 콘텐츠 또는 렌더 함수 |
render |
DOMRenderFunction<keyof React.JSX.IntrinsicElements, TagListRenderProps> |
- | 기본 DOM 요소를 커스텀 렌더 함수로 대체합니다. |
Tag
| Prop | Type | Default | Description |
|---|---|---|---|
id |
Key |
- | 태그의 고유 식별자 |
textValue |
string |
- | 접근성을 위해 사용되는 태그 콘텐츠의 문자열 표현 |
isDisabled |
boolean |
- | 태그가 비활성화되었는지 여부 |
className |
string |
- | 추가 CSS 클래스 |
children |
ReactNode | RenderFunction |
- | Tag 콘텐츠 또는 렌더 함수 |
render |
DOMRenderFunction<keyof React.JSX.IntrinsicElements, TagRenderProps> |
- | 기본 DOM 요소를 커스텀 렌더 함수로 대체합니다. |
참고: size, variant는 부모 TagGroup 컴포넌트에서 상속되며 개별 Tag 컴포넌트에 직접 설정할 수 없어요.
Tag.RemoveButton
| Prop | Type | Default | Description |
|---|---|---|---|
className |
string |
- | 추가 CSS 클래스 |
children |
ReactNode |
- | 커스텀 제거 버튼 콘텐츠 (기본값은 닫기 아이콘) |
참고: Tag.RemoveButton 컴포넌트는 SearchField.ClearButton와 유사한 커스터마이징을 지원해요. TagGroup에 onRemove를 제공하면:
- 자동 렌더링 (Auto-rendering):
Tag자식에 커스텀Tag.RemoveButton이 없으면 기본 제거 버튼이 자동으로 렌더링돼요. - 커스텀 버튼 (Custom button):
Tag의 자식으로 커스텀Tag.RemoveButton을 제공하면 자동 렌더링된 버튼 대신 사용돼요. - 커스텀 아이콘 (Custom icon):
Tag.RemoveButton자식에 커스텀 콘텐츠(아이콘 등)를 전달해 모양을 커스터마이징할 수 있어요.
예시 - 자동 렌더링 (기본값):
<TagGroup onRemove={handleRemove}>
<TagGroup.List>
<Tag id="news">News</Tag>
{/* Remove button is automatically rendered */}
</TagGroup.List>
</TagGroup>
예시 - 아이콘이 있는 커스텀 RemoveButton:
<TagGroup onRemove={handleRemove}>
<TagGroup.List>
<Tag id="news">
News
<Tag.RemoveButton>
<CustomIcon />
</Tag.RemoveButton>
</Tag>
</TagGroup.List>
</TagGroup>
예시 - 렌더 프로퍼티의 커스텀 RemoveButton:
<Tag id="news">
{(renderProps) => (
<>
News
{!!renderProps.allowsRemoving && (
<Tag.RemoveButton>
<CustomIcon />
</Tag.RemoveButton>
)}
</>
)}
</Tag>
렌더 프로퍼티 (Render Props)
TagGroup.List와 함께 렌더 함수를 사용하면 다음 값이 제공돼요:
| Prop | Type | Description |
|---|---|---|
isSelected |
boolean |
태그가 선택되었는지 여부 |
isDisabled |
boolean |
태그가 비활성화되었는지 여부 |
isHovered |
boolean |
태그가 호버되었는지 여부 |
isPressed |
boolean |
태그가 눌렸는지 여부 |
isFocused |
boolean |
태그가 포커스되었는지 여부 |
isFocusVisible |
boolean |
태그에 키보드 포커스가 있는지 여부 |