-
Notifications
You must be signed in to change notification settings - Fork 840
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Data Grid] Add custom actions to DataGridCells and cell popover (#3668)
Co-authored-by: Andrea Del Rio <[email protected]> Co-authored-by: Chandler Prall <[email protected]> Co-authored-by: cchaos <[email protected]> Co-authored-by: Dave Snider <[email protected]>
- Loading branch information
1 parent
debcfea
commit 5226ddb
Showing
13 changed files
with
763 additions
and
171 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
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,152 @@ | ||
import React, { useState, useCallback } from 'react'; | ||
import { fake } from 'faker'; | ||
|
||
import { EuiDataGrid, EuiAvatar } from '../../../../src/components/'; | ||
|
||
const columns = [ | ||
{ | ||
id: 'avatar', | ||
initialWidth: 40, | ||
isResizable: false, | ||
actions: false, | ||
}, | ||
{ | ||
id: 'name', | ||
initialWidth: 100, | ||
isSortable: true, | ||
actions: { | ||
showHide: false, | ||
}, | ||
}, | ||
{ | ||
id: 'email', | ||
isSortable: true, | ||
cellActions: [ | ||
({ rowIndex, columnId, Component }) => { | ||
const row = ++rowIndex; | ||
return ( | ||
<Component | ||
onClick={() => | ||
alert(`Love sent from row ${row}, column "${columnId}"`) | ||
} | ||
iconType="heart" | ||
aria-label={`Send love to ${row}, column "${columnId}" `}> | ||
Send love | ||
</Component> | ||
); | ||
}, | ||
], | ||
}, | ||
{ | ||
id: 'city', | ||
isSortable: true, | ||
cellActions: [ | ||
({ rowIndex, columnId, Component, isExpanded }) => { | ||
const row = ++rowIndex; | ||
const message = isExpanded | ||
? `Cheers sent in Popover to row "${row}" column "${columnId}"` | ||
: `Cheers sent from row ${row}, column "${columnId}"`; | ||
|
||
return ( | ||
<Component | ||
onClick={() => alert(message)} | ||
iconType="cheer" | ||
aria-label={message}> | ||
Cheer | ||
</Component> | ||
); | ||
}, | ||
], | ||
}, | ||
{ | ||
id: 'country', | ||
cellActions: [ | ||
({ rowIndex, columnId, Component }) => { | ||
const row = ++rowIndex; | ||
const label = `Love sent from row ${row}, column "${columnId}"`; | ||
return ( | ||
<Component | ||
onClick={() => | ||
alert(`Love sent from row ${row}, column "${columnId}"`) | ||
} | ||
iconType="heart" | ||
aria-label={label}> | ||
Love this city | ||
</Component> | ||
); | ||
}, | ||
({ rowIndex, columnId, Component }) => { | ||
const row = ++rowIndex; | ||
const label = `Paint country at row ${row}, column "${columnId}"`; | ||
return ( | ||
<Component | ||
onClick={() => | ||
alert(`Paint sent from row ${row}, column "${columnId}"`) | ||
} | ||
iconType="brush" | ||
aria-label={label}> | ||
Paint this city | ||
</Component> | ||
); | ||
}, | ||
], | ||
}, | ||
{ | ||
id: 'account', | ||
}, | ||
]; | ||
|
||
const data = []; | ||
|
||
for (let i = 1; i < 5; i++) { | ||
data.push({ | ||
avatar: ( | ||
<EuiAvatar | ||
size="s" | ||
imageUrl={fake('{{internet.avatar}}')} | ||
name={fake('{{name.lastName}}, {{name.firstName}}')} | ||
/> | ||
), | ||
name: fake('{{name.lastName}}, {{name.firstName}} {{name.suffix}}'), | ||
email: fake('{{internet.email}}'), | ||
city: fake('{{address.city}}'), | ||
country: fake('{{address.country}}'), | ||
account: fake('{{finance.account}}'), | ||
}); | ||
} | ||
|
||
export default () => { | ||
const [pagination, setPagination] = useState({ pageIndex: 0, pageSize: 10 }); | ||
|
||
const [visibleColumns, setVisibleColumns] = useState(() => | ||
columns.map(({ id }) => id) | ||
); | ||
|
||
const setPageIndex = useCallback( | ||
(pageIndex) => setPagination({ ...pagination, pageIndex }), | ||
[pagination, setPagination] | ||
); | ||
const setPageSize = useCallback( | ||
(pageSize) => setPagination({ ...pagination, pageSize, pageIndex: 0 }), | ||
[pagination, setPagination] | ||
); | ||
|
||
return ( | ||
<EuiDataGrid | ||
aria-label="DataGrid demonstrating column sizing constraints" | ||
columns={columns} | ||
columnVisibility={{ | ||
visibleColumns: visibleColumns, | ||
setVisibleColumns: setVisibleColumns, | ||
}} | ||
rowCount={data.length} | ||
renderCellValue={({ rowIndex, columnId }) => data[rowIndex][columnId]} | ||
pagination={{ | ||
...pagination, | ||
pageSizeOptions: [5, 10, 25], | ||
onChangeItemsPerPage: setPageSize, | ||
onChangePage: setPageIndex, | ||
}} | ||
/> | ||
); | ||
}; |
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.