Skip to content

Commit

Permalink
descendants: add index to descendant data
Browse files Browse the repository at this point in the history
  • Loading branch information
chaance committed Jan 30, 2020
1 parent a3e436d commit 7068a07
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/combobox/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ export const ComboboxOption: ComponentWithForwardedRef<

let index = useDescendant({
context: ComboboxDescendantContext,
element: ownRef.current,
element: ownRef.current!,
value
});

Expand Down
2 changes: 1 addition & 1 deletion packages/menu-button/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ const MenuItemImpl = forwardRefWithAs<MenuItemImplProps, "div">(
const index = useDescendant(
{
context: MenuDescendantContext,
element: ownRef.current,
element: ownRef.current!,
key: valueText
},
indexProp
Expand Down
4 changes: 2 additions & 2 deletions packages/tabs/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ export const Tab = forwardRefWithAs<
const ownRef = useRef<HTMLElement | null>(null);
const ref = useForkedRef(forwardedRef, ownRef);
const index = useDescendant({
element: ownRef.current,
element: ownRef.current!,
context: TabsDescendantsContext
});

Expand Down Expand Up @@ -572,7 +572,7 @@ export const TabPanel = forwardRefWithAs<TabPanelProps, "div">(
let ownRef = useRef<HTMLElement | null>(null);

let index = useDescendant({
element: ownRef.current,
element: ownRef.current!,
context: TabPanelDescendantsContext
});
let isSelected = index === selectedIndex;
Expand Down
7 changes: 5 additions & 2 deletions packages/utils/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ export {

export type Descendant<T, P = {}> = P & {
element: (T extends HTMLElement ? T : HTMLElement) | null;
index: number;
};

export interface IDescendantContext<T, P> {
Expand Down Expand Up @@ -375,7 +376,9 @@ export function useDescendant<T, P>(
context,
element,
...rest
}: Descendant<T, P> & { context: React.Context<IDescendantContext<T, P>> },
}: Omit<Descendant<T, P>, "index"> & {
context: React.Context<IDescendantContext<T, P>>;
},
indexProp?: number
) {
let [, forceUpdate] = useState();
Expand Down Expand Up @@ -450,7 +453,7 @@ export function DescendantProvider<T, P>({
);
});

let newItem = { element, ...rest } as Descendant<T, P>;
let newItem = { element, index, ...rest } as Descendant<T, P>;

// If an index is not found we will push the element to the end.
if (index === -1) {
Expand Down

0 comments on commit 7068a07

Please sign in to comment.