Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/strong-moles-care.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"bako-ui": patch
---

Add optional placeholder and prevent options menu from opening when it is empty
73 changes: 41 additions & 32 deletions packages/ui/src/components/RhfCombobox/rhf-combobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export function RhfCombobox<
name,
defaultValue,
label,
placeholder,
error,
options,
disabled = false,
Expand Down Expand Up @@ -153,6 +154,8 @@ export function RhfCombobox<
}
: {};

const showOptions = isLoadingOptions || collection.items.length > 0;

return (
<Field.Root invalid={!!error}>
{label && (
Expand All @@ -179,12 +182,13 @@ export function RhfCombobox<
onOpenChange={handleOpenChange}
onInputValueChange={handleInputValueChange}
disabled={disabled}
openOnClick={openOnFocus}
openOnClick={openOnFocus && showOptions}
multiple={false}
invalid={!!error}
allowCustomValue={allowCustomValue}
selectionBehavior="preserve"
defaultValue={[defaultValue || '']}
placeholder={placeholder}
{...slotProps?.root}
>
<Combobox.Control>
Expand All @@ -206,42 +210,47 @@ export function RhfCombobox<
</Combobox.IndicatorGroup>
</Combobox.Control>

<ComboboxHiddenInput name={rest.name} value={value || ''} />
<ComboboxHiddenInput name={rest.name} value={value ?? ''} />

{helperText && <Field.HelperText>{helperText}</Field.HelperText>}
{error && <Field.ErrorText>{error.message}</Field.ErrorText>}
<Portal>
<Combobox.Positioner>
<Combobox.Content>
{!allowCustomValue && (
<Combobox.Empty>{noOptionsText}</Combobox.Empty>
)}

{isLoadingOptions && (
<HStack p="2">
<Spinner size="xs" borderWidth="1px" />
<Span>Loading...</Span>
</HStack>
)}

{!isLoadingOptions &&
collection.items.map((item) => (
<Combobox.Item item={item} key={item.value}>
{!item.imageUrl && item.label}
{item.imageUrl && (
<Flex gap={2} align="center">
<Image
src={item.imageUrl}
boxSize="5"
alt={`${item.label} image`}
/>
<Span>{item.label}</Span>
</Flex>
)}
<Combobox.ItemIndicator />
</Combobox.Item>
))}
</Combobox.Content>
{(showOptions || !allowCustomValue) && (
<Combobox.Content>
{isLoadingOptions && (
<HStack p="2">
<Spinner size="xs" borderWidth="1px" />
<Span>Loading...</Span>
</HStack>
)}

{!isLoadingOptions &&
collection.items.length > 0 &&
collection.items.map((item) => (
<Combobox.Item item={item} key={item.value}>
{!item.imageUrl && item.label}
{item.imageUrl && (
<Flex gap={2} align="center">
<Image
src={item.imageUrl}
boxSize="5"
alt={`${item.label} image`}
/>
<Span>{item.label}</Span>
</Flex>
)}
<Combobox.ItemIndicator />
</Combobox.Item>
))}

{!allowCustomValue &&
!isLoadingOptions &&
collection.items.length === 0 && (
<Combobox.Empty>{noOptionsText}</Combobox.Empty>
)}
</Combobox.Content>
)}
</Combobox.Positioner>
</Portal>
</Combobox.Root>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export type RhfComboboxProps<
> = UseControllerProps<TFieldValues, TName> & {
options: RhfComboboxOptions[];
label?: string;
placeholder?: string;
helperText?: string;
disabled?: boolean;
error?: FieldError;
Expand Down
Loading