UNPKG

@mskcc/carbon-react

Version:

Carbon react components for the MSKCC DSM

103 lines (95 loc) 3.09 kB
/** * MSKCC 2021, 2024 */ 'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var sortStates = require('./sortStates.js'); var sorting = require('../tools/sorting.js'); // Our initialSortState should be `NONE`, unless a consumer has specified a // different initialSortState const initialSortState = sortStates.sortStates.NONE; /** * Utility used to get the next sort state given the following pieces of * information: * * @param {string} prevHeader the value of the previous header * @param {string} header the value of the currently selected header * @param {string} prevState the previous sort state of the table * @returns {string} */ const getNextSortDirection = (prevHeader, header, prevState) => { // If the previous header is equivalent to the current header, we know that we // have to derive the next sort state from the previous sort state if (prevHeader === header) { // When transitioning, we know that the sequence of states is as follows: // NONE -> ASC -> DESC -> NONE if (prevState === 'NONE') { return sortStates.sortStates.ASC; } if (prevState === 'ASC') { return sortStates.sortStates.DESC; } return sortStates.sortStates.NONE; } // Otherwise, we have selected a new header and need to start off by sorting // in descending order by default return sortStates.sortStates.ASC; }; const getNextSortState = (props, state, _ref) => { let { key } = _ref; const { sortDirection, sortHeaderKey } = state; const nextSortDirection = getNextSortDirection(key, sortHeaderKey, sortDirection); return getSortedState(props, state, key, nextSortDirection); }; /** * Derive the set of sorted state fields from props and state for the given * header key and sortDirection * * @param {object} props * @param {string} props.locale The current locale * @param {Function} props.sortRows Method to handle sorting a collection of * rows * @param {object} state * @param {Array<string>} state.rowIds Array of row ids * @param {object} state.cellsById Lookup object for cells by id * @param {Array<string>} state.initialRowOrder Initial row order for the * current set of rows * @param {string} key The key for the given header we are serving the * sorted state for * @param {string} sortDirection The sortState that we want to order by * @returns {object} */ const getSortedState = (props, state, key, sortDirection) => { const { rowIds, cellsById, initialRowOrder } = state; const { locale, sortRow } = props; const nextRowIds = sortDirection !== sortStates.sortStates.NONE ? sorting.sortRows({ rowIds, cellsById, sortDirection, key, locale, sortRow }) : initialRowOrder; return { sortHeaderKey: key, sortDirection: sortDirection, rowIds: nextRowIds }; }; exports.sortStates = sortStates.sortStates; exports.getNextSortDirection = getNextSortDirection; exports.getNextSortState = getNextSortState; exports.getSortedState = getSortedState; exports.initialSortState = initialSortState;