1- import React from 'react' ;
2- import { formatDate } from '../../../../../src/services/format' ;
3- import { createDataStore } from '../data_store' ;
4- import {
5- EuiInMemoryTable ,
6- EuiLink ,
7- EuiHealth ,
8- } from '../../../../../src/components' ;
1+ import React , { useState , useMemo } from 'react' ;
2+ import { EuiInMemoryTable , EuiButtonIcon } from '../../../../../src/components' ;
93
104/*
115Example user object:
@@ -29,71 +23,59 @@ Example country object:
2923}
3024*/
3125
32- const store = createDataStore ( ) ;
26+ let tableCounter = 0 ;
3327
3428export const Table = ( ) => {
29+ const [ itemIdToExpandedRowMap , setItemIdToExpandedRowMap ] = useState ( { } ) ;
30+
31+ const toggleDetails = ( item ) => {
32+ const itemIdToExpandedRowMapValues = { ...itemIdToExpandedRowMap } ;
33+ if ( itemIdToExpandedRowMapValues [ item . id ] ) {
34+ delete itemIdToExpandedRowMapValues [ item . id ] ;
35+ } else {
36+ itemIdToExpandedRowMapValues [ item . id ] = < Table /> ;
37+ }
38+ setItemIdToExpandedRowMap ( itemIdToExpandedRowMapValues ) ;
39+ } ;
40+
3541 const columns = [
3642 {
37- field : 'firstName' ,
38- name : 'First Name' ,
39- sortable : true ,
40- truncateText : true ,
43+ field : 'row' ,
44+ name : 'Row Name' ,
45+ width : '4em' ,
4146 } ,
4247 {
43- field : 'lastName' ,
44- name : 'Last Name' ,
45- truncateText : true ,
46- } ,
47- {
48- field : 'github' ,
49- name : 'Github' ,
50- render : ( username ) => (
51- < EuiLink href = { `https://github.com/${ username } ` } target = "_blank" >
52- { username }
53- </ EuiLink >
48+ isExpander : true ,
49+ width : '1em' ,
50+ render : ( item ) => (
51+ < EuiButtonIcon
52+ aria-label = { itemIdToExpandedRowMap [ item . id ] ? 'Collapse' : 'Expand' }
53+ iconType = { itemIdToExpandedRowMap [ item . id ] ? 'arrowUp' : 'arrowDown' }
54+ />
5455 ) ,
5556 } ,
56- {
57- field : 'dateOfBirth' ,
58- name : 'Date of Birth' ,
59- dataType : 'date' ,
60- render : ( date ) => formatDate ( date , 'dobLong' ) ,
61- sortable : true ,
62- } ,
63- {
64- field : 'nationality' ,
65- name : 'Nationality' ,
66- render : ( countryCode ) => {
67- const country = store . getCountry ( countryCode ) ;
68- return `${ country . flag } ${ country . name } ` ;
69- } ,
70- } ,
71- {
72- field : 'online' ,
73- name : 'Online' ,
74- dataType : 'boolean' ,
75- render : ( online ) => {
76- const color = online ? 'success' : 'danger' ;
77- const label = online ? 'Online' : 'Offline' ;
78- return < EuiHealth color = { color } > { label } </ EuiHealth > ;
79- } ,
80- sortable : true ,
81- } ,
8257 ] ;
8358
84- const sorting = {
85- sort : {
86- field : 'dateOfBirth' ,
87- direction : 'desc' ,
88- } ,
89- } ;
59+ const items = useMemo ( ( ) => {
60+ function createItems ( count ) {
61+ let index = 0 ;
62+ tableCounter ++ ;
63+ return Array . apply ( 0 , Array ( count ) ) . map ( ( ) => ( {
64+ id : `${ ++ index } ` ,
65+ row : `row ${ index } in table ${ tableCounter } ` ,
66+ } ) ) ;
67+ }
68+ return createItems ( 5 ) ;
69+ } , [ ] ) ;
9070
9171 return (
9272 < EuiInMemoryTable
93- items = { store . users }
73+ items = { items }
74+ itemId = "id"
75+ itemIdToExpandedRowMap = { itemIdToExpandedRowMap }
76+ isExpandable = { true }
9477 columns = { columns }
95- pagination = { true }
96- sorting = { sorting }
78+ rowProps = { ( row ) => ( { onClick : ( ) => toggleDetails ( row ) } ) }
9779 />
9880 ) ;
9981} ;
0 commit comments