Skip to content

Commit

Permalink
fix(select): correct element ref passing via props
Browse files Browse the repository at this point in the history
  • Loading branch information
hextion committed Aug 1, 2024
1 parent 739fa5b commit 854bbf1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
11 changes: 6 additions & 5 deletions packages/select/src/components/base-select/Component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import React, {
useEffect,
useMemo,
useRef,
useState,
} from 'react';
import mergeRefs from 'react-merge-refs';
import { ResizeObserver as ResizeObserverPolyfill } from '@juggle/resize-observer';
Expand Down Expand Up @@ -110,7 +111,7 @@ export const BaseSelect = forwardRef<unknown, ComponentProps>(
} = props;
const shouldSearchBlurRef = useRef(true);
const rootRef = useRef<HTMLDivElement>(null);
const fieldRef = useRef<HTMLInputElement>(null);
const [fieldRef, setFieldRef] = useState<HTMLInputElement | null>(null);
const listRef = useRef<HTMLDivElement>(null);
const initiatorRef = useRef<OptionShape | null>(null);
const searchRef = useRef<HTMLInputElement>(null);
Expand Down Expand Up @@ -332,7 +333,7 @@ export const BaseSelect = forwardRef<unknown, ComponentProps>(
}
}, [openProp, openMenu, closeMenu]);

const inputProps = getInputProps(getDropdownProps({ ref: mergeRefs([ref, fieldRef]) }));
const inputProps = getInputProps(getDropdownProps({ ref: mergeRefs([ref, setFieldRef]) }));
const { ref: menuRef, ...menuProps } = getMenuProps(
{ ref: listRef },
{ suppressRefError: true },
Expand All @@ -358,7 +359,7 @@ export const BaseSelect = forwardRef<unknown, ComponentProps>(

const handleFieldBlur = (event: FocusEvent<HTMLDivElement | HTMLInputElement>) => {
if (view === 'desktop') {
const isNextFocusInsideField = fieldRef.current?.contains(
const isNextFocusInsideField = fieldRef?.contains(
(event.relatedTarget || document.activeElement) as HTMLElement,
);

Expand Down Expand Up @@ -441,7 +442,7 @@ export const BaseSelect = forwardRef<unknown, ComponentProps>(
shouldSearchBlurRef.current = false;
searchRef.current?.blur();
shouldSearchBlurRef.current = true;
fieldRef.current?.focus();
fieldRef?.focus();
},
}),
multiple,
Expand Down Expand Up @@ -693,7 +694,7 @@ export const BaseSelect = forwardRef<unknown, ComponentProps>(
<ListPopoverDesktop
{...getListPopoverDesktopProps(props)}
open={open}
fieldRef={fieldRef}
popoverAnchorElement={fieldRef}
renderOptionsList={renderOptionsList}
/>
)}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FC, ReactNode, RefObject } from 'react';
import React, { FC, ReactNode } from 'react';
import cn from 'classnames';

import { ListPopoverDesktopRestProps } from './types/types';
Expand All @@ -7,7 +7,7 @@ import styles from '../../index.module.css';

type ListPopoverDesktopProps = {
open: boolean;
fieldRef: RefObject<HTMLInputElement>;
popoverAnchorElement: HTMLElement | null;
renderOptionsList: () => ReactNode;
} & ListPopoverDesktopRestProps;

Expand All @@ -16,7 +16,7 @@ export const ListPopoverDesktop: FC<ListPopoverDesktopProps> = (props) => {
Popover,
open,
popoverProps,
fieldRef,
popoverAnchorElement,
popoverPosition = 'bottom-start',
preventFlip = true,
popperClassName,
Expand All @@ -31,7 +31,7 @@ export const ListPopoverDesktop: FC<ListPopoverDesktopProps> = (props) => {
{...popoverProps}
open={open}
withTransition={false}
anchorElement={fieldRef.current as HTMLElement}
anchorElement={popoverAnchorElement}
position={popoverPosition}
preventFlip={preventFlip}
popperClassName={cn(styles.popoverInner, popperClassName)}
Expand Down

0 comments on commit 854bbf1

Please sign in to comment.