Grapes homepage1.57.0
  • Guide
  • Tokens
  • Components
    Grapes on GithubGrapes on Figma
    Interaction
    • Button
    • IconButton
    • FloatingActionBar
    • Link
    Icons
    • Icon
    • HighlightIcon
    Form
    • AmountInput
    • Autocomplete
    • AutocompleteMultiple
    • AutocompletePlace
    • CheckboxBox
    • CheckboxField
    • DatePicker
    • FormField
    • Input
    • OptionGroup
    • PasswordInput
    • PhoneInput
    • RadioBox
    • RadioField
    • RadioGroup
    • Select
    • SwitchField
    • TextArea
    • TextInput
    • Upload
    • UploadButton
    Feedback
    • Badge
    • Banner
    • Callout
    • EmptyState
    • Modal
    • ModalSlideshow
    • DeprecatedModalSlideshow
    • PageModal
    • Skeleton
    • Tag
    • Toast
    • Tooltip
    Data display
    • Accordion
    • Avatar
    • Box
    • Calendar
    • CalendarRange
    • CollapsibleList
    • FileCard
    • InfoTip
    • ListBox
    • ListView
    • Panel
    • SidePanel
    • DeprecatedPreview
    • Table
    • Timeline
    • useDateFormatter
    Navigation
    • DropdownItem
    • DropdownMenu
    • Navigation
    • NavigationItem
    • Popover
    • Tabs

    ListBox

    The ListBox component is used to display data.

    View source code
    • Usage
    • Props
    • Accessibility

    Basic usage

    const options = [
      {
        id: "michael",
        name: "Michael Murphy",
        birthDate: "19/09/1979",
        avatar: "/aurelien.webp",
        description: "Growth team",
      },
      // other options
    ];
    
    const Demo = () => {
      const [activeOption, setActiveOption] = useState<string | undefined>();
    
      return (
        <ListBox
          getOptionId={(option) => option.id}
          options={options}
          getIsOptionActive={(option) => option.id === activeOption}
          onOptionClick={(option) => {
            setActiveOption(option.id);
          }}
          aria-label="Employees"
        >
          {(option, titleId) => (
            <div className="flex gap-16 items-center">
              <Avatar variant="circle" src={option.avatar} text={option.name} />
              <div className="flex-grow">
                <div className="title-m" id={titleId}>
                  {option.name}
                </div>
                <div className="body-m text-content-secondary-bg-primary mt-4">
                  {option.description}
                </div>
              </div>
              <span>{option.birthDate}</span>
            </div>
          )}
        </ListBox>
      );
    };
    

    Empty state

    If no data has been provided, the list's empty state will appear. The title is mandatory, and the subtitle is optional.

    • No employee found
      Add employees to get started
    <ListBox
      options={[]}
      emptyState={{
        title: "No employee found",
        subtitle: "Add employees to get started",
      }}
      {...otherProps}
    />
    

    Row height

    The ListBox component has two variants: normal and compact. The compact mode makes the rows smaller.

    <ListBox rowHeight="compact" {...otherProps} />
    

    Header / Footer

    The header and footer props allow you to specify a header or a footer for the ListBox component.

    <ListBox
      header={<div>Members</div>}
      footer={
        <div className="my-8 mx-auto">
          <Button fit="content" text="Load more" variant="secondaryNeutral" />
        </div>
      }
      {...otherProps}
    />
    

    Disabled rows

    The getIsOptionDisabled prop allows you to disable options in the ListBox. These options will be neither clickable nor selectable.

    <ListBox
      getIsOptionDisabled={(option) => option.id === "laura"}
      {...otherProps}
    />
    

    Groups

    ListBox options can be grouped, with the groupBy and renderGroupedOptionsHeader props.

    <ListBox
      groupBy={(option) => {
        const date = new Date(option.birthDate);
        return `${date.getFullYear()}`;
      }}
      renderGroupedOptionsHeader={(value, aggregatedOptions) => {
        const aggregatedCount = aggregatedOptions.length;
        return (
          <div className="flex justify-between">
            <span>{value}</span>
            <span>
              {aggregatedCount} {aggregatedCount > 1 ? "members" : "member"}
            </span>
          </div>
        );
      }}
      {...otherProps}
    />
    

    Selectable

    If you want to make a ListBox component selectable, you will need to specify the following props: onOptionChange, onAllOptionsChange, and checkedOptionIds. Options that have been disabled with getIsOptionDisabled are not selectable. You can also specify directly if an option should be selectable with getIsOptionCheckable, without having to disable the row.

    const [optionsSelected, setOptionSelected] = useState<string[]>([]);
    
    return (
      <ListBox
        onOptionChange={(_, id, checked) => {
          setOptionSelected((options) => {
            if (checked) {
              return options.concat(id);
            }
            return options.filter((optionId) => optionId !== id);
          });
        }}
        onAllOptionsChange={(_, ids, checked) => {
          setOptionSelected(checked ? ids : []);
        }}
        checkedOptionIds={optionsSelected}
        {...otherProps}
      />
    );
    

    Option variant

    The getOptionVariant prop allows you to specify a variant (alert or warning) for the different options.

    <ListBox
      getOptionVariant={(option) => {
        switch (option.id) {
          case "george":
            return "alert";
          case "lewis":
            return "warning";
          default:
            return undefined;
        }
      }}
      {...otherProps}
    />
    
    • M
      Michael Murphy
      Growth team
      1979-09-19
    • N
      Nayden Lennart
      Design team
      1980-03-01
    • N
      Nicolas Harvey
      Growth team
      1980-06-05
    • L
      Lewis Barker
      Design team
      1980-07-30
    • G
      George Gray
      Design team
      1980-07-31
    • L
      Laura Lagarde
      Marketing team
      1981-02-13
    • M
      Michael Murphy
      1979-09-19
    • N
      Nayden Lennart
      1980-03-01
    • N
      Nicolas Harvey
      1980-06-05
    • L
      Lewis Barker
      1980-07-30
    • G
      George Gray
      1980-07-31
    • L
      Laura Lagarde
      1981-02-13
    • Members
    • M
      Michael Murphy
      Growth team
      1979-09-19
    • N
      Nayden Lennart
      Design team
      1980-03-01
    • N
      Nicolas Harvey
      Growth team
      1980-06-05
    • L
      Lewis Barker
      Design team
      1980-07-30
    • G
      George Gray
      Design team
      1980-07-31
    • L
      Laura Lagarde
      Marketing team
      1981-02-13
    • M
      Michael Murphy
      Growth team
      1979-09-19
    • N
      Nayden Lennart
      Design team
      1980-03-01
    • N
      Nicolas Harvey
      Growth team
      1980-06-05
    • L
      Lewis Barker
      Design team
      1980-07-30
    • G
      George Gray
      Design team
      1980-07-31
    • L
      Laura Lagarde
      Marketing team
      1981-02-13
    • 19791 member
      • M
        Michael Murphy
        Growth team
        1979-09-19
    • 19804 members
      • N
        Nayden Lennart
        Design team
        1980-03-01
      • N
        Nicolas Harvey
        Growth team
        1980-06-05
      • L
        Lewis Barker
        Design team
        1980-07-30
      • G
        George Gray
        Design team
        1980-07-31
    • 19811 member
      • L
        Laura Lagarde
        Marketing team
        1981-02-13
    • Members
    • M
      Michael Murphy
      Growth team
      1979-09-19
    • N
      Nayden Lennart
      Design team
      1980-03-01
    • N
      Nicolas Harvey
      Growth team
      1980-06-05
    • L
      Lewis Barker
      Design team
      1980-07-30
    • G
      George Gray
      Design team
      1980-07-31
    • L
      Laura Lagarde
      Marketing team
      1981-02-13
    • M
      Michael Murphy
      Growth team
      1979-09-19
    • N
      Nayden Lennart
      Design team
      1980-03-01
    • N
      Nicolas Harvey
      Growth team
      1980-06-05
    • L
      Lewis Barker
      Design team
      1980-07-30
    • G
      George Gray
      Design team
      1980-07-31
    • L
      Laura Lagarde
      Marketing team
      1981-02-13