Skip to content

Commit

Permalink
Merge pull request #550 from aeharding/react-19-ref
Browse files Browse the repository at this point in the history
Support React 19 ref as prop
  • Loading branch information
inokawa authored Dec 6, 2024
2 parents e583c7d + d903f39 commit d38b455
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
6 changes: 2 additions & 4 deletions src/react/VList.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,9 @@ it("should pass attributes to element", () => {

it("should pass index to items", () => {
const Item = forwardRef<HTMLDivElement, CustomItemComponentProps>(
({ children, index, style }, ref) => {
({ index, ...rest }, ref) => {
return (
<div ref={ref} style={style} data-index={index}>
{children}
</div>
<div ref={ref} data-index={index} {...rest} />
);
}
);
Expand Down
6 changes: 2 additions & 4 deletions src/react/WindowVirtualizer.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ afterEach(cleanup);

it("should pass index to items", () => {
const Item = forwardRef<HTMLDivElement, CustomItemComponentProps>(
({ children, index, style }, ref) => {
({ index, ...rest }, ref) => {
return (
<div ref={ref} style={style} data-index={index}>
{children}
</div>
<div ref={ref} data-index={index} {...rest} />
);
}
);
Expand Down
20 changes: 12 additions & 8 deletions src/react/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CSSProperties, ReactNode } from "react";
import { ComponentType, CSSProperties, LegacyRef, ReactNode } from "react";

export type ViewportComponentAttributes = Pick<
React.HTMLAttributes<HTMLElement>,
Expand All @@ -9,12 +9,14 @@ export type ViewportComponentAttributes = Pick<
export interface CustomContainerComponentProps {
style: CSSProperties;
children: ReactNode;
/**
* only available after React 19
*/
ref?: LegacyRef<any>;
}

export type CustomContainerComponent = React.ForwardRefExoticComponent<
React.PropsWithoutRef<CustomContainerComponentProps> &
React.RefAttributes<any>
>;
export type CustomContainerComponent =
ComponentType<CustomContainerComponentProps>;

/**
* Props of customized item component for {@link Virtualizer} or {@link WindowVirtualizer}.
Expand All @@ -23,8 +25,10 @@ export interface CustomItemComponentProps {
style: CSSProperties;
index: number;
children: ReactNode;
/**
* only available after React 19
*/
ref?: LegacyRef<any>;
}

export type CustomItemComponent = React.ForwardRefExoticComponent<
React.PropsWithoutRef<CustomItemComponentProps> & React.RefAttributes<any>
>;
export type CustomItemComponent = ComponentType<CustomItemComponentProps>;

0 comments on commit d38b455

Please sign in to comment.