-
Notifications
You must be signed in to change notification settings - Fork 80
feat: create new option #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ import { | |
ChevronDown, | ||
XIcon, | ||
WandSparkles, | ||
Plus | ||
} from "lucide-react"; | ||
|
||
import { cn } from "@/lib/utils"; | ||
|
@@ -115,6 +116,18 @@ interface MultiSelectProps | |
* Optional, can be used to add custom styles. | ||
*/ | ||
className?: string; | ||
|
||
/** | ||
* If true, allows creating new options that are not in the original list. | ||
* Optional, defaults to false. | ||
*/ | ||
creatable?: boolean; | ||
|
||
/** | ||
* Callback function triggered when a new option is created. | ||
* Required if creatable is true. | ||
*/ | ||
onCreate?: (value: string) => void; | ||
} | ||
|
||
export const MultiSelect = React.forwardRef< | ||
|
@@ -133,6 +146,8 @@ export const MultiSelect = React.forwardRef< | |
modalPopover = false, | ||
asChild = false, | ||
className, | ||
creatable = false, | ||
onCreate, | ||
...props | ||
}, | ||
ref | ||
|
@@ -141,6 +156,7 @@ export const MultiSelect = React.forwardRef< | |
React.useState<string[]>(defaultValue); | ||
const [isPopoverOpen, setIsPopoverOpen] = React.useState(false); | ||
const [isAnimating, setIsAnimating] = React.useState(false); | ||
const [inputValue, setInputValue] = React.useState(""); | ||
|
||
React.useEffect(() => { | ||
if (JSON.stringify(selectedValues) !== JSON.stringify(defaultValue)) { | ||
|
@@ -194,6 +210,18 @@ export const MultiSelect = React.forwardRef< | |
} | ||
}; | ||
|
||
const handleInputChange = (value: string) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't being used, and can be removed |
||
setInputValue(value); | ||
}; | ||
|
||
const handleCreateOption = () => { | ||
if (creatable && onCreate && inputValue) { | ||
onCreate(inputValue); | ||
toggleOption(inputValue); | ||
setInputValue(""); | ||
} | ||
}; | ||
|
||
return ( | ||
<Popover | ||
open={isPopoverOpen} | ||
|
@@ -295,7 +323,16 @@ export const MultiSelect = React.forwardRef< | |
onKeyDown={handleInputKeyDown} | ||
/> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This I think it would be better to use the controlled version of the input by passing in It would also be a good idea to rename them to |
||
<CommandList> | ||
<CommandEmpty>No results found.</CommandEmpty> | ||
<CommandEmpty> | ||
{creatable ? ( | ||
<CommandItem onSelect={handleCreateOption}> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This won't be displayed, because the filter will automatically hide this <CommandEmpty className={cn({ p0: !!onCreate })}>
{onCreate ? (
<CommandGroup forceMount>
<CommandItem
forceMount
onSelect={() => {
if (search) {
onCreate(search);
toggleOption(search);
setSearch("");
}
}}
>
<Plus className="mr-2 h-4 w-4" />
Create "{search}"
</CommandItem>
</CommandGroup>
) : (
"No results found."
)}
</CommandEmpty> The most important parts are wrapping the |
||
<Plus className="mr-2 h-4 w-4" /> | ||
Create "{inputValue}" | ||
</CommandItem> | ||
) : ( | ||
"No results found." | ||
)} | ||
</CommandEmpty> | ||
<CommandGroup> | ||
<CommandItem | ||
key="all" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We probably don't need this
creatable
prop - we could make it creatable if anonCreate
function is provided. Otherwise, the two props depend on each other, and it's possible to submit invalid values by e.g. settingcreatable={true}
and not providingonCreate
, or providingonCreate
without making itcreatable