Skip to content

Commit

Permalink
improve ref handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ling1726 committed Dec 8, 2023
1 parent 3631af8 commit 3636fef
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import * as React from 'react';
import { useOptionWalker } from './useOptionWalker';
import type { ActiveDescendantOptions } from './types';
import type { ActiveDescendantImperativeRef, ActiveDescendantOptions } from './types';
import { ACTIVEDESCENDANT_ATTRIBUTE } from './constants';
import { useMergedRefs } from '@fluentui/react-utilities';

export function useActiveDescendant<TActiveParentElement extends HTMLElement, TListboxElement extends HTMLElement>(
options: ActiveDescendantOptions,
) {
const { imperativeRef, matchOption } = options;
const { imperativeRef: imperativeRefProp, matchOption } = options;
const activeParentRef = React.useRef<TActiveParentElement>(null);
const { listboxRef, optionWalker } = useOptionWalker<TListboxElement>({ matchOption });
const imperativeRef = React.useRef<ActiveDescendantImperativeRef>();
const getActiveDescendant = () => {
return listboxRef.current?.querySelector<HTMLElement>(`[${ACTIVEDESCENDANT_ATTRIBUTE}]`);
};
Expand Down Expand Up @@ -54,7 +56,7 @@ export function useActiveDescendant<TActiveParentElement extends HTMLElement, TL
}
};

React.useImperativeHandle(imperativeRef, () => ({
React.useImperativeHandle(useMergedRefs(imperativeRef, imperativeRefProp), () => ({
first: () => {
if (!listboxRef.current || !activeParentRef.current) {
return;
Expand Down Expand Up @@ -134,5 +136,5 @@ export function useActiveDescendant<TActiveParentElement extends HTMLElement, TL
},
}));

return { listboxRef, activeParentRef };
return { listboxRef, activeParentRef, imperativeRef };
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,18 @@ export const useCombobox_unstable = (props: ComboboxProps, ref: React.Ref<HTMLIn
};

const triggerRef = React.useRef<HTMLInputElement>(null);
let listbox: Slot<typeof Listbox> | undefined = slot.optional(props.listbox, {
let listbox = slot.optional(props.listbox, {
renderByDefault: true,
defaultProps: { children: props.children },
elementType: Listbox,
});
listbox = useListboxSlot(props, listbox, comboboxPopupRef, triggerRef);
listbox = open ? listbox : undefined;

const listboxRef = useMergedRefs(listbox?.ref, comboboxPopupRef);
if (listbox) {
listbox.ref = listboxRef;
}

listbox = useListboxSlot(props, listbox, triggerRef) as typeof listbox;

let triggerSlot: Slot<'input'> = slot.always(props.input, {
defaultProps: {
Expand All @@ -84,7 +89,7 @@ export const useCombobox_unstable = (props: ComboboxProps, ref: React.Ref<HTMLIn

const rootSlot = slot.always(props.root, {
defaultProps: {
'aria-owns': !inlinePopup ? listbox?.id : undefined,
'aria-owns': !inlinePopup && open ? listbox?.id : undefined,
...rootNativeProps,
},
elementType: 'div',
Expand All @@ -95,7 +100,7 @@ export const useCombobox_unstable = (props: ComboboxProps, ref: React.Ref<HTMLIn
components: { root: 'div', input: 'input', expandIcon: 'span', listbox: Listbox },
root: rootSlot,
input: triggerSlot,
listbox,
listbox: open ? listbox : undefined,
expandIcon: slot.optional(props.expandIcon, {
renderByDefault: true,
defaultProps: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,16 @@ export const useDropdown_unstable = (props: DropdownProps, ref: React.Ref<HTMLBu
const [comboboxPopupRef, comboboxTargetRef] = useComboboxPositioning(props);

const triggerRef = React.useRef<HTMLButtonElement>(null);
let listbox: Slot<typeof Listbox> | undefined = slot.optional(props.listbox, {
let listbox = slot.optional(props.listbox, {
renderByDefault: true,
defaultProps: { children: props.children },
elementType: Listbox,
});
listbox = useListboxSlot(props, listbox, comboboxPopupRef, triggerRef);
listbox = open ? listbox : undefined;
const listboxRef = useMergedRefs(listbox?.ref, comboboxPopupRef);
if (listbox) {
listbox.ref = listboxRef;
}
listbox = useListboxSlot(props, listbox, triggerRef) as typeof listbox;

let trigger: Slot<'button'> = slot.always(props.button, {
defaultProps: {
Expand All @@ -57,7 +60,7 @@ export const useDropdown_unstable = (props: DropdownProps, ref: React.Ref<HTMLBu

const rootSlot = slot.always(props.root, {
defaultProps: {
'aria-owns': !props.inlinePopup ? listbox?.id : undefined,
'aria-owns': !props.inlinePopup && open ? listbox?.id : undefined,
children: props.children,
...rootNativeProps,
},
Expand All @@ -69,7 +72,7 @@ export const useDropdown_unstable = (props: DropdownProps, ref: React.Ref<HTMLBu
components: { root: 'div', button: 'button', expandIcon: 'span', listbox: Listbox },
root: rootSlot,
button: trigger,
listbox,
listbox: open ? listbox : undefined,
expandIcon: slot.optional(props.expandIcon, {
renderByDefault: true,
defaultProps: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { mergeCallbacks, useId, useEventCallback, useMergedRefs } from '@fluentui/react-utilities';
import { mergeCallbacks, useId, useEventCallback } from '@fluentui/react-utilities';
import type { ExtractSlotProps, Slot } from '@fluentui/react-utilities';
import { Listbox } from '../components/Listbox/Listbox';
import type { ComboboxBaseProps } from './ComboboxBase.types';
Expand All @@ -10,9 +10,8 @@ import type { ComboboxBaseProps } from './ComboboxBase.types';
export function useListboxSlot(
props: ComboboxBaseProps,
listboxSlot: ExtractSlotProps<Slot<typeof Listbox>> | undefined,
ref: React.Ref<HTMLDivElement>,
triggerRef: React.RefObject<HTMLInputElement> | React.RefObject<HTMLButtonElement>,
): ExtractSlotProps<Slot<typeof Listbox>> | undefined {
): ExtractSlotProps<Slot<typeof Listbox>> {
const { multiselect } = props;

/**
Expand Down Expand Up @@ -41,7 +40,6 @@ export function useListboxSlot(
...listboxSlot,
onMouseDown,
onClick,
ref: useMergedRefs(listboxSlot?.ref, ref),
};

return listbox;
Expand Down

0 comments on commit 3636fef

Please sign in to comment.