This repository has been archived by the owner on May 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[terra-table] Fix exception case for single row selection with collap…
…sible sections (#2134)
- Loading branch information
Showing
11 changed files
with
190 additions
and
4 deletions.
There are no files selected for viewing
156 changes: 156 additions & 0 deletions
156
...docs/src/terra-dev-site/test/table/TableSingleRowSelectionAndCollapsibleSections.test.jsx
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,156 @@ | ||
import React, { useState, useCallback } from 'react'; | ||
import Table from 'terra-table'; | ||
|
||
const tableData = { | ||
cols: [ | ||
{ | ||
id: 'Column-0', displayName: 'Patient', sortIndicator: 'ascending', isSelectable: true, | ||
}, | ||
{ | ||
id: 'Column-1', displayName: 'Location', isSelectable: true, | ||
}, | ||
{ id: 'Column-2', displayName: 'Illness Severity', isSelectable: true }, | ||
{ id: 'Column-3', displayName: 'Visit' }, | ||
{ id: 'Column-4', displayName: 'Allergy' }, | ||
{ id: 'Column-5', displayName: 'Primary Contact' }, | ||
|
||
], | ||
sections: [{ | ||
id: 'section-0', | ||
isCollapsible: true, | ||
isCollapsed: false, | ||
text: 'Test Section', | ||
rows: [ | ||
{ | ||
id: '1', | ||
cells: [ | ||
{ content: 'Fleck, Arthur' }, | ||
{ content: '1007-MTN' }, | ||
{ content: 'Unstable' }, | ||
{ content: 'Inpatient, 2 months' }, | ||
{ content: '' }, | ||
{ content: 'Quinzell, Harleen' }, | ||
], | ||
}, | ||
{ | ||
id: '2', | ||
cells: [ | ||
{ content: 'Wayne, Bruce' }, | ||
{ content: '1007-MTN-DR' }, | ||
{ content: 'Stable' }, | ||
{ content: 'Outpatient, 2 days' }, | ||
{ content: 'Phytochemicals' }, | ||
{ content: 'Grayson, Richard' }, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
id: 'section-1', | ||
isCollapsible: true, | ||
isCollapsed: false, | ||
text: 'Test Section #2', | ||
rows: [ | ||
{ | ||
id: '3', | ||
cells: [ | ||
{ content: 'Parker, Peter' }, | ||
{ content: '1007-MTN' }, | ||
{ content: 'Unstable' }, | ||
{ content: 'Inpatient, 2 months' }, | ||
{ content: '' }, | ||
{ content: 'Octopus, Doctor' }, | ||
], | ||
}, | ||
{ | ||
id: '4', | ||
cells: [ | ||
{ content: 'Stark, Tony' }, | ||
{ content: '1007-MTN-DR' }, | ||
{ content: 'Stable' }, | ||
{ content: 'Outpatient, 2 days' }, | ||
{ content: 'Phytochemicals' }, | ||
{ content: 'America, Captain' }, | ||
], | ||
}, | ||
], | ||
}], | ||
}; | ||
|
||
const TableSingleRowSelectionAndCollapsibleSections = () => { | ||
const rowHeaderIndex = 0; | ||
const { cols } = tableData; | ||
const [tableSections, setTableSections] = useState(tableData.sections); | ||
|
||
const handleSectionSelect = (sectionId) => { | ||
const newSections = [...tableSections]; | ||
const sectionIndex = newSections.findIndex(section => section.id === sectionId); | ||
|
||
if (sectionIndex > -1) { | ||
newSections[sectionIndex].isCollapsed = !newSections[sectionIndex].isCollapsed; | ||
} | ||
|
||
setTableSections(newSections); | ||
}; | ||
|
||
// const onRowSelect = useCallback((rowSelection) => { | ||
// const { rowId } = rowSelection; | ||
|
||
// const newRowData = [...rowData]; | ||
|
||
// const dataRowToUpdate = newRowData.find(row => row.id === rowId); | ||
|
||
// newRowData.forEach((row) => { | ||
// if (row.id !== dataRowToUpdate.id) { | ||
// // eslint-disable-next-line no-return-assign, no-param-reassign | ||
// row.isSelected = false; | ||
// } | ||
// }); | ||
|
||
// dataRowToUpdate.isSelected = !dataRowToUpdate.isSelected; | ||
|
||
// setRowData(newRowData); | ||
// }, [rowData]); | ||
|
||
const onRowSelect = useCallback((rowSelection) => { | ||
const { sectionId, rowId } = rowSelection; | ||
|
||
const newSections = [...tableSections]; | ||
const currentSection = newSections.find(section => section.id === sectionId); | ||
|
||
const currentSectionRows = [...currentSection.rows]; | ||
const dataRowToUpdate = currentSectionRows.find(row => row.id === rowId); | ||
|
||
newSections.forEach((section) => { | ||
const sectionRowData = [...section.rows]; | ||
sectionRowData.forEach((row) => { | ||
if (row.id !== dataRowToUpdate.id) { | ||
// eslint-disable-next-line no-return-assign, no-param-reassign | ||
row.isSelected = false; | ||
} | ||
}); | ||
|
||
// eslint-disable-next-line no-param-reassign | ||
section.rows = sectionRowData; | ||
}); | ||
|
||
dataRowToUpdate.isSelected = !dataRowToUpdate.isSelected; | ||
setTableSections(newSections); | ||
}, [tableSections]); | ||
|
||
return ( | ||
<Table | ||
id="table-with-single-row-selection" | ||
overflowColumns={cols} | ||
sections={tableSections} | ||
rowHeaderIndex={rowHeaderIndex} | ||
columnWidth="180px" | ||
ariaLabel="Table with Single Row Selection And Collapsible Sections" | ||
rowSelectionMode="single" // Prop to turn row selection mode on/off | ||
onRowSelect={onRowSelect} // For row selection, consumer must provide a callback that the Worklist Data Grid will call when the user selects one or more rows. | ||
onSectionSelect={handleSectionSelect} | ||
/> | ||
); | ||
}; | ||
|
||
export default TableSingleRowSelectionAndCollapsibleSections; |
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
Binary file added
BIN
+24.8 KB
...e/en/chrome_large/table-spec/row-single-selection-with-collapsible-sections.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+23.3 KB
.../en/chrome_medium/table-spec/row-single-selection-with-collapsible-sections.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+20.2 KB
...e/en/chrome_large/table-spec/row-single-selection-with-collapsible-sections.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+18.9 KB
.../en/chrome_medium/table-spec/row-single-selection-with-collapsible-sections.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+25.1 KB
...e/en/chrome_large/table-spec/row-single-selection-with-collapsible-sections.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+23.6 KB
.../en/chrome_medium/table-spec/row-single-selection-with-collapsible-sections.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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