import React, { useState } from 'react'; import { action } from 'storybook/actions'; import { Column, EmptyCell, Table, } from '../src/lib/components/tablev2/Tablev2.component'; import { Title } from './common'; import { BrowserRouter, BrowserRouter as Router, useLocation, } from 'react-router-dom'; import { CellProps, Row } from 'react-table'; import { Box, Button } from '../src/lib/next'; import styled from 'styled-components'; const Flex = styled(Box)` display: flex; `; const info = { title: 'Components/Data Display/Table', component: Table, }; export default info; const data: Entry[] = [ { id: 1, firstName: 'Sotiria-long-long-long-long-long', lastName: 'Agathangelou', age: undefined, health: 'healthy', }, { id: 2, firstName: 'Stefania', lastName: 'Evgenios', age: 27, health: 'warning', }, { id: 3, firstName: 'Yohann', lastName: 'Rodolph', age: 27, health: 'critical', }, { id: 4, firstName: 'Ninette', lastName: 'Caroline', age: 31, health: 'healthy', }, ]; type Entry = { id: number; firstName: string; lastName: string; age?: number; health: string; }; const columns: Column[] = [ { Header: 'First Name', accessor: 'firstName', cellStyle: { width: 'unset', flex: 2, textAlign: 'left', }, Cell: ({ value }) => { if (value) return <>{value}; return ; }, }, { Header: 'Last Name', accessor: 'lastName', cellStyle: { width: 'unset', flex: 2, textAlign: 'left', }, // disable the sorting on this column disableSortBy: true, }, { Header: 'Age', accessor: 'age', cellStyle: { width: 'unset', flex: 1, textAlign: 'left', }, }, { Header: 'Health', accessor: 'health', sortType: 'health', cellStyle: { width: 'unset', flex: 1, textAlign: 'left', }, }, ]; const getRowId = (row: Entry, relativeIndex: number) => { return row.lastName + ' ' + row.firstName; }; export const SimpleContentTable = { render: ({}) => { const TableWithQueryParams = ({}) => { const location = useLocation(); return ( <> {location.search}
); }; return ( <> Non Selectable Table
Single Selectable Table
Table with Search
Empty table
MultiSelect
{ console.log('Table.MultiSelectableContent selected row', rows); }} />
); }, }; export const asyncTable = { render: ({}) => { function DataComponent({ data, loading, row, }: { row: Row; loading: boolean; data: string; }) { return loading ? ( loading ... ) : ( {`${row.values.firstName} ${data}`} ); } function RowAsync({ row }: { row: Row }) { const [loading, setLoading] = React.useState(true); const [data, setData] = React.useState(''); React.useEffect(() => { const timer = setTimeout(() => { setData('loaded async'); setLoading(false); }, 1000); return () => { clearTimeout(timer); }; }, []); return ; } const renderRowSubComponent = React.useCallback( ({ row, ...rest }: CellProps) => { return ; }, [], ); const columnAsync: Column[] = [ { Header: 'First Name', accessor: 'firstName', cellStyle: { textAlign: 'left', width: 'unset', flex: 1, }, Cell: renderRowSubComponent, }, { Header: 'Last Name', accessor: 'lastName', cellStyle: { textAlign: 'left', width: 'unset', flex: 1, }, }, { Header: 'Age', accessor: 'age', cellStyle: { textAlign: 'left', width: 'unset', flex: 0.5, }, }, { Header: 'Health', accessor: 'health', sortType: 'health', cellStyle: { textAlign: 'left', width: 'unset', flex: 1, }, }, ]; return ( <> async cell Table
); }, }; export const OnBottomCallback = { render: ({}) => { const columns: Column<{ index: number; value: number }>[] = [ { Header: 'value', accessor: 'value', cellStyle: { textAlign: 'left', }, }, ]; const createData = (indexStart = 0) => { const data: { index: number; value: number }[] = []; for (let i = 0; i < 100; i++) { data.push({ index: indexStart + i, value: Math.floor(Math.random() * 1000), }); } return data; }; const [randomData, setRandomData] = useState(createData()); const onBottom = () => { action('onBottom'); setRandomData([...randomData, ...createData(randomData.length)]); }; return ( <> async cell Table
); }, }; export const MultiTable = { render: ({}) => { const [data1, setData1] = useState([ { name: 'test', volume: 1, capacity: '1Gi', }, { name: 'test', volume: 1, capacity: '1Gi', }, { name: 'test', volume: 1, capacity: '1Gi', }, ]); const [data2, setData2] = useState([ { name: 'test', volume: 1, capacity: '1Gi', }, { name: 'test', volume: 1, capacity: '1Gi', }, { name: 'test', volume: 1, capacity: '1Gi', }, ]); const columns2: Column<(typeof data2)[number]>[] = [ { Header: 'Name', accessor: 'name', }, { Header: 'Volume', accessor: 'volume', }, { Header: 'Capacity', accessor: 'capacity', }, ]; const demo = () => { setData1([ { name: 'test', volume: 1, capacity: '1Gi', }, { name: 'test', volume: 1, capacity: '1Gi', }, ]); setData2([ { name: 'test', volume: 1, capacity: '2Gi', }, { name: 'test', volume: 1, capacity: '1Gi', }, ]); }; return ( <> Several Multiselect { console.log( 'Table.MultiSelectableContent selected row', rows, ); }} />