Skip to content

Commit 53141ab

Browse files
committed
refactor: use isValidElement, apply more suggestions
1 parent 7a3bce2 commit 53141ab

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/Select.tsx

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import {
2-
Children,
32
type ComponentType,
43
type ForwardedRef,
54
forwardRef,
5+
isValidElement,
66
type ReactElement,
77
type ReactNode,
88
useEffect,
@@ -26,20 +26,20 @@ export type SelectProps = Partial<Omit<_SelectProps, 'children' | 'renderer'>> &
2626
}>;
2727

2828
function Select(props: SelectProps, ref: ForwardedRef<SelectElement>): ReactElement | null {
29-
const children = Children.toArray(props.children as ReactNode[]);
29+
// React.Children.toArray() doesn't allow functions, so we convert manually.
30+
const children = Array.isArray(props.children) ? props.children : [props.children];
3031

3132
// Components with slot attribute should stay in light DOM.
3233
const slotted = children.filter((child) => {
33-
return typeof child === 'object' && 'props' in child && child.props.slot;
34+
return isValidElement(child) && child.props.slot;
3435
});
3536

3637
// Component without slot attribute should go to the overlay.
37-
const overlayChildren = children.filter((child) => !slotted.includes(child));
38+
const overlayChildren = children.filter((child): child is ReactNode => {
39+
return isValidElement(child) && !slotted.includes(child);
40+
});
3841

39-
// React.Children.toArray() doesn't allow functions, so we convert manually.
40-
const renderFn = (Array.isArray(props.children) ? props.children : [props.children]).find(
41-
(child) => typeof child === 'function',
42-
);
42+
const renderFn = children.find((child) => typeof child === 'function');
4343

4444
const innerRef = useRef<SelectElement>(null);
4545
const [portals, renderer] = useSimpleOrChildrenRenderer(

0 commit comments

Comments
 (0)