Skip to content
This repository was archived by the owner on Jul 31, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/components/Combobox/index.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Meta, StoryObj } from '@storybook/react-vite'
import { useState } from 'react'
import { Combobox } from './index'

const meta: Meta<typeof Combobox> = {
Expand Down Expand Up @@ -195,3 +196,33 @@ export const SearchableGroupedList: Story = {
searchPlaceholder: 'Search groups and items...',
},
}

// Create new item example - demonstrates dynamic options
export const WithCreateOption: Story = {
render: () => {
const [options, setOptions] = useState(generateList(10))
const [value, setValue] = useState<string | undefined>()

return (
<Combobox
options={options}
value={value!}
onValueChange={setValue}
placeholder="Search or create..."
searchPlaceholder="Type to search or create..."
createOptions={{
handleCreate: (query) => {
const newValue = query.toLowerCase().replace(/\s+/g, '-')
setOptions([...options, { value: newValue, label: query }])
setValue(newValue)
},
renderCreatePrompt: (query) => (
<span>
Create <strong>{query}</strong>
</span>
),
}}
/>
)
},
}
19 changes: 19 additions & 0 deletions src/components/Combobox/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ interface ComboboxBaseProps<T extends string = string> {
emptyText?: string
searchPlaceholder?: string
iconOnly?: boolean
createOptions?: {
handleCreate: (search: string) => void
renderCreatePrompt?: (search: string) => JSX.Element
}
}

export type ComboboxProps<T extends string = string> = ComboboxBaseProps<T> &
Expand All @@ -72,7 +76,9 @@ export function Combobox<T extends string = string>({
placeholder = 'Select option...',
emptyText = 'No option found.',
searchPlaceholder = 'Search...',

iconOnly = false,
createOptions,
}: ComboboxProps<T>) {
const [open, setOpen] = React.useState(false)
const [search, setSearch] = React.useState('')
Expand Down Expand Up @@ -185,6 +191,19 @@ export function Combobox<T extends string = string>({
)}
/>
)}
{(() => {
if (!createOptions || search.length === 0) return null
const { renderCreatePrompt, handleCreate } = createOptions
return (
<CommandGroup>
<CommandItem onSelect={() => handleCreate(search)}>

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Ensure consistent popover behavior on creation.

When the create option is selected, the popover remains open and the search text isn't cleared. This differs from selecting a regular option, which closes the popover and clears the search via handleSelect. The handleCreate callback should trigger the same cleanup behavior for consistent UX.

Fix in Cursor Fix in Web

{renderCreatePrompt
? renderCreatePrompt(search)
: `Create ${search}`}
</CommandItem>
</CommandGroup>
)
})()}
</CommandList>
</Command>
</PopoverContent>
Expand Down