import * as faker from 'faker';
import React, { useState, useEffect } from 'react';
import { Column } from '../src/';
import Group from '../src/components/Group';
import Row from '../src/components/Row';
import { RowProps, ColumnProps, GetRowKey } from '../src/types';
import {
getRowData,
DataScrollerContextProvider,
} from '../src/components/DataScrollerContext/DataScrollerContext';
import { storiesOf } from '@storybook/react';
import DataScroller, {
CellRendererArgs,
HeaderRendererArgs,
RowGetterArgs,
} from '../src';
const IndexCell = ({ rowData }: CellRendererArgs) => {
return (
{rowData.index}
);
};
const CustomInput = function(props: { value: string }) {
const [inputValue, setInputValue] = useState(props.value);
const handleOnChange = (e: React.ChangeEvent) => {
setInputValue(e.target.value);
};
return ;
};
const FirstNameCell = ({ rowData }: CellRendererArgs) => {
return ;
};
const LastNameCell = ({ rowData }: CellRendererArgs) => {
return {rowData.lastName}
;
};
const initialColumns = [
{
cellRenderer: IndexCell,
columnData: {},
dataKey: 'lastName',
headerRenderer: ({ columnData }: HeaderRendererArgs) => (
Header {columnData.columnIndex}
),
label: 'index',
width: 200,
},
{
cellRenderer: LastNameCell,
columnData: {},
dataKey: 'lastName',
headerRenderer: ({ columnData }: HeaderRendererArgs) => (
Header {columnData.columnIndex}
),
label: 'last name',
width: 200,
},
{
cellRenderer: FirstNameCell,
columnData: {},
dataKey: 'firstName',
headerRenderer: ({ columnData }: HeaderRendererArgs) => (
Header{columnData.columnIndex}
),
label: 'first name',
width: 200,
},
];
const generateRows = (n: number) => {
const arr = Array.apply(null, Array(n));
return arr.map((_, index) => {
return {
index,
avatar: faker.image.imageUrl(100, 100, 'people'),
firstName: faker.name.firstName(),
lastName: faker.name.lastName(),
};
});
};
const rowCount = 5000;
const rows = generateRows(rowCount);
const rowGetter = ({ index }: RowGetterArgs) => rows[index];
let columns: ColumnProps[] = [];
for (let counter = 0; counter < 10; counter += 1) {
columns = [...initialColumns, ...(columns || [])];
}
columns = columns.map((column, index) => ({
...column,
columnData: { ...(column.columnData || {}), columnIndex: index },
}));
let frozenColumns: ColumnProps[] = [];
for (let counter = 0; counter < 2; counter += 1) {
frozenColumns = [...initialColumns, ...(frozenColumns || [])];
}
frozenColumns = frozenColumns.map((column, index) => ({
...column,
columnData: { ...(column.columnData || {}), columnIndex: index },
}));
const GroupHeaderA = (props: { width: number }) => {
return (
First Group
);
};
const GroupHeaderB = (props: { width: number }) => {
return (
Second Group
);
};
storiesOf('react-data-scroller', module).add('default', () => (
{columns.map((column, index) => (
))}
,
{columns.map((column, index) => (
))}
,
]}
frozenColumns={frozenColumns.map((column, index) => (
))}
/>
));
const customGetRowKey: GetRowKey = ({ renderIndex }) => renderIndex;
storiesOf('react-data-scroller', module).add('custom row key', () => (
{columns.map((column, index) => (
))}
,
{columns.map((column, index) => (
))}
,
]}
frozenColumns={frozenColumns.map((column, index) => (
))}
getRowKey={customGetRowKey}
/>
));
const ScrollToIndexDataScroller = (props: {
children: (arg: {
scrollToIndex: number;
handleScrollToIndex: () => void;
}) => JSX.Element;
}) => {
const [scrollToIndex, setScrollToIndex] = useState();
useEffect(() => {
setScrollToIndex(null);
}, [scrollToIndex]);
const handleScrollToIndex = () => {
setScrollToIndex(20);
};
return props.children({ scrollToIndex, handleScrollToIndex });
};
storiesOf('react-data-scroller', module).add('scroll to index', () => (
{({
scrollToIndex,
handleScrollToIndex,
}: {
scrollToIndex: number;
handleScrollToIndex: () => void;
}) => (
{columns.map((column, index) => (
))}
,
{columns.map((column, index) => (
))}
,
]}
frozenColumns={frozenColumns.map((column, index) => (
))}
/>
)}
));
storiesOf('react-data-scroller', module).add('custom rowRenderer', () => {
const rowRenderer = ({ rowIndex, children }: RowProps) => {
// Render AMOUNT_OF_PADDING_ROWS empty rows
if (rowIndex === 0) {
return My Custom Row!
;
}
return {children}
;
};
return (
{columns.map((column, index) => (
))}
,
{columns.map((column, index) => (
))}
,
]}
frozenColumns={frozenColumns.map((column, index) => (
))}
/>
);
});
storiesOf('react-data-scroller', module).add('DataScrollerContext', () => {
const DetachedIndexCell = ({ index }: { index: number }) => {
return (
{index}
);
};
const DetachedFirstNameCell = ({ firstName }: { firstName: string }) => {
return ;
};
const DetachedLastNameCell = ({ lastName }: { lastName: string }) => {
return {lastName}
;
};
const InjectedIndexCell = getRowData((props: any, data: any) => ({
...props,
index: data[props.rowIndex].index,
}))(DetachedIndexCell);
const InjectedFirstNameCell = getRowData((props: any, data: any) => ({
...props,
firstName: data[props.rowIndex].firstName,
}))(DetachedFirstNameCell);
const InjectedLastNameCell = getRowData((props: any, data: any) => ({
...props,
lastName: data[props.rowIndex].lastName,
}))(DetachedLastNameCell);
const initialContextColumns = [
{
cellRenderer: InjectedIndexCell,
columnData: {},
dataKey: 'lastName',
headerRenderer: ({ columnData }: HeaderRendererArgs) => (
Header {columnData.columnIndex}
),
label: 'index',
width: 200,
},
{
cellRenderer: InjectedLastNameCell,
columnData: {},
dataKey: 'lastName',
headerRenderer: ({ columnData }: HeaderRendererArgs) => (
Header {columnData.columnIndex}
),
label: 'last name',
width: 200,
},
{
cellRenderer: InjectedFirstNameCell,
columnData: {},
dataKey: 'firstName',
headerRenderer: ({ columnData }: HeaderRendererArgs) => (
Header{columnData.columnIndex}
),
label: 'first name',
width: 200,
},
];
const rowGetter = () => {};
let contextColumns: any[] = [];
for (let counter = 0; counter < 10; counter += 1) {
contextColumns = [...initialContextColumns, ...(contextColumns || [])];
}
contextColumns = contextColumns.map((column, index) => ({
...column,
columnData: { ...(column.columnData || {}), columnIndex: index },
}));
let frozenContextColumns: any[] = [];
for (let counter = 0; counter < 2; counter += 1) {
frozenContextColumns = [
...initialContextColumns,
...(frozenContextColumns || []),
];
}
frozenContextColumns = frozenContextColumns.map((column, index) => ({
...column,
columnData: { ...(column.columnData || {}), columnIndex: index },
}));
return (
{contextColumns.map((column, index) => (
))}
,
{contextColumns.map((column, index) => (
))}
,
]}
frozenColumns={frozenContextColumns.map((column, index) => (
))}
/>
);
});