@tanstack/table-core
Version:
Headless UI for building powerful tables & datagrids for TS/JS.
85 lines (75 loc) • 3.29 kB
JavaScript
/**
* table-core
*
* Copyright (c) TanStack
*
* This source code is licensed under the MIT license found in the
* LICENSE.md file in the root directory of this source tree.
*
* @license MIT
*/
;
var utils = require('../utils.js');
var ColumnGrouping = require('./ColumnGrouping.js');
var ColumnVisibility = require('./ColumnVisibility.js');
//
const ColumnOrdering = {
getInitialState: state => {
return {
columnOrder: [],
...state
};
},
getDefaultOptions: table => {
return {
onColumnOrderChange: utils.makeStateUpdater('columnOrder', table)
};
},
createColumn: (column, table) => {
column.getIndex = utils.memo(position => [ColumnVisibility._getVisibleLeafColumns(table, position)], columns => columns.findIndex(d => d.id === column.id), utils.getMemoOptions(table.options, 'debugColumns', 'getIndex'));
column.getIsFirstColumn = position => {
var _columns$;
const columns = ColumnVisibility._getVisibleLeafColumns(table, position);
return ((_columns$ = columns[0]) == null ? void 0 : _columns$.id) === column.id;
};
column.getIsLastColumn = position => {
var _columns;
const columns = ColumnVisibility._getVisibleLeafColumns(table, position);
return ((_columns = columns[columns.length - 1]) == null ? void 0 : _columns.id) === column.id;
};
},
createTable: table => {
table.setColumnOrder = updater => table.options.onColumnOrderChange == null ? void 0 : table.options.onColumnOrderChange(updater);
table.resetColumnOrder = defaultState => {
var _table$initialState$c;
table.setColumnOrder(defaultState ? [] : (_table$initialState$c = table.initialState.columnOrder) != null ? _table$initialState$c : []);
};
table._getOrderColumnsFn = utils.memo(() => [table.getState().columnOrder, table.getState().grouping, table.options.groupedColumnMode], (columnOrder, grouping, groupedColumnMode) => columns => {
// Sort grouped columns to the start of the column list
// before the headers are built
let orderedColumns = [];
// If there is no order, return the normal columns
if (!(columnOrder != null && columnOrder.length)) {
orderedColumns = columns;
} else {
const columnOrderCopy = [...columnOrder];
// If there is an order, make a copy of the columns
const columnsCopy = [...columns];
// And make a new ordered array of the columns
// Loop over the columns and place them in order into the new array
while (columnsCopy.length && columnOrderCopy.length) {
const targetColumnId = columnOrderCopy.shift();
const foundIndex = columnsCopy.findIndex(d => d.id === targetColumnId);
if (foundIndex > -1) {
orderedColumns.push(columnsCopy.splice(foundIndex, 1)[0]);
}
}
// If there are any columns left, add them to the end
orderedColumns = [...orderedColumns, ...columnsCopy];
}
return ColumnGrouping.orderColumns(orderedColumns, grouping, groupedColumnMode);
}, utils.getMemoOptions(table.options, 'debugTable', '_getOrderColumnsFn'));
}
};
exports.ColumnOrdering = ColumnOrdering;
//# sourceMappingURL=ColumnOrdering.js.map