Back to Medusa

{metadata.title}

www/apps/ui/app/components/select/page.mdx

2.18.02.1 KB
Original Source

import { ComponentExample } from "@/components/ComponentExample" import { ComponentReference } from "@/components/ComponentReference"

export const metadata = { title: Select, }

{metadata.title}

A component that displays a select form input using Medusa's design system.

In this guide, you'll learn how to use the Select component.

<ComponentExample name="select-demo" />

Usage

tsx
import { Select } from "@medusajs/ui"
tsx
<Select>
  <Select.Trigger>
    <Select.Value placeholder="Placeholder" />
  </Select.Trigger>
  <Select.Content>
    {items.map((item) => (
      <Select.Item key={item.value} value={item.value}>
        {item.label}
      </Select.Item>
    ))}
  </Select.Content>
</Select>

API Reference

<ComponentReference mainComponent="Select" componentsToShow={[ "Select", "Select.Trigger", "Select.Value", "Select.Group", "Select.Label", "Select.Item", "Select.Content" ]} />


Examples

Small Select

<ComponentExample name="select-small" />

Select Item-Aligned Position

<ComponentExample name="select-item-aligned" />

Disabled Select

<ComponentExample name="select-disabled" />

Select with Grouped Items

<ComponentExample name="select-grouped-items" />

Controlled Select

<ComponentExample name="select-controlled" hideFeedback />

Empty Values

A Select.Item can't have an empty string as its value prop. If you do, you'll get the error A <Select.Item /> must have a value prop that is not an empty string. This is because the Select reserves the empty string to clear the selection and show the placeholder.

To add an option like None, use a non-empty placeholder value, then convert it in your change handler:

tsx
<Select
  onValueChange={(value) =>
    handleChange(value === "none" ? null : value)
  }
>
  <Select.Trigger>
    <Select.Value placeholder="Placeholder" />
  </Select.Trigger>
  <Select.Content>
    <Select.Item value="none">None</Select.Item>
    {items.map((item) => (
      <Select.Item key={item.value} value={item.value}>
        {item.label}
      </Select.Item>
    ))}
  </Select.Content>
</Select>