|
| 1 | +import React, { useState, useEffect, useCallback, ReactNode } from 'react'; |
| 2 | +import { faker } from '@faker-js/faker'; |
| 3 | + |
| 4 | +import { |
| 5 | + EuiDataGrid, |
| 6 | + EuiDataGridColumn, |
| 7 | + type RenderCellValue, |
| 8 | + EuiButton, |
| 9 | + EuiSpacer, |
| 10 | + EuiSkeletonText, |
| 11 | +} from '../../../../../src'; |
| 12 | + |
| 13 | +type DataType = Array<{ [key: string]: ReactNode }>; |
| 14 | + |
| 15 | +const columns: EuiDataGridColumn[] = [ |
| 16 | + { id: 'firstName' }, |
| 17 | + { id: 'lastName' }, |
| 18 | + { id: 'suffix' }, |
| 19 | + { id: 'boolean' }, |
| 20 | +]; |
| 21 | + |
| 22 | +const CellValue: RenderCellValue = ({ |
| 23 | + rowIndex, |
| 24 | + columnId, |
| 25 | + // Props from cellContext |
| 26 | + data, |
| 27 | + isLoading, |
| 28 | +}) => { |
| 29 | + if (isLoading) { |
| 30 | + return <EuiSkeletonText lines={1} />; |
| 31 | + } |
| 32 | + |
| 33 | + const value = data[rowIndex][columnId]; |
| 34 | + return value; |
| 35 | +}; |
| 36 | + |
| 37 | +export default () => { |
| 38 | + const [visibleColumns, setVisibleColumns] = useState( |
| 39 | + columns.map(({ id }) => id) |
| 40 | + ); |
| 41 | + |
| 42 | + const [data, setData] = useState<DataType>([]); |
| 43 | + const [cellContext, setCellContext] = useState({ |
| 44 | + data, |
| 45 | + isLoading: false, |
| 46 | + }); |
| 47 | + |
| 48 | + // Mock fetching data from an async API |
| 49 | + const mockLoading = useCallback(() => { |
| 50 | + setCellContext((context) => ({ |
| 51 | + ...context, |
| 52 | + isLoading: true, |
| 53 | + })); |
| 54 | + |
| 55 | + // End the loading state after 3 seconds |
| 56 | + const timeout = setTimeout(() => { |
| 57 | + setCellContext((context) => ({ |
| 58 | + ...context, |
| 59 | + isLoading: false, |
| 60 | + })); |
| 61 | + }, 3000); |
| 62 | + return () => clearTimeout(timeout); |
| 63 | + }, []); |
| 64 | + |
| 65 | + const fetchData = useCallback(() => { |
| 66 | + mockLoading(); |
| 67 | + |
| 68 | + const data: DataType = []; |
| 69 | + for (let i = 1; i < 5; i++) { |
| 70 | + data.push({ |
| 71 | + firstName: faker.person.firstName(), |
| 72 | + lastName: faker.person.lastName(), |
| 73 | + suffix: faker.person.suffix(), |
| 74 | + boolean: `${faker.datatype.boolean()}`, |
| 75 | + }); |
| 76 | + } |
| 77 | + setData(data); |
| 78 | + setCellContext((context) => ({ ...context, data })); |
| 79 | + }, [mockLoading]); |
| 80 | + |
| 81 | + // Fetch data on page load |
| 82 | + useEffect(() => { |
| 83 | + fetchData(); |
| 84 | + }, [fetchData]); |
| 85 | + |
| 86 | + return ( |
| 87 | + <> |
| 88 | + <EuiButton size="s" onClick={fetchData}> |
| 89 | + Fetch grid data |
| 90 | + </EuiButton> |
| 91 | + <EuiSpacer size="s" /> |
| 92 | + <EuiDataGrid |
| 93 | + aria-label="Data grid example of cellContext" |
| 94 | + columns={columns} |
| 95 | + columnVisibility={{ visibleColumns, setVisibleColumns }} |
| 96 | + rowCount={data.length} |
| 97 | + renderCellValue={CellValue} |
| 98 | + cellContext={cellContext} |
| 99 | + /> |
| 100 | + </> |
| 101 | + ); |
| 102 | +}; |
0 commit comments