|
| 1 | +import { autoUpdate, useFloating } from "@floating-ui/react-dom"; |
1 | 2 | import { Combobox, ComboboxButton, ComboboxInput, ComboboxOption, ComboboxOptions } from "@headlessui/react"; |
2 | 3 | import classNames from "classnames"; |
3 | 4 | import React, { ReactNode, useMemo, useState } from "react"; |
| 5 | +import { createPortal } from "react-dom"; |
4 | 6 | import { ControllerRenderProps, FieldValues } from "react-hook-form"; |
5 | 7 | import { useIntl } from "react-intl"; |
6 | 8 |
|
@@ -301,20 +303,40 @@ export const MultiComboBox = ({ |
301 | 303 | error, |
302 | 304 | fieldInputProps, |
303 | 305 | disabled, |
304 | | -}: MultiComboBoxProps) => ( |
305 | | - <Combobox value={value} onChange={onChange} multiple immediate> |
306 | | - <ComboboxInput as={React.Fragment}> |
307 | | - <TagInput |
308 | | - name={name} |
309 | | - fieldValue={value ?? []} |
310 | | - onChange={onChange} |
311 | | - onBlur={fieldInputProps?.onBlur} |
312 | | - error={error} |
313 | | - disabled={disabled} |
314 | | - /> |
315 | | - </ComboboxInput> |
316 | | - <ComboboxOptions as="ul" className={styles.optionsMenu} modal={false}> |
317 | | - <Options optionSections={normalizeOptionsAsSections(options)} /> |
318 | | - </ComboboxOptions> |
319 | | - </Combobox> |
320 | | -); |
| 306 | +}: MultiComboBoxProps) => { |
| 307 | + const { x, y, reference, floating, strategy } = useFloating({ |
| 308 | + whileElementsMounted: autoUpdate, |
| 309 | + placement: "bottom-start", |
| 310 | + }); |
| 311 | + |
| 312 | + return ( |
| 313 | + <Combobox ref={reference} as="div" value={value} onChange={onChange} multiple immediate> |
| 314 | + <ComboboxInput as={React.Fragment}> |
| 315 | + <TagInput |
| 316 | + name={name} |
| 317 | + fieldValue={value ?? []} |
| 318 | + onChange={onChange} |
| 319 | + onBlur={fieldInputProps?.onBlur} |
| 320 | + error={error} |
| 321 | + disabled={disabled} |
| 322 | + /> |
| 323 | + </ComboboxInput> |
| 324 | + {createPortal( |
| 325 | + <ComboboxOptions |
| 326 | + ref={floating} |
| 327 | + as="ul" |
| 328 | + className={styles.optionsMenu} |
| 329 | + modal={false} |
| 330 | + style={{ |
| 331 | + position: strategy, |
| 332 | + top: y ?? 0, |
| 333 | + left: x ?? 0, |
| 334 | + }} |
| 335 | + > |
| 336 | + <Options optionSections={normalizeOptionsAsSections(options)} /> |
| 337 | + </ComboboxOptions>, |
| 338 | + document.body |
| 339 | + )} |
| 340 | + </Combobox> |
| 341 | + ); |
| 342 | +}; |
0 commit comments