본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

리스트박스(Listbox) v2 → v3 마이그레이션

원문 보기 위키 갱신

HeroUI v2의 Listbox를 v3으로 옮길 때 필요한 변경을 정리한 가이드예요. v3에서 컴포넌트 이름이 대문자 B를 쓰는 ListBox로 바뀌었고, 하위 요소들도 누구나 알 수 있게 구조가 정리되었어요. 구조와 이름, 항목 식별 방법이 어떻게 달라지는지 차근차근 살펴볼게요.

출처: HeroUI

본문

참고: 완전한 API 레퍼런스, 스타일링 가이드, 고급 예제는 v3 ListBox 문서를 참고하세요. 이 가이드는 HeroUI v2에서 마이그레이션하는 부분에만 집중합니다.

구조 변화

v2: 분리된 컴포넌트

v2에서는 Listbox가 분리된 컴포넌트를 사용했어요:

import { Listbox, ListboxItem, ListboxSection } from "@heroui/react";

export default function App() {
  return (
    <Listbox>
      <ListboxItem key="1">Item 1</ListboxItem>
    </Listbox>
  );
}

v3: 합성 컴포넌트

v3에서는 ListBox가 합성 컴포넌트를 사용해요:

import { ListBox, Label } from "@heroui/react";

export default function App() {
  return (
    <ListBox>
      <ListBox.Item id="1" textValue="Item 1">
        <Label>Item 1</Label>
      </ListBox.Item>
    </ListBox>
  );
}

주요 변경 사항

1. 컴포넌트 이름

v2: Listbox, ListboxItem, ListboxSection v3: ListBox, ListBox.Item, ListBox.Section

2. 항목 식별(Item Identification)

v2: React의 key를 목록 재조정(reconciliation)과 항목 식별(선택, 포커스)에 모두 사용했어요. v3: 상태/포커스에는 id를, 접근성(콘텐츠가 일반 텍스트가 아닐 때)에는 textValue를 사용하고, 목록의 항목에는 React key를 유지해요.

3. Prop 변경

v2 Prop v3 Location Notes
key (for state) ListBox.Item Use id for item identity (state)
- textValue (on ListBox.Item) For accessibility (type-ahead)
variant, color variant on ListBox or ListBox.Item Simplified to "default" | "danger" (no color prop)
onAction ListBox Callback fired when an item is pressed ((key: Key) => void)
disabledKeys ListBox Same — set of keys for items that should be non-interactive
startContent, endContent - Place icons manually in item content
description Description Use Description component
title (on Section) Header Use Header component
topContent, bottomContent - Removed (handle separately)
itemClasses, classNames - Use className on parts
hideSelectedIcon - Omit ListBox.ItemIndicator
disableAnimation - Removed
isVirtualized, virtualization React Aria Virtualizer Use React Aria's <Virtualizer> wrapper (see example below)
selectedKeys ListBox Same (uses Selection type Set)

마이그레이션 예제

선택(Selection)

v2:

import { useState } from "react";

{/* Single selection */}
const [singleSelected, setSingleSelected] = useState(new Set(["text"]));

<Listbox
  selectedKeys={singleSelected}
  selectionMode="single"
  onSelectionChange={setSingleSelected}
>
  <ListboxItem key="text">Text</ListboxItem>
  <ListboxItem key="number">Number</ListboxItem>
</Listbox>

{/* Multiple selection */}
const [multiSelected, setMultiSelected] = useState(new Set(["text"]));

<Listbox
  selectedKeys={multiSelected}
  selectionMode="multiple"
  onSelectionChange={setMultiSelected}
>
  <ListboxItem key="text">Text</ListboxItem>
  <ListboxItem key="number">Number</ListboxItem>
</Listbox>

v3:

import { useState } from "react";
import type { Selection } from "@heroui/react";

{/* Single selection */}
const [singleSelected, setSingleSelected] = useState<Selection>(new Set(["text"]));

<ListBox
  selectedKeys={singleSelected}
  selectionMode="single"
  onSelectionChange={setSingleSelected}
>
  <ListBox.Item id="text" textValue="Text">
    <Label>Text</Label>
    <ListBox.ItemIndicator />
  </ListBox.Item>
  <ListBox.Item id="number" textValue="Number">
    <Label>Number</Label>
    <ListBox.ItemIndicator />
  </ListBox.Item>
</ListBox>

{/* Multiple selection */}
const [multiSelected, setMultiSelected] = useState<Selection>(new Set(["text"]));

<ListBox
  selectedKeys={multiSelected}
  selectionMode="multiple"
  onSelectionChange={setMultiSelected}
>
  <ListBox.Item id="text" textValue="Text">
    <Label>Text</Label>
    <ListBox.ItemIndicator />
  </ListBox.Item>
  <ListBox.Item id="number" textValue="Number">
    <Label>Number</Label>
    <ListBox.ItemIndicator />
  </ListBox.Item>
</ListBox>

설명과 함께

v2:

<ListboxItem
  key="new"
  description="Create a new file"
>
  New file
</ListboxItem>

v3:

import { Description, Label } from "@heroui/react";

<ListBox.Item id="new" textValue="New file">
  <Label>New file</Label>
  <Description>Create a new file</Description>
</ListBox.Item>

아이콘과 함께

v2:

<ListboxItem
  key="new"
  startContent={<AddIcon />}
>
  New file
</ListboxItem>

v3:

<ListBox.Item id="new" textValue="New file">
  <AddIcon />
  <Label>New file</Label>
</ListBox.Item>

섹션(Sections)과 함께

v2:

<Listbox>
  <ListboxSection title="Actions">
    <ListboxItem key="new">New file</ListboxItem>
    <ListboxItem key="edit">Edit file</ListboxItem>
  </ListboxSection>
  <ListboxSection title="Danger zone">
    <ListboxItem key="delete">Delete</ListboxItem>
  </ListboxSection>
</Listbox>

v3:

import { Header, Label, Separator } from "@heroui/react";

<ListBox>
  <ListBox.Section>
    <Header>Actions</Header>
    <ListBox.Item id="new" textValue="New file">
      <Label>New file</Label>
    </ListBox.Item>
    <ListBox.Item id="edit" textValue="Edit file">
      <Label>Edit file</Label>
    </ListBox.Item>
  </ListBox.Section>
  <Separator />
  <ListBox.Section>
    <Header>Danger zone</Header>
    <ListBox.Item id="delete" textValue="Delete" variant="danger">
      <Label>Delete</Label>
    </ListBox.Item>
  </ListBox.Section>
</ListBox>

커스텀 인디케이터와 함께

v2:

<ListboxItem
  key="1"
  selectedIcon={<CustomCheckIcon />}
>
  Item 1
</ListboxItem>

v3:

<ListBox.Item id="1" textValue="Item 1">
  <Label>Item 1</Label>
  <ListBox.ItemIndicator>
    {({isSelected}) =>
      isSelected ? <CustomCheckIcon /> : null
    }
  </ListBox.ItemIndicator>
</ListBox.Item>

variant Prop

v3에서 ListBox와 ListBox.Item 모두 variant prop을 받고, 값은 "default"(기본값) 또는 "danger"를 가져요. 루트 ListBox에 variant를 설정하면 모든 항목에 적용되고, 개별 ListBox.Item에 설정하면 그 항목에 대해 루트 값을 덮어써요.

{/* Root-level variant — all items inherit "danger" styling */}
<ListBox variant="danger">
  <ListBox.Item id="delete" textValue="Delete">
    <Label>Delete</Label>
  </ListBox.Item>
</ListBox>

{/* Per-item variant */}
<ListBox>
  <ListBox.Item id="edit" textValue="Edit">
    <Label>Edit</Label>
  </ListBox.Item>
  <ListBox.Item id="delete" textValue="Delete" variant="danger">
    <Label>Delete</Label>
  </ListBox.Item>
</ListBox>

onAction 핸들러

onAction 콜백은 항목을 누를 때(클릭 또는 Enter) 실행돼요. 항목의 id를 Key로 받아요.

<ListBox onAction={(key) => alert(`Action on ${key}`)}>
  <ListBox.Item id="copy" textValue="Copy">
    <Label>Copy</Label>
  </ListBox.Item>
  <ListBox.Item id="paste" textValue="Paste">
    <Label>Paste</Label>
  </ListBox.Item>
</ListBox>

비활성 키(Disabled Keys)

disabledKeys를 사용해 특정 항목을 비상호작용으로 만들 수 있어요:

<ListBox disabledKeys={new Set(["paste"])}>
  <ListBox.Item id="copy" textValue="Copy">
    <Label>Copy</Label>
  </ListBox.Item>
  <ListBox.Item id="paste" textValue="Paste">
    <Label>Paste</Label>
  </ListBox.Item>
</ListBox>

ListBox.Item의 렌더 props

ListBox.Item은 렌더 props를 지원해서 현재 상호작용 상태에 접근할 수 있어요. 사용 가능한 렌더 prop 값은 isSelected, isFocused, isDisabled, isPressed예요:

<ListBox selectionMode="single">
  <ListBox.Item id="item1" textValue="Item 1">
    {({isSelected, isFocused, isDisabled, isPressed}) => (
      <>
        <Label className={isSelected ? "font-bold" : ""}>Item 1</Label>
        {isSelected && <ListBox.ItemIndicator />}
      </>
    )}
  </ListBox.Item>
</ListBox>

가상화(Virtualization)

가상화는 v3에서 React Aria의 <Virtualizer> 컴포넌트를 통해 여전히 지원돼요. 큰 목록을 효율적으로 렌더링하려면 ListBox 항목을 Virtualizer로 감싸세요:

import {Virtualizer} from "react-aria-components";

<ListBox
  aria-label="Large list"
  items={items}
  selectionMode="multiple"
>
  <Virtualizer>
    {(item) => (
      <ListBox.Item id={item.id} textValue={item.name}>
        <Label>{item.name}</Label>
      </ListBox.Item>
    )}
  </Virtualizer>
</ListBox>

컴포넌트 구조(Anatomy)

v3 ListBox는 다음과 같은 구조를 가져요:

ListBox (Root)
  ├── ListBox.Item
  │   ├── Icon (optional, manual placement)
  │   ├── Label (required)
  │   ├── Description (optional)
  │   └── ListBox.ItemIndicator (optional)
  └── ListBox.Section (optional)
      ├── Header (optional)
      └── ListBox.Item

요약

  1. 컴포넌트 이름: Listbox → ListBox, ListboxItem → ListBox.Item, ListboxSection → ListBox.Section
  2. 항목 구조: Label, Description, ListBox.ItemIndicator 컴포넌트를 반드시 사용
  3. 아이콘: startContent/endContent props 대신 수동 배치
  4. 섹션: title prop 대신 Header 컴포넌트 사용
  5. variant Prop: variant와 color가 ListBox와 ListBox.Item 양쪽의 단일 variant prop("default" | "danger")으로 대체됨
  6. onAction: 항목 클릭 처리를 위한 ListBox의 새 onAction 콜백
  7. disabledKeys: 특정 항목을 비활성화하는 ListBox의 disabledKeys 지원
  8. 렌더 props: ListBox.Item이 렌더 props로 isSelected, isFocused, isDisabled, isPressed 제공
  9. 콘텐츠 Props 제거: topContent, bottomContent — 별도로 처리
  10. 가상화: React Aria의 <Virtualizer> 컴포넌트로 계속 지원 (isVirtualized prop 대체)
  11. Selection 타입: 배열 대신 Selection 타입(Set) 사용

더 알아보기 (Learn more)