Replies: 8 comments
-
PS. I don't have |
Beta Was this translation helpful? Give feedback.
-
Did you ever figure this out? I'd like to get similar behavior working for my own project. |
Beta Was this translation helpful? Give feedback.
-
I'm also trying to figure it out, with no luck. My use case: I have a filter dropdown (multiple). 98% of the time, a user just selects one filter option. (An author for example) But it should be possible to select multiple options nonetheless. So the dropdown should close automatically, every time a single option is selected. In VueMultiselect, there is a :close-on-select="true" property for this. I already tried
I search for something like Is there any way to programmatically close the dropdown, maybe using ref? |
Beta Was this translation helpful? Give feedback.
-
@camya I have had a similar task, plus the rule to "allow at most 3 values to be selected". SingleFor non- const inputRef = useRef<HTMLInputElement>(null)
function handleChange(value: Place | null): void {
onChange(value)
setTimeout(() => {
if (inputRef.current) {
inputRef.current.blur()
}
}) // setTimeout is necessary to blur AFTER `Combobox` does its own focus handling (race condition basically)
}
...
<Combobox onChange={handleChange} ...>
...
<Combobox.Input ref={inputRef} .../>
...
</Combobox> MultipleFor export const SkillsCombobox = memo(forwardRef(function SkillCombobox(props: SkillComboboxProps, ref: any) {
...
const [disabled, setDisabled] = useState(false)
const previousValue = usePrevious(value) || value // from any hook library, not shown for brevity
const buttonRef = useRef<HTMLButtonElement>(null)
useEffect(() => {
const wasAdded = value.length > previousValue.length
const wasRemoved = value.length < previousValue.length
if (wasAdded) {
if (buttonRef.current) {
buttonRef.current.click() // manually closing the popup
}
if (value.length >= 3 && !disabled) {
setDisabled(true)
}
} else if (wasRemoved) {
if (value.length < 3 && disabled) {
setDisabled(false)
}
}
}, [value])
// ...
return (
<Combobox disabled={disabled} ...>
...
<Combobox.Button ref={buttonRef} .../>
...
== rendering errors here ==
...
== selected options with mini-badges ==
{value && Boolean(value.length) && (
<div className="mt-2 flex items-center gap-2">
{value.map((option, i) =>
<Badge key={i} white closeable onClose={() => onRemove(option)}>
{option}
</Badge>
)}
</div>
)}
</Combobox>
)
})) There might be a better approach but that's where I'm at for now. If you don't have length limits – it's 1/2 of the above hook. |
Beta Was this translation helpful? Give feedback.
-
@RobinMalfait Are there any plans to add an option to close the combobox without having to use the For us this is really a deal breaker to use the combobox, otherwise it's a great component and we would really like to use it. The current workarounds are quite hacky. |
Beta Was this translation helpful? Give feedback.
-
The only thing which worked for me to close & open the combobox manually: for closing: export function simulateMouseClick(element: HTMLElement) {
const events = ["mousedown", "click", "mouseup"];
events.forEach((mouseEventType) =>
element.dispatchEvent(
new MouseEvent(mouseEventType, {
view: window,
bubbles: true,
cancelable: true,
buttons: 1,
})
)
);
}
simulateMouseClick(document.body) for opening: const inputRef = useRef<HTMLInputElement>(null)
<Combobox.Input ref={inputRef} ... />
inputRef.current?.click() |
Beta Was this translation helpful? Give feedback.
-
@thewh1teagle blurring an input at const onChange = useCallback((value: Place | null) => {
if (!error) {
setTimeout(() => {
if (inputRef.current) {
inputRef.current.blur()
}
})
_onChange(value)
}
}, [_onChange, error]) |
Beta Was this translation helpful? Give feedback.
-
We need this feature. |
Beta Was this translation helpful? Give feedback.
-
Is there a way to have the menu close when 'multiple' is set for a combobox when the user picks an option? Use case is creating all new options from scratch (none provided by default), where the user clicks the new option, but the dropdown doesn't close leaving just a 'create option X' option.
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions