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
[terra-flowsheet-data-grid] Allow flowsheet sections to have sticky title #2043
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
165 changes: 165 additions & 0 deletions
165
...c/terra-dev-site/test/data-grid/flowsheet-data-grid/BoundedFlowsheetWithSections.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,165 @@ | ||
import React, { useCallback, useState, useRef } from 'react'; | ||
import { FlowsheetDataGrid } from 'terra-data-grid'; | ||
|
||
const gridDataJSON = { | ||
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, | ||
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', | ||
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 BoundedFlowsheetWithSections = () => { | ||
const boundingRef = useRef(null); | ||
const [tableSections, setTableSections] = useState(gridDataJSON.sections); | ||
|
||
const handleSectionSelect = (sectionId) => { | ||
const newSections = [...tableSections]; | ||
const sectionIndex = newSections.findIndex(section => section.id === sectionId); | ||
|
||
const sectionToClear = newSections.find(section => section.id === sectionId); | ||
|
||
sectionToClear.rows = sectionToClear.rows.map(row => ({ | ||
...row, | ||
cells: row.cells.map(cell => ({ | ||
...cell, | ||
isSelected: false, | ||
})), | ||
})); | ||
|
||
if (sectionIndex > -1) { | ||
newSections[sectionIndex].isCollapsed = !newSections[sectionIndex].isCollapsed; | ||
} | ||
|
||
setTableSections(newSections); | ||
}; | ||
|
||
const getClearedSections = useCallback(() => tableSections.map(section => ({ | ||
...section, | ||
rows: section.rows.map(row => ({ | ||
...row, | ||
cells: row.cells.map(cell => ({ | ||
...cell, | ||
isSelected: false, | ||
})), | ||
})), | ||
})), [tableSections]); | ||
|
||
const onCellSelect = useCallback((selectedCell) => { | ||
let selectedSection = tableSections.find(section => section.id === selectedCell.sectionId); | ||
|
||
const columnIndex = gridDataJSON.cols.findIndex(col => col.id === selectedCell.columnId); | ||
const rowIndex = selectedSection.rows.findIndex(row => row.id === selectedCell.rowId); | ||
const previousCell = selectedSection.rows[rowIndex].cells[columnIndex]; | ||
|
||
const newSections = getClearedSections(); | ||
|
||
// // If the current cell is the only selected cell, toggle it to unselected. Otherwise, set it to selected. | ||
selectedSection = newSections.find(section => section.id === selectedCell.sectionId); | ||
selectedSection.rows[rowIndex].cells[columnIndex].isSelected = !previousCell.isSelected; | ||
setTableSections(newSections); | ||
}, [tableSections, getClearedSections]); | ||
|
||
const handleCellRangeSelection = useCallback((cells) => { | ||
const columnIndexesToUpdate = new Set(cells | ||
.map(cell => cell.columnId) | ||
.map(id => gridDataJSON.cols.findIndex(col => col.id === id))); | ||
|
||
const rowsToUpdate = new Set(cells.map(cell => cell.rowId)); | ||
|
||
const newSections = getClearedSections(); | ||
const selectedSection = newSections.find(section => section.id === cells[0].sectionId); | ||
|
||
selectedSection.rows = selectedSection.rows.map(row => ({ | ||
...row, | ||
cells: row.cells.map((cell, cellIndex) => ({ | ||
...cell, | ||
isSelected: columnIndexesToUpdate.has(cellIndex) && rowsToUpdate.has(row.id), | ||
})), | ||
})); | ||
|
||
setTableSections(newSections); | ||
}, [getClearedSections]); | ||
|
||
return ( | ||
// eslint-disable-next-line react/forbid-dom-props | ||
<div style={{ height: '150px', width: '700px' }} ref={boundingRef}> | ||
<FlowsheetDataGrid | ||
id="flowsheet-with-sections-bounded" | ||
columns={gridDataJSON.cols} | ||
sections={tableSections} | ||
onCellSelect={onCellSelect} | ||
onSectionSelect={handleSectionSelect} | ||
onCellRangeSelect={handleCellRangeSelection} | ||
boundingRef={boundingRef} | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
export default BoundedFlowsheetWithSections; |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The 50px offset used here is to reduce button width (under header) further from the bounding width to maintain sticky header. It fails to stick when its exactly set to container width.