@carbon/react
Version:
React components for the Carbon Design System
48 lines (46 loc) • 1.68 kB
JavaScript
/**
* Copyright IBM Corp. 2016, 2026
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
const require_sorting = require("./sorting.js");
const require_normalize = require("../tools/normalize.js");
//#region src/components/DataTable/state/getDerivedStateFromProps.js
/**
* Copyright IBM Corp. 2016, 2025
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
/**
* Helper to derive the next state from the given props and the
* prevState. Potential future-facing API hook for React v17.
*
* Currently, it's being used as a way to normalize the incoming data that we
* are receiving for rows
*/
const getDerivedStateFromProps = (props, prevState) => {
const { rowIds, rowsById, cellsById } = require_normalize.default(props.rows, props.headers, prevState);
const state = {
rowIds,
rowsById,
cellsById,
sortDirection: prevState.sortDirection || require_sorting.initialSortState,
sortHeaderKey: prevState.sortHeaderKey || null,
initialRowOrder: rowIds.slice(),
filterInputValue: prevState.filterInputValue || null,
shouldShowBatchActions: prevState.shouldShowBatchActions || false,
isExpandedAll: false
};
if (prevState.sortDirection && prevState.sortHeaderKey) {
const { rowIds } = require_sorting.getSortedState(props, state, prevState.sortHeaderKey, prevState.sortDirection);
state.rowIds = rowIds;
}
state.isExpandedAll = state.rowIds.every((id) => {
return state.rowsById[id].isExpanded === true;
});
return state;
};
//#endregion
exports.default = getDerivedStateFromProps;