-
Notifications
You must be signed in to change notification settings - Fork 842
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add controlled pagination props to EuiInMemoryTable (#4038)
* Adds pageSize and pageIndex controlled pagination fields to EuiInMemoryTable's pagination * Update src-docs/src/views/tables/in_memory/in_memory_controlled_pagination_section.js Co-authored-by: Greg Thompson <thompson.glowe@gmail.com> * changelog Co-authored-by: Greg Thompson <thompson.glowe@gmail.com>
- Loading branch information
1 parent
8dcdabd
commit 093f7d9
Showing
8 changed files
with
458 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
src-docs/src/views/tables/in_memory/in_memory_controlled_pagination.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import { formatDate } from '../../../../../src/services/format'; | ||
import { createDataStore } from '../data_store'; | ||
import { | ||
EuiInMemoryTable, | ||
EuiLink, | ||
EuiHealth, | ||
} from '../../../../../src/components'; | ||
|
||
/* | ||
Example user object: | ||
{ | ||
id: '1', | ||
firstName: 'john', | ||
lastName: 'doe', | ||
github: 'johndoe', | ||
dateOfBirth: Date.now(), | ||
nationality: 'NL', | ||
online: true | ||
} | ||
Example country object: | ||
{ | ||
code: 'NL', | ||
name: 'Netherlands', | ||
flag: '🇳🇱' | ||
} | ||
*/ | ||
|
||
const store = createDataStore(); | ||
|
||
export const Table = () => { | ||
const columns = [ | ||
{ | ||
field: 'firstName', | ||
name: 'First Name', | ||
sortable: true, | ||
truncateText: true, | ||
}, | ||
{ | ||
field: 'lastName', | ||
name: 'Last Name', | ||
truncateText: true, | ||
}, | ||
{ | ||
field: 'github', | ||
name: 'Github', | ||
render: username => ( | ||
<EuiLink href={`https://github.com/${username}`} target="_blank"> | ||
{username} | ||
</EuiLink> | ||
), | ||
}, | ||
{ | ||
field: 'dateOfBirth', | ||
name: 'Date of Birth', | ||
dataType: 'date', | ||
render: date => formatDate(date, 'dobLong'), | ||
sortable: true, | ||
}, | ||
{ | ||
field: 'nationality', | ||
name: 'Nationality', | ||
render: countryCode => { | ||
const country = store.getCountry(countryCode); | ||
return `${country.flag} ${country.name}`; | ||
}, | ||
}, | ||
{ | ||
field: 'online', | ||
name: 'Online', | ||
dataType: 'boolean', | ||
render: online => { | ||
const color = online ? 'success' : 'danger'; | ||
const label = online ? 'Online' : 'Offline'; | ||
return <EuiHealth color={color}>{label}</EuiHealth>; | ||
}, | ||
sortable: true, | ||
}, | ||
]; | ||
|
||
const sorting = { | ||
sort: { | ||
field: 'dateOfBirth', | ||
direction: 'desc', | ||
}, | ||
}; | ||
|
||
const [users, setUsers] = useState(store.users); | ||
|
||
useEffect(() => { | ||
const updateInterval = setInterval(() => { | ||
setUsers(users => | ||
// randomly toggle some of the online statuses | ||
users.map(({ online, ...user }) => ({ | ||
...user, | ||
online: Math.random() > 0.7 ? !online : online, | ||
})) | ||
); | ||
}, 1000); | ||
return () => clearInterval(updateInterval); | ||
}, []); | ||
|
||
const [pagination, setPagination] = useState({ pageIndex: 0 }); | ||
|
||
return ( | ||
<EuiInMemoryTable | ||
onTableChange={({ page: { index } }) => | ||
setPagination({ pageIndex: index }) | ||
} | ||
items={users} | ||
columns={columns} | ||
pagination={pagination} | ||
sorting={sorting} | ||
/> | ||
); | ||
}; |
44 changes: 44 additions & 0 deletions
44
src-docs/src/views/tables/in_memory/in_memory_controlled_pagination_section.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import React from 'react'; | ||
import { EuiCode } from '../../../../../src/components'; | ||
import { GuideSectionTypes } from '../../../components'; | ||
import { renderToHtml } from '../../../services'; | ||
|
||
import { Table } from './in_memory_controlled_pagination'; | ||
import { propsInfo } from './props_info'; | ||
|
||
const source = require('!!raw-loader!./in_memory_controlled_pagination'); | ||
const html = renderToHtml(Table); | ||
|
||
export const controlledPaginationSection = { | ||
title: 'In-memory table with controlled pagination', | ||
source: [ | ||
{ | ||
type: GuideSectionTypes.JS, | ||
code: source, | ||
}, | ||
{ | ||
type: GuideSectionTypes.HTML, | ||
code: html, | ||
}, | ||
], | ||
text: ( | ||
<div> | ||
<p> | ||
By default <EuiCode>EuiInMemoryTable</EuiCode> resets its page index | ||
when receiving a new <EuiCode>EuiInMemoryTable</EuiCode> array. To avoid | ||
this behavior the pagination object optionally takes a | ||
<EuiCode>pageIndex</EuiCode> value to control this yourself. | ||
Additionally, <EuiCode>pageSize</EuiCode> can also be controlled the | ||
same way. Both of these are provided to your app during the | ||
<EuiCode>onTableChange</EuiCode> callback. | ||
</p> | ||
<p> | ||
The example below updates the array of users every second, randomly | ||
toggling their online status. Pagination state is maintained by the app, | ||
preventing it from being reset by the updates. | ||
</p> | ||
</div> | ||
), | ||
props: propsInfo, | ||
demo: <Table />, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.