Skip to content

Commit 9c53d59

Browse files
committed
fix chakra SelectWidget content position via portal
1 parent 4f8b330 commit 9c53d59

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

packages/chakra-ui/src/SelectWidget/SelectWidget.tsx

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FocusEvent, useMemo } from 'react';
1+
import { FocusEvent, useMemo, useRef } from 'react';
22

33
import {
44
ariaDescribedByIds,
@@ -14,7 +14,7 @@ import {
1414
import { Field } from '../components/ui/field';
1515
import { SelectRoot, SelectTrigger, SelectValueText } from '../components/ui/select';
1616
import { OptionsOrGroups } from 'chakra-react-select';
17-
import { createListCollection, SelectValueChangeDetails } from '@chakra-ui/react';
17+
import { createListCollection, Portal, SelectValueChangeDetails } from '@chakra-ui/react';
1818
import { Select as ChakraSelect } from '@chakra-ui/react';
1919

2020
export default function SelectWidget<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
@@ -114,15 +114,19 @@ export default function SelectWidget<T = any, S extends StrictRJSFSchema = RJSFS
114114
items: displayEnumOptions.filter((item) => item.value),
115115
});
116116

117+
const containerRef = useRef(null);
118+
117119
return (
118120
<Field
121+
ref={containerRef}
119122
mb={1}
120123
// {...chakraProps}
121124
disabled={disabled || readonly}
122125
required={required}
123126
readOnly={readonly}
124127
invalid={rawErrors && rawErrors.length > 0}
125128
label={labelValue(label, hideLabel || !label)}
129+
position='relative'
126130
>
127131
<SelectRoot
128132
collection={selectOptions}
@@ -136,20 +140,25 @@ export default function SelectWidget<T = any, S extends StrictRJSFSchema = RJSFS
136140
autoFocus={autofocus}
137141
value={formValue}
138142
aria-describedby={ariaDescribedByIds<T>(id)}
143+
positioning={{ placement: 'bottom' }}
139144
>
140-
<SelectTrigger>
141-
<SelectValueText placeholder={placeholder} />
142-
</SelectTrigger>
143-
<ChakraSelect.Positioner minWidth='100% !important' zIndex='2 !important' top='calc(100% + 5px) !important'>
144-
<ChakraSelect.Content>
145-
{selectOptions.items.map((item) => (
146-
<ChakraSelect.Item item={item} key={item.value}>
147-
{item.label}
148-
<ChakraSelect.ItemIndicator />
149-
</ChakraSelect.Item>
150-
))}
151-
</ChakraSelect.Content>
152-
</ChakraSelect.Positioner>
145+
<ChakraSelect.Control>
146+
<SelectTrigger>
147+
<SelectValueText placeholder={placeholder} />
148+
</SelectTrigger>
149+
</ChakraSelect.Control>
150+
<Portal container={containerRef}>
151+
<ChakraSelect.Positioner minWidth='100% !important' zIndex='2 !important' top='calc(100% + 5px) !important'>
152+
<ChakraSelect.Content>
153+
{selectOptions.items.map((item) => (
154+
<ChakraSelect.Item item={item} key={item.value}>
155+
{item.label}
156+
<ChakraSelect.ItemIndicator />
157+
</ChakraSelect.Item>
158+
))}
159+
</ChakraSelect.Content>
160+
</ChakraSelect.Positioner>
161+
</Portal>
153162
</SelectRoot>
154163
</Field>
155164
);

0 commit comments

Comments
 (0)