ListBox

The ListBox component is used to display data.

View source code

Basic usage

  • 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
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-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.

<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.

  • 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
<ListBox rowHeight="compact" {...otherProps} />

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

  • 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
<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.

  • 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
<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.

  • 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
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.

  • 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
<ListBox
  getOptionVariant={(option) => {
    switch (option.id) {
      case "george":
        return "alert";
      case "lewis":
        return "warning";
      default:
        return undefined;
    }
  }}
  {...otherProps}
/>