Jacob Paris

A searchable dropdown menu that supports creating new options on the fly. For multi-selection, see Combobox Multiple

This is a form component, so wrap it in a <Form> and its data will be submitted along with the rest of the form. This page submits to local storage, which you can clear by opening the browser's developer console and running localStorage.clear().

Installation

You can either copy/paste the code into your project directly or use Sly CLI to install the package.

npx @sly-cli/sly@latest add jacobparis/ui combobox

Single select

The default settings only allow selecting one item and only from the provided options.

Submit the combobox in a form and handle the form submission in your action function.


export async function action({ request }: ActionFunctionArgs) {
  const formData = await request.formData()

  // Save it wherever you want
  const genreId = formData.get("genreId")
}
            

Single-select + create

If you want the user to be able to add their own items, add these props

  • createName="newGenreName" - The FormData key for the new item
  • createLabel="Name:" - The label shown in the dropdown and in the input when the user has selected a new item

Submit the combobox in a form and handle the form submission in your action function.


export async function action({ request }: ActionFunctionArgs) {
  const formData = await request.formData()

  let genreId = formData.get("genreId")
  
  // If this exists, the user created a new item
  const newGenreName = formData.get("newGenreName")
  if (newGenreName) {
    const genre = await db.genre.create({
      data: { name: newGenreName },
    })

    genreId = genre.id
  }

  // Now save the genreId wherever you want
}
            
Professional headshot
Moulton
Moulton

Hey there! I'm a developer, designer, and digital nomad building cool things with Remix, and I'm also writing Moulton, the Remix Community Newsletter

About once per month, I send an email with:

  • New guides and tutorials
  • Upcoming talks, meetups, and events
  • Cool new libraries and packages
  • What's new in the latest versions of Remix

Stay up to date with everything in the Remix community by entering your email below.

Unsubscribe at any time.