-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: optimize re-renders #87
Merged
Birkbjo
merged 6 commits into
development
from
fix/refactor-category-combo-table-header
Jul 11, 2022
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cdc2aa3
refactor: to category-combo-table-header
Birkbjo 44b34f5
fix: separate setCurrentItem-context
Birkbjo 711293f
refactor: align paddingCells naming
Birkbjo 39faadb
refactor: use hook for useCategoryColumns
Birkbjo a05fc3b
fix: calculate maxColumnsInSection using actual categoryOptions
Birkbjo 8c8b637
refactor: some cleanup
Birkbjo 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
130 changes: 130 additions & 0 deletions
130
src/data-workspace/category-combo-table/category-combo-table-header.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,130 @@ | ||
import i18n from '@dhis2/d2-i18n' | ||
import { TableRowHead, TableCellHead } from '@dhis2/ui' | ||
import cx from 'classnames' | ||
import PropTypes from 'prop-types' | ||
import React, { useMemo } from 'react' | ||
import { useMetadata, selectors } from '../../metadata/index.js' | ||
import { useActiveCell } from '../data-entry-cell/index.js' | ||
import styles from './category-combo-table.module.css' | ||
import { PaddingCell } from './padding-cell.js' | ||
import { TotalHeader } from './total-cells.js' | ||
|
||
// Computes the span and columns to render in each category-row | ||
// columns are category-options, and needs to be repeated in case of multiple categories | ||
const useCategoryColumns = (categories, numberOfCoCs) => { | ||
const { data: metadata } = useMetadata() | ||
return useMemo(() => { | ||
let catColSpan = numberOfCoCs | ||
return categories.map((c) => { | ||
const categoryOptions = selectors.getCategoryOptionsByCategoryId( | ||
metadata, | ||
c.id | ||
) | ||
const nrOfOptions = c.categoryOptions.length | ||
// catColSpan should always be equal to nrOfOptions in last iteration | ||
// unless anomaly with categoryOptionCombo-generation server-side | ||
if (nrOfOptions > 0 && catColSpan >= nrOfOptions) { | ||
// calculate colSpan for current options | ||
// this is the span for each option, not the "total" span of the row | ||
catColSpan = catColSpan / nrOfOptions | ||
// when table have multiple categories, options need to be repeated for each disaggregation "above" current-category | ||
const repeat = numberOfCoCs / (catColSpan * nrOfOptions) | ||
|
||
const columnsToRender = new Array(repeat) | ||
.fill(0) | ||
.flatMap(() => categoryOptions) | ||
|
||
return { | ||
span: catColSpan, | ||
columns: columnsToRender, | ||
category: c, | ||
} | ||
} else { | ||
console.warn( | ||
`Category ${c.displayFormName} malformed. Number of options: ${nrOfOptions}, span: ${catColSpan}` | ||
) | ||
} | ||
return c | ||
}) | ||
}, [metadata, categories, numberOfCoCs]) | ||
} | ||
|
||
export const CategoryComboTableHeader = ({ | ||
renderRowTotals, | ||
paddingCells, | ||
categoryOptionCombos, | ||
categories, | ||
checkTableActive, | ||
}) => { | ||
const { deId: activeDeId, cocId: activeCocId } = useActiveCell() | ||
|
||
const rowToColumnsMap = useCategoryColumns( | ||
categories, | ||
categoryOptionCombos.length | ||
) | ||
|
||
// Find if this column header includes the active cell's column | ||
const isHeaderActive = (headerIdx, headerColSpan) => { | ||
const activeCellColIdx = categoryOptionCombos.findIndex( | ||
(coc) => activeCocId === coc.id | ||
) | ||
const idxDiff = activeCellColIdx - headerIdx * headerColSpan | ||
return ( | ||
checkTableActive(activeDeId) && | ||
idxDiff < headerColSpan && | ||
idxDiff >= 0 | ||
) | ||
} | ||
Mohammer5 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return rowToColumnsMap.map((colInfo, colInfoIndex) => { | ||
const { span, columns, category } = colInfo | ||
return ( | ||
<TableRowHead key={category.id}> | ||
<TableCellHead | ||
className={styles.categoryNameHeader} | ||
colSpan={'1'} | ||
> | ||
{category.displayFormName !== 'default' && | ||
category.displayFormName} | ||
</TableCellHead> | ||
{columns.map((co, columnIndex) => { | ||
return ( | ||
<TableCellHead | ||
key={columnIndex} | ||
className={cx(styles.tableHeader, { | ||
[styles.active]: isHeaderActive( | ||
columnIndex, | ||
span | ||
), | ||
})} | ||
colSpan={span.toString()} | ||
> | ||
{co.isDefault | ||
? i18n.t('Value') | ||
: co.displayFormName} | ||
</TableCellHead> | ||
) | ||
})} | ||
{paddingCells.map((_, i) => ( | ||
<PaddingCell key={i} /> | ||
))} | ||
{renderRowTotals && colInfoIndex === 0 && ( | ||
<TotalHeader rowSpan={categories.length} /> | ||
)} | ||
</TableRowHead> | ||
) | ||
}) | ||
} | ||
|
||
CategoryComboTableHeader.propTypes = { | ||
categories: PropTypes.array, | ||
// Note that this must be the sorted categoryoOptionCombos, eg. in the same order as they are rendered | ||
categoryOptionCombos: PropTypes.arrayOf( | ||
PropTypes.shape({ | ||
id: PropTypes.string, | ||
}) | ||
), | ||
checkTableActive: PropTypes.func, | ||
paddingCells: PropTypes.array, | ||
renderRowTotals: PropTypes.bool, | ||
} |
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
32 changes: 32 additions & 0 deletions
32
src/data-workspace/category-combo-table/data-element-cell.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,32 @@ | ||
import { TableCell } from '@dhis2/ui' | ||
import cx from 'classnames' | ||
import PropTypes from 'prop-types' | ||
import React from 'react' | ||
import { useActiveCell } from '../data-entry-cell/index.js' | ||
import styles from './category-combo-table.module.css' | ||
|
||
export const DataElementCell = ({ dataElement }) => { | ||
const { deId: activeDeId } = useActiveCell() | ||
return ( | ||
<TableCell | ||
className={cx(styles.dataElementName, { | ||
[styles.active]: dataElement.id === activeDeId, | ||
})} | ||
> | ||
{dataElement.displayFormName} | ||
</TableCell> | ||
) | ||
} | ||
|
||
DataElementCell.propTypes = { | ||
dataElement: PropTypes.shape({ | ||
id: PropTypes.string.isRequired, | ||
categoryCombo: PropTypes.shape({ | ||
id: PropTypes.string, | ||
}), | ||
displayFormName: PropTypes.string, | ||
valueType: PropTypes.string, | ||
}), | ||
} | ||
|
||
export default DataElementCell |
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,9 @@ | ||
import { TableCell } from '@dhis2/ui' | ||
import React from 'react' | ||
import styles from './category-combo-table.module.css' | ||
|
||
export const PaddingCell = () => ( | ||
<TableCell className={styles.paddingCell}></TableCell> | ||
) | ||
|
||
export default PaddingCell |
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.
I'm thinking that it would probably benefit the app's architecture and performance if we were to push this form state subscription down to the level of the actual header cell.
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.
I don't really think it's necessary to push it further. You could in theory have a
CategoryTableHeaderColumns
and move it there, and basically move the rendered component fromcolumns.map...
. But that would only save re-rendering of the static components "Category name" and "PaddingCell". Again these are static, with no dynamic props and no DOM-updated are needed.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.
I was mainly concerned about the nested iterations needed to compute the columns, and that fact that they would need to run whenever a the active cell changes. I suggested solving this by pushing the subscription to the active cell down, but I noticed you are now memoizing these columns in the
useCategoryColumns
. I think that's an equally valid solution.