Skip to content

Commit

Permalink
Merge pull request #152 from inokawa/rename-element
Browse files Browse the repository at this point in the history
Rename element props to components
  • Loading branch information
inokawa authored Aug 1, 2023
2 parents e8f0bdf + a4a1c5d commit e247ef1
Show file tree
Hide file tree
Showing 15 changed files with 147 additions and 119 deletions.
21 changes: 1 addition & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,2 @@
export type { CacheSnapshot } from "./core/types";
export { VList } from "./react/VList";
export type { VListProps, VListHandle, ScrollMode } from "./react/VList";
export { VGrid } from "./react/VGrid";
export type {
VGridProps,
VGridHandle,
CustomCellComponent,
CustomCellComponentProps,
} from "./react/VGrid";
export { WVList } from "./react/WVList";
export type { WVListProps, WVListHandle } from "./react/WVList";
export type {
WindowComponentAttributes,
CustomWindowComponent,
CustomWindowComponentProps,
} from "./react/Window";
export type {
CustomItemComponent,
CustomItemComponentProps,
} from "./react/ListItem";
export * from "./react";
4 changes: 2 additions & 2 deletions src/react/VGrid.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ it("should pass attributes to element", async () => {
});

// it("should change components", async () => {
// const UlList = forwardRef<HTMLDivElement, CustomWindowComponentProps>(
// const UlList = forwardRef<HTMLDivElement, CustomViewportComponentProps>(
// ({ children, attrs, scrollSize }, ref) => {
// return (
// <div ref={ref} {...attrs}>
Expand All @@ -116,7 +116,7 @@ it("should pass attributes to element", async () => {
// }
// );
// const { asFragment } = render(
// <VList element={UlList} itemElement="li">
// <VList components={{ Root: UlList, Item: "li" }}>
// <div>0</div>
// <div>1</div>
// <div>2</div>
Expand Down
56 changes: 33 additions & 23 deletions src/react/VGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ import {
} from "./useSelector";
import { max, min, values } from "../core/utils";
import { createScroller } from "../core/scroller";
import { refKey } from "./utils";
import { emptyComponents, refKey } from "./utils";
import { useStatic } from "./useStatic";
import {
CustomWindowComponent,
CustomWindowComponentProps,
WindowComponentAttributes,
CustomViewportComponent,
CustomViewportComponentProps,
ViewportComponentAttributes,
} from "..";
import { createGridResizer, GridResizer } from "../core/resizer";
import { Window as DefaultWindow } from "./Window";
import { Viewport as DefaultViewport } from "./Viewport";

const genKey = (i: number, j: number) => `${i}-${j}`;

Expand Down Expand Up @@ -178,7 +178,7 @@ export interface VGridHandle {
/**
* Props of {@link VGrid}.
*/
export interface VGridProps extends WindowComponentAttributes {
export interface VGridProps extends ViewportComponentAttributes {
/**
* A function to create elements rendered by this component.
*/
Expand Down Expand Up @@ -228,15 +228,20 @@ export interface VGridProps extends WindowComponentAttributes {
*/
rtl?: boolean;
/**
* Customized element type for scrollable element. This element will get {@link CustomWindowComponentProps} as props.
* @defaultValue {@link Window}
* Customized components for advanced usage.
*/
element?: CustomWindowComponent;
/**
* Customized element type for cell element. This element will get {@link CustomCellComponentProps} as props.
* @defaultValue "div"
*/
cellElement?: CustomCellComponentOrElement;
components?: {
/**
* Component for scrollable element. This component will get {@link CustomViewportComponentProps} as props.
* @defaultValue {@link DefaultViewport}
*/
Root?: CustomViewportComponent;
/**
* Component or element type for cell element. This component will get {@link CustomCellComponentProps} as props.
* @defaultValue "div"
*/
Cell?: CustomCellComponentOrElement;
};
}

/**
Expand All @@ -254,9 +259,14 @@ export const VGrid = forwardRef<VGridHandle, VGridProps>(
initialRowCount,
initialColCount,
rtl: rtlProp,
element: Window = DefaultWindow,
cellElement: itemElement = "div",
...windowAttrs
components: {
Root: Viewport = DefaultViewport,
Cell: ItemElement = "div",
} = emptyComponents as {
Root?: CustomViewportComponent;
Cell?: CustomCellComponentOrElement;
},
...viewportAttrs
},
ref
): ReactElement => {
Expand Down Expand Up @@ -421,7 +431,7 @@ export const VGrid = forwardRef<VGridHandle, VGridProps>(
_hStore={hStore}
_rowIndex={i}
_colIndex={j}
_element={itemElement as "div"}
_element={ItemElement as "div"}
_children={render(i, j)}
_isRtl={isRtl}
/>
Expand All @@ -439,14 +449,14 @@ export const VGrid = forwardRef<VGridHandle, VGridProps>(
]);

return (
<Window
<Viewport
ref={rootRef}
width={width}
height={height}
scrolling={verticalScrolling || horizontalScrolling}
attrs={useMemo(
() => ({
...windowAttrs,
...viewportAttrs,
style: {
overflow: "auto",
contain: "strict",
Expand All @@ -457,14 +467,14 @@ export const VGrid = forwardRef<VGridHandle, VGridProps>(
height: "100%",
padding: 0,
margin: 0,
...windowAttrs.style,
...viewportAttrs.style,
},
}),
values(windowAttrs)
values(viewportAttrs)
)}
>
{items}
</Window>
</Viewport>
);
}
);
6 changes: 3 additions & 3 deletions src/react/VList.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
useRef,
useState,
} from "react";
import { CustomWindowComponentProps } from "..";
import { CustomViewportComponentProps } from "..";

const ITEM_HEIGHT = 50;
const ITEM_WIDTH = 100;
Expand Down Expand Up @@ -107,7 +107,7 @@ it("should pass attributes to element", async () => {
});

it("should change components", async () => {
const UlList = forwardRef<HTMLDivElement, CustomWindowComponentProps>(
const UlList = forwardRef<HTMLDivElement, CustomViewportComponentProps>(
({ children, attrs, height }, ref) => {
return (
<div ref={ref} {...attrs}>
Expand All @@ -119,7 +119,7 @@ it("should change components", async () => {
}
);
const { asFragment } = render(
<VList element={UlList} itemElement="li">
<VList components={{ Root: UlList, Item: "li" }}>
<div>0</div>
<div>1</div>
<div>2</div>
Expand Down
58 changes: 34 additions & 24 deletions src/react/VList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ import {
} from "./useSelector";
import { exists, max, min, values } from "../core/utils";
import { createScroller } from "../core/scroller";
import { MayHaveKey, flattenChildren, refKey } from "./utils";
import { MayHaveKey, emptyComponents, flattenChildren, refKey } from "./utils";
import { useStatic } from "./useStatic";
import { useRefWithUpdate } from "./useRefWithUpdate";
import { createResizer } from "../core/resizer";
import { WindowComponentAttributes } from "..";
import { ViewportComponentAttributes } from "..";
import {
CustomWindowComponent,
CustomWindowComponentProps,
Window as DefaultWindow,
} from "./Window";
CustomViewportComponent,
CustomViewportComponentProps,
Viewport as DefaultViewport,
} from "./Viewport";
import { CustomItemComponent, ListItem } from "./ListItem";
import { CacheSnapshot } from "../core/types";

Expand Down Expand Up @@ -77,7 +77,7 @@ export interface VListHandle {
/**
* Props of {@link VList}.
*/
export interface VListProps extends WindowComponentAttributes {
export interface VListProps extends ViewportComponentAttributes {
/**
* Elements rendered by this component.
*/
Expand Down Expand Up @@ -114,15 +114,20 @@ export interface VListProps extends WindowComponentAttributes {
*/
cache?: CacheSnapshot;
/**
* Customized element type for scrollable element. This element will get {@link CustomWindowComponentProps} as props.
* @defaultValue {@link Window}
* Customized components for advanced usage.
*/
element?: CustomWindowComponent;
/**
* Customized element type for item element. This element will get {@link CustomItemComponentProps} as props.
* @defaultValue "div"
*/
itemElement?: CustomItemComponentOrElement;
components?: {
/**
* Component for scrollable element. This component will get {@link CustomViewportComponentProps} as props.
* @defaultValue {@link DefaultViewport}
*/
Root?: CustomViewportComponent;
/**
* Component or element type for item element. This component will get {@link CustomItemComponentProps} as props.
* @defaultValue "div"
*/
Item?: CustomItemComponentOrElement;
};
/**
* Callback invoked whenever scroll offset changes.
* @param offset Current scrollTop or scrollLeft.
Expand Down Expand Up @@ -160,12 +165,17 @@ export const VList = forwardRef<VListHandle, VListProps>(
horizontal: horizontalProp,
mode,
cache,
element: Window = DefaultWindow,
itemElement = "div",
components: {
Root: Viewport = DefaultViewport,
Item: ItemElement = "div",
} = emptyComponents as {
Root?: CustomViewportComponent;
Item?: CustomItemComponentOrElement;
},
onScroll: onScrollProp,
onScrollStop: onScrollStopProp,
onRangeChange: onRangeChangeProp,
...windowAttrs
...viewportAttrs
},
ref
): ReactElement => {
Expand Down Expand Up @@ -293,7 +303,7 @@ export const VList = forwardRef<VListHandle, VListProps>(
_resizer={resizer}
_store={store}
_index={i}
_element={itemElement as "div"}
_element={ItemElement as "div"}
_children={e}
_isHorizontal={isHorizontal}
_isRtl={isRtl}
Expand All @@ -305,14 +315,14 @@ export const VList = forwardRef<VListHandle, VListProps>(
}, [elements, overscanedStartIndex, overscanedEndIndex]);

return (
<Window
<Viewport
ref={rootRef}
width={isHorizontal ? scrollSize : undefined}
height={isHorizontal ? undefined : scrollSize}
scrolling={scrolling}
attrs={useMemo(
() => ({
...windowAttrs,
...viewportAttrs,
style: {
overflow: isHorizontal ? "auto hidden" : "hidden auto",
display: isHorizontal ? "inline-block" : "block",
Expand All @@ -326,14 +336,14 @@ export const VList = forwardRef<VListHandle, VListProps>(
height: "100%",
padding: 0,
margin: 0,
...windowAttrs.style,
...viewportAttrs.style,
},
}),
values(windowAttrs)
values(viewportAttrs)
)}
>
{items}
</Window>
</Viewport>
);
}
);
10 changes: 5 additions & 5 deletions src/react/Window.tsx → src/react/Viewport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
useMemo,
} from "react";

export type WindowComponentAttributes = Pick<
export type ViewportComponentAttributes = Pick<
React.HTMLAttributes<HTMLElement>,
"className" | "style" | "id" | "role" | "tabIndex"
> &
Expand All @@ -15,15 +15,15 @@ export type WindowComponentAttributes = Pick<
/**
* Props of customized scrollable component.
*/
export interface CustomWindowComponentProps {
export interface CustomViewportComponentProps {
/**
* Renderable item elements.
*/
children: ReactNode;
/**
* Attributes that should be passed to the scrollable element.
*/
attrs: WindowComponentAttributes;
attrs: ViewportComponentAttributes;
/**
* Total height of items. It's undefined if component is not vertically scrollable.
*/
Expand All @@ -38,7 +38,7 @@ export interface CustomWindowComponentProps {
scrolling: boolean;
}

export const Window = forwardRef<any, CustomWindowComponentProps>(
export const Viewport = forwardRef<any, CustomViewportComponentProps>(
({ children, attrs, width, height, scrolling }, ref): ReactElement => {
return (
<div ref={ref} {...attrs}>
Expand All @@ -60,4 +60,4 @@ export const Window = forwardRef<any, CustomWindowComponentProps>(
}
);

export type CustomWindowComponent = typeof Window;
export type CustomViewportComponent = typeof Viewport;
6 changes: 3 additions & 3 deletions src/react/WVList.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { afterEach, it, expect, describe, jest } from "@jest/globals";
import { render, cleanup } from "@testing-library/react";
import { WVList } from "./WVList";
import { Profiler, ReactElement, forwardRef, useEffect, useState } from "react";
import { CustomWindowComponentProps } from "..";
import { CustomViewportComponentProps } from "..";

const ITEM_HEIGHT = 50;
const ITEM_WIDTH = 100;
Expand Down Expand Up @@ -77,7 +77,7 @@ it("should pass attributes to element", async () => {
});

it("should change components", async () => {
const UlList = forwardRef<HTMLDivElement, CustomWindowComponentProps>(
const UlList = forwardRef<HTMLDivElement, CustomViewportComponentProps>(
({ children, attrs, height }, ref) => {
return (
<div ref={ref} {...attrs}>
Expand All @@ -89,7 +89,7 @@ it("should change components", async () => {
}
);
const { asFragment } = render(
<WVList element={UlList} itemElement="li">
<WVList components={{ Root: UlList, Item: "li" }}>
<div>0</div>
<div>1</div>
<div>2</div>
Expand Down
Loading

0 comments on commit e247ef1

Please sign in to comment.