Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(docs): convert VirtualizedGrid stories FILES to TS #2530

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export interface VirtualizedGridProps extends VibeComponentProps {
item: ItemType,
index: number,
style: CSSProperties
) => ItemType | ComponentType<GridChildComponentProps<ItemType>>;
) => ItemType | ComponentType<GridChildComponentProps<ItemType>> | JSX.Element;
/**
* in order to calculate the number of rows to render in the grid, the component needs the height of the row
* return `number`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +0,0 @@
import React from "react";
import { Text } from "../../Text";

export const generateItems = (height = 30, width = "100%", itemsCount) => {
const items = [];
for (let i = 0; i < itemsCount; i++) {
items.push({ value: `Item ${i}`, height, width, id: i });
}
return items;
};

export const itemRenderer = (item, index, style) => {
if (item) {
const backgroundColor = index % 2 === 0 ? "#e1e1e1" : "#f8f8f0";
return (
<div key={index} style={style}>
<Text
color={Text.colors.FIXED_DARK}
style={{
backgroundColor,
height: item.height,
width: item.width,
display: "flex",
alignItems: "center",
justifyContent: "center"
}}
>
{item.value}
</Text>
</div>
);
}
return <div key={index} style={style} />;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React, { ComponentType } from "react";
import { GridChildComponentProps } from "react-window";
import { VirtualizedListItem } from "/workspaces/vibe/packages/core/src/components/VirtualizedList/VirtualizedList";
import { Text } from "../../Text";

export const generateItems = (height = 30, width = 100, itemsCount: number): VirtualizedListItem[] => {
const items: VirtualizedListItem[] = [];
for (let i = 0; i < itemsCount; i++) {
items.push({ value: `Item ${i}`, height, width, id: `${i}` });
}
return items;
};

export const itemRenderer = (
item: VirtualizedListItem,
index: number,
style: React.CSSProperties
): JSX.Element | ComponentType<GridChildComponentProps<VirtualizedListItem>> => {
if (item) {
const backgroundColor = index % 2 === 0 ? "#e1e1e1" : "#f8f8f0";
return (
<div key={index} style={style}>
<Text
color={Text.colors.FIXED_DARK}
style={{
backgroundColor,
height: item.height,
width: item.width,
display: "flex",
alignItems: "center",
justifyContent: "center"
}}
>
{item.value}
</Text>
</div>
);
}
return <div key={index} style={style} />;
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,43 @@ export default {
component: VirtualizedGrid
};

const virtualizedGridTemplate = args => {
const [scrollToId, setScrollToId] = useState(null);
const [lastScrolledId, setLastScrolledId] = useState(null);
const [scrollToDisabled, setScrollToDisabled] = useState(false);
const [nextScrollToId, setNextScrollToId] = useState(args.itemsCount - 1);
interface VirtualizedGridTemplateArgs {
wrapperStyle: React.CSSProperties;
wrapperId: string;
itemsCount: number;
}

const virtualizedGridTemplate = (args: VirtualizedGridTemplateArgs) => {
const [scrollToId, setScrollToId] = useState<number | null>(null);
const [lastScrolledId, setLastScrolledId] = useState<string | null>(null);
const [scrollToDisabled, setScrollToDisabled] = useState<boolean>(false);
const [nextScrollToId, setNextScrollToId] = useState<number>(args.itemsCount - 1);

const getColumnWidth = useCallback(() => {
return 100;
}, []);

const getRowHeight = useCallback(() => {
return 50;
}, []);

const items = useMemo(() => {
return generateItems(50, 100, args.itemsCount);
}, [args.itemsCount]);

const onClickToScroll = useCallback(() => {
setScrollToId(nextScrollToId);
setLastScrolledId("");
setLastScrolledId(null);
setScrollToDisabled(true);
}, [setScrollToId, setScrollToDisabled, nextScrollToId]);

const onScrollToFinished = useCallback(() => {
setLastScrolledId(nextScrollToId);
setLastScrolledId(nextScrollToId.toString());
setScrollToId(null);
setNextScrollToId(Math.round(Math.random() * items.length));
setScrollToDisabled(false);
}, [nextScrollToId, items, setNextScrollToId, setLastScrolledId]);

return (
<div style={args.wrapperStyle}>
<div style={{ width: "430px", height: "100%" }}>
Expand Down