-
Notifications
You must be signed in to change notification settings - Fork 841
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into flexing/header_links
- Loading branch information
Showing
25 changed files
with
588 additions
and
56 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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
71 changes: 71 additions & 0 deletions
71
src-docs/src/views/datagrid/datagrid_footer_row_example.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,71 @@ | ||
import React, { Fragment } from 'react'; | ||
|
||
import { renderToHtml } from '../../services'; | ||
|
||
import { GuideSectionTypes } from '../../components'; | ||
import { EuiDataGrid, EuiCodeBlock, EuiCode } from '../../../../src/components'; | ||
|
||
import DataGridFooterRow from './footer_row'; | ||
const dataGridControlColumnsSource = require('!!raw-loader!./footer_row'); | ||
const dataGridControlColumnsHtml = renderToHtml(DataGridFooterRow); | ||
|
||
import { EuiDataGridControlColumn } from '!!prop-loader!../../../../src/components/datagrid/data_grid_types'; | ||
import { EuiDataGridCellValueElementProps } from '!!prop-loader!../../../../src/components/datagrid/data_grid_cell'; | ||
|
||
const gridSnippet = `const footerCellValues = { | ||
// desired data | ||
}; | ||
<EuiDataGrid | ||
{...usualProps} | ||
renderFooterCellValue={({ columnId }) => | ||
footerCellValues[columnId] || null | ||
} | ||
/> | ||
`; | ||
|
||
export const DataGridFooterRowExample = { | ||
title: 'Data grid footer row', | ||
sections: [ | ||
{ | ||
source: [ | ||
{ | ||
type: GuideSectionTypes.JS, | ||
code: dataGridControlColumnsSource, | ||
}, | ||
{ | ||
type: GuideSectionTypes.HTML, | ||
code: dataGridControlColumnsHtml, | ||
}, | ||
], | ||
text: ( | ||
<Fragment> | ||
<p> | ||
A footer row can be used to include value aggregations to the grid. | ||
Values could be based on the dataset, such as sum/average/min/max of | ||
numeric values in a column, or any other necessary data. | ||
</p> | ||
<p> | ||
The footer row is defined by passing{' '} | ||
<EuiCode>renderFooterCellValue</EuiCode> function prop into | ||
EuiDataGrid. <EuiCode>renderFooterCellValue</EuiCode> acts like a | ||
React component, receiving{' '} | ||
<EuiCode>EuiDataGridCellValueElementProps</EuiCode> and returning a | ||
React node. | ||
</p> | ||
<EuiCodeBlock language="javascript" paddingSize="s" isCopyable> | ||
{gridSnippet} | ||
</EuiCodeBlock> | ||
</Fragment> | ||
), | ||
components: { DataGridFooterRow }, | ||
|
||
props: { | ||
EuiDataGrid, | ||
EuiDataGridControlColumn, | ||
EuiDataGridCellValueElementProps, | ||
}, | ||
demo: <DataGridFooterRow />, | ||
}, | ||
], | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import React, { useCallback, useEffect, useMemo, useState } from 'react'; | ||
import { fake } from 'faker'; | ||
|
||
import { | ||
EuiDataGrid, | ||
EuiSwitch, | ||
EuiFlexGroup, | ||
EuiFlexItem, | ||
} from '../../../../src/components/'; | ||
|
||
const raw_data = []; | ||
|
||
for (let i = 1; i < 20; i++) { | ||
raw_data.push({ | ||
name: fake('{{name.lastName}}, {{name.firstName}} {{name.suffix}}'), | ||
date: fake('{{date.past}}'), | ||
amount: fake('${{commerce.price}}'), | ||
phone: fake('{{phone.phoneNumber}}'), | ||
version: fake('{{system.semver}}'), | ||
}); | ||
} | ||
|
||
const columns = [ | ||
{ | ||
id: 'name', | ||
displayAsText: 'Name', | ||
defaultSortDirection: 'asc', | ||
}, | ||
{ | ||
id: 'date', | ||
defaultSortDirection: 'desc', | ||
}, | ||
{ | ||
id: 'amount', | ||
}, | ||
{ | ||
id: 'phone', | ||
isSortable: false, | ||
}, | ||
{ | ||
id: 'version', | ||
defaultSortDirection: 'desc', | ||
}, | ||
]; | ||
|
||
const footerCellValues = { | ||
amount: `Total: $${raw_data.reduce( | ||
(acc, { amount }) => acc + Number(amount.split('$')[1]), | ||
0 | ||
)}`, | ||
version: `Latest: ${ | ||
raw_data.map(({ version }) => version).sort()[raw_data.length - 1] | ||
}`, | ||
}; | ||
|
||
export default () => { | ||
// ** Pagination config | ||
const [pagination, setPagination] = useState({ pageIndex: 0, pageSize: 10 }); | ||
const onChangeItemsPerPage = useCallback( | ||
pageSize => | ||
setPagination(pagination => ({ ...pagination, pageSize, pageIndex: 0 })), | ||
[setPagination] | ||
); | ||
const onChangePage = useCallback( | ||
pageIndex => setPagination(pagination => ({ ...pagination, pageIndex })), | ||
[setPagination] | ||
); | ||
|
||
// Column visibility | ||
const [visibleColumns, setVisibleColumns] = useState(() => | ||
columns.map(({ id }) => id) | ||
); // initialize to the full set of columns | ||
|
||
const renderCellValue = useMemo(() => { | ||
return ({ rowIndex, columnId, setCellProps }) => { | ||
useEffect(() => { | ||
if (columnId === 'amount') { | ||
if (raw_data.hasOwnProperty(rowIndex)) { | ||
const numeric = parseFloat( | ||
raw_data[rowIndex][columnId].match(/\d+\.\d+/)[0], | ||
10 | ||
); | ||
setCellProps({ | ||
style: { | ||
backgroundColor: `rgba(0, 255, 0, ${numeric * 0.0002})`, | ||
}, | ||
}); | ||
} | ||
} | ||
}, [rowIndex, columnId, setCellProps]); | ||
|
||
return raw_data.hasOwnProperty(rowIndex) | ||
? raw_data[rowIndex][columnId] | ||
: null; | ||
}; | ||
}, []); | ||
|
||
const renderFooterCellValue = useCallback( | ||
({ columnId }) => footerCellValues[columnId] || null, | ||
[] | ||
); | ||
|
||
// Footer row | ||
const [showFooterRow, setShowFooterRow] = useState(true); | ||
|
||
return ( | ||
<EuiFlexGroup direction="column"> | ||
<EuiFlexItem> | ||
<EuiSwitch | ||
label="Show footer row" | ||
checked={showFooterRow} | ||
onChange={e => setShowFooterRow(e.target.checked)} | ||
/> | ||
</EuiFlexItem> | ||
<EuiFlexItem> | ||
<EuiDataGrid | ||
aria-label="Data grid footer row demo" | ||
columns={columns} | ||
columnVisibility={{ visibleColumns, setVisibleColumns }} | ||
rowCount={raw_data.length} | ||
renderCellValue={renderCellValue} | ||
renderFooterCellValue={ | ||
showFooterRow ? renderFooterCellValue : undefined | ||
} | ||
pagination={{ | ||
...pagination, | ||
pageSizeOptions: [10, 15, 20], | ||
onChangeItemsPerPage: onChangeItemsPerPage, | ||
onChangePage: onChangePage, | ||
}} | ||
onColumnResize={eventData => { | ||
console.log(eventData); | ||
}} | ||
gridStyle={{ | ||
border: 'horizontal', | ||
rowHover: 'highlight', | ||
header: 'underline', | ||
}} | ||
/> | ||
</EuiFlexItem> | ||
</EuiFlexGroup> | ||
); | ||
}; |
Oops, something went wrong.