UNPKG

@atlaskit/dynamic-table

Version:

A dynamic table displays rows of data with built-in pagination, sorting, and re-ordering functionality.

74 lines (71 loc) 1.96 kB
export const getPageRows = (allRows, pageNumber, rowsPerPage) => { if (!pageNumber || !rowsPerPage || !allRows.length) { return []; } return allRows.slice((pageNumber - 1) * rowsPerPage, pageNumber * rowsPerPage); }; export const assertIsSortable = head => { if (!head || !head.cells) { return; } head.cells.forEach(cell => { if (cell.isSortable && !cell.key) { try { throw Error("isSortable can't be set to true, if the 'key' prop is missing."); } catch (e) { // eslint-disable-next-line no-console console.error(e); } } }); }; export const validateSortKey = (sortKey, head) => { if (!sortKey) { return; } const headHasKey = head && head.cells.map(cell => cell.key).includes(sortKey); if (!headHasKey) { try { throw Error(`Cell with ${sortKey} key not found in head.`); } catch (e) { // eslint-disable-next-line no-console console.error(e); } } }; // creates inline styles if flag ranking is true export const inlineStylesIfRanking = (isRanking, width, height) => { if (!isRanking) { return {}; } if (height) { return { width, height }; } return { width }; }; // computes index of dropped item after ranking export const computeIndex = (index, page, rowsPerPage) => { const itemOnPreviousPages = rowsPerPage && isFinite(rowsPerPage) ? (page - 1) * rowsPerPage : 0; return index + itemOnPreviousPages; }; // reorder rows in table after ranking export const reorderRows = (rankEnd, rows, page = 1, rowsPerPage) => { const { destination, sourceIndex } = rankEnd; if (!destination) { return rows; } const fromIndex = computeIndex(sourceIndex, page, rowsPerPage); const toIndex = computeIndex(destination.index, page, rowsPerPage); const reordered = rows.slice(); const [removed] = reordered.splice(fromIndex, 1); reordered.splice(toIndex, 0, removed); return reordered; };