|
2 | 2 | import { Times } from '@primeicons/react/times'; |
3 | 3 | import { useInputTags, useInputTagsCompleteEvent, type useInputTagsValueChangeEvent } from '@primereact/headless/inputtags'; |
4 | 4 | import { useListboxOption } from '@primereact/headless/listbox'; |
| 5 | +import { usePortal } from '@primereact/headless/portal'; |
5 | 6 | import { usePositioner } from '@primereact/headless/positioner'; |
6 | 7 | import type { useListboxInstance } from '@primereact/types/headless/listbox'; |
7 | 8 | import * as React from 'react'; |
8 | | - |
9 | | -const countries = [ |
10 | | - 'Argentina', |
11 | | - 'Australia', |
12 | | - 'Brazil', |
13 | | - 'Canada', |
14 | | - 'China', |
15 | | - 'Egypt', |
16 | | - 'France', |
17 | | - 'Germany', |
18 | | - 'India', |
19 | | - 'Italy', |
20 | | - 'Japan', |
21 | | - 'Mexico', |
22 | | - 'Netherlands', |
23 | | - 'Norway', |
24 | | - 'Poland', |
25 | | - 'South Korea', |
26 | | - 'Spain', |
27 | | - 'Sweden', |
28 | | - 'Switzerland', |
29 | | - 'Turkey', |
30 | | - 'United Kingdom', |
31 | | - 'United States' |
32 | | -]; |
| 9 | +import { createPortal } from 'react-dom'; |
33 | 10 |
|
34 | 11 | function OptionItem({ option, index, listbox }: { option: unknown; index: number; listbox: useListboxInstance }) { |
35 | 12 | const { optionProps } = useListboxOption({ option, index, context: listbox }); |
@@ -61,6 +38,8 @@ export default function BasicDemo() { |
61 | 38 | onComplete: (e: useInputTagsCompleteEvent) => setQuery(e.query) |
62 | 39 | }); |
63 | 40 |
|
| 41 | + const portal = usePortal(); |
| 42 | + |
64 | 43 | usePositioner({ |
65 | 44 | anchor: state.anchorElement, |
66 | 45 | content: state.positionerElement, |
@@ -96,22 +75,50 @@ export default function BasicDemo() { |
96 | 75 | placeholder={state.value.length === 0 ? 'Add countries...' : ''} |
97 | 76 | className="flex-1 min-w-20 px-1 py-0.5 text-sm bg-transparent outline-none border-none text-surface-700 dark:text-surface-0 placeholder:text-surface-400" |
98 | 77 | /> |
99 | | - {state.opened && ( |
100 | | - <div {...positionerProps}> |
101 | | - <div |
102 | | - {...popupProps} |
103 | | - className="border border-surface-200 dark:border-surface-700 rounded-lg bg-surface-0 dark:bg-surface-900 shadow-md overflow-hidden" |
104 | | - > |
105 | | - <ul {...listProps} className="list-none m-0 p-1 outline-none max-h-56 overflow-auto"> |
106 | | - {listbox?.getOptions().map((option: unknown, index: number) => ( |
107 | | - <OptionItem key={index} option={option} index={index} listbox={listbox} /> |
108 | | - ))} |
109 | | - </ul> |
110 | | - {filteredCountries.length === 0 && <div className="px-3 py-2 text-sm text-surface-400">No countries found</div>} |
111 | | - </div> |
112 | | - </div> |
113 | | - )} |
| 78 | + {portal.state.mounted && |
| 79 | + state.opened && |
| 80 | + createPortal( |
| 81 | + <div {...positionerProps}> |
| 82 | + <div |
| 83 | + {...popupProps} |
| 84 | + className="border border-surface-200 dark:border-surface-700 rounded-lg bg-surface-0 dark:bg-surface-900 shadow-md overflow-hidden" |
| 85 | + > |
| 86 | + <ul {...listProps} className="list-none m-0 p-1 outline-none max-h-56 overflow-auto"> |
| 87 | + {listbox?.getOptions().map((option: unknown, index: number) => ( |
| 88 | + <OptionItem key={index} option={option} index={index} listbox={listbox} /> |
| 89 | + ))} |
| 90 | + </ul> |
| 91 | + {filteredCountries.length === 0 && <div className="px-3 py-2 text-sm text-surface-400">No countries found</div>} |
| 92 | + </div> |
| 93 | + </div>, |
| 94 | + document.body |
| 95 | + )} |
114 | 96 | </div> |
115 | 97 | </div> |
116 | 98 | ); |
117 | 99 | } |
| 100 | + |
| 101 | +const countries = [ |
| 102 | + 'Argentina', |
| 103 | + 'Australia', |
| 104 | + 'Brazil', |
| 105 | + 'Canada', |
| 106 | + 'China', |
| 107 | + 'Egypt', |
| 108 | + 'France', |
| 109 | + 'Germany', |
| 110 | + 'India', |
| 111 | + 'Italy', |
| 112 | + 'Japan', |
| 113 | + 'Mexico', |
| 114 | + 'Netherlands', |
| 115 | + 'Norway', |
| 116 | + 'Poland', |
| 117 | + 'South Korea', |
| 118 | + 'Spain', |
| 119 | + 'Sweden', |
| 120 | + 'Switzerland', |
| 121 | + 'Turkey', |
| 122 | + 'United Kingdom', |
| 123 | + 'United States' |
| 124 | +]; |
0 commit comments