UNPKG

@cn-ui/table-core

Version:

Headless UI for building powerful tables & datagrids for TS/JS.

103 lines (98 loc) 4.64 kB
/** * 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 */ 'use strict'; var utils = require('../utils.js'); // const ColumnVisibility = { getInitialState: state => { return { columnVisibility: {}, ...state }; }, getDefaultOptions: table => { return { onColumnVisibilityChange: utils.makeStateUpdater('columnVisibility', table) }; }, createColumn: (column, table) => { column.toggleVisibility = value => { if (column.getCanHide()) { table.setColumnVisibility(old => ({ ...old, [column.id]: value != null ? value : !column.getIsVisible() })); } }; column.getIsVisible = () => { var _ref, _table$getState$colum; const childColumns = column.columns; return (_ref = childColumns.length ? childColumns.some(c => c.getIsVisible()) : (_table$getState$colum = table.getState().columnVisibility) == null ? void 0 : _table$getState$colum[column.id]) != null ? _ref : true; }; column.getCanHide = () => { var _column$columnDef$ena, _table$options$enable; return ((_column$columnDef$ena = column.columnDef.enableHiding) != null ? _column$columnDef$ena : true) && ((_table$options$enable = table.options.enableHiding) != null ? _table$options$enable : true); }; column.getToggleVisibilityHandler = () => { return e => { column.toggleVisibility == null || column.toggleVisibility(e.target.checked); }; }; }, createRow: (row, table) => { row._getAllVisibleCells = () => { const cells = row.getAllCells(); return cells.filter(cell => cell.column.getIsVisible()); }; row.getVisibleCells = () => { return [...row.getLeftVisibleCells(), ...row.getCenterVisibleCells(), ...row.getRightVisibleCells()]; }; }, createTable: table => { const makeVisibleColumnsMethod = (key, getColumns) => { return utils.memo(() => [getColumns(), getColumns().filter(d => d.getIsVisible()).map(d => d.id).join('_')], columns => { return columns.filter(d => d.getIsVisible == null ? void 0 : d.getIsVisible()); }, utils.getMemoOptions(table.options, 'debugColumns', key)); }; table.getVisibleFlatColumns = makeVisibleColumnsMethod('getVisibleFlatColumns', () => table.getAllFlatColumns()); table.getVisibleLeafColumns = makeVisibleColumnsMethod('getVisibleLeafColumns', () => table.getAllLeafColumns()); table.getLeftVisibleLeafColumns = makeVisibleColumnsMethod('getLeftVisibleLeafColumns', () => table.getLeftLeafColumns()); table.getRightVisibleLeafColumns = makeVisibleColumnsMethod('getRightVisibleLeafColumns', () => table.getRightLeafColumns()); table.getCenterVisibleLeafColumns = makeVisibleColumnsMethod('getCenterVisibleLeafColumns', () => table.getCenterLeafColumns()); table.setColumnVisibility = updater => table.options.onColumnVisibilityChange == null ? void 0 : table.options.onColumnVisibilityChange(updater); table.resetColumnVisibility = defaultState => { var _table$initialState$c; table.setColumnVisibility(defaultState ? {} : (_table$initialState$c = table.initialState.columnVisibility) != null ? _table$initialState$c : {}); }; table.toggleAllColumnsVisible = value => { var _value; value = (_value = value) != null ? _value : !table.getIsAllColumnsVisible(); table.setColumnVisibility(table.getAllLeafColumns().reduce((obj, column) => ({ ...obj, [column.id]: !value ? !(column.getCanHide != null && column.getCanHide()) : value }), {})); }; table.getIsAllColumnsVisible = () => !table.getAllLeafColumns().some(column => !(column.getIsVisible != null && column.getIsVisible())); table.getIsSomeColumnsVisible = () => table.getAllLeafColumns().some(column => column.getIsVisible == null ? void 0 : column.getIsVisible()); table.getToggleAllColumnsVisibilityHandler = () => { return e => { var _target; table.toggleAllColumnsVisible((_target = e.target) == null ? void 0 : _target.checked); }; }; } }; function _getVisibleLeafColumns(table, position) { return !position ? table.getVisibleLeafColumns() : position === 'center' ? table.getCenterVisibleLeafColumns() : position === 'left' ? table.getLeftVisibleLeafColumns() : table.getRightVisibleLeafColumns(); } exports.ColumnVisibility = ColumnVisibility; exports._getVisibleLeafColumns = _getVisibleLeafColumns; //# sourceMappingURL=ColumnVisibility.js.map