178 lines
7.3 KiB
JavaScript
178 lines
7.3 KiB
JavaScript
'use client';
|
|
|
|
import _extends from "@babel/runtime/helpers/esm/extends";
|
|
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
|
|
const _excluded = ["field", "colDef"];
|
|
import * as React from 'react';
|
|
import PropTypes from 'prop-types';
|
|
import composeClasses from '@mui/utils/composeClasses';
|
|
import useEnhancedEffect from '@mui/utils/useEnhancedEffect';
|
|
import { forwardRef } from '@mui/x-internals/forwardRef';
|
|
import { isMultipleRowSelectionEnabled } from "../../hooks/features/rowSelection/utils.mjs";
|
|
import { useGridSelector } from "../../hooks/utils/useGridSelector.mjs";
|
|
import { gridTabIndexColumnHeaderSelector } from "../../hooks/features/focus/gridFocusStateSelector.mjs";
|
|
import { gridRowSelectionStateSelector } from "../../hooks/features/rowSelection/gridRowSelectionSelector.mjs";
|
|
import { useGridApiContext } from "../../hooks/utils/useGridApiContext.mjs";
|
|
import { getDataGridUtilityClass } from "../../constants/gridClasses.mjs";
|
|
import { useGridRootProps } from "../../hooks/utils/useGridRootProps.mjs";
|
|
import { gridExpandedSortedRowIdsSelector } from "../../hooks/features/filter/gridFilterSelector.mjs";
|
|
import { gridPaginatedVisibleSortedGridRowIdsSelector } from "../../hooks/features/pagination/gridPaginationSelector.mjs";
|
|
import { createRowSelectionManager } from "../../models/gridRowSelectionManager.mjs";
|
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
const useUtilityClasses = ownerState => {
|
|
const {
|
|
classes
|
|
} = ownerState;
|
|
const slots = {
|
|
root: ['checkboxInput']
|
|
};
|
|
return composeClasses(slots, getDataGridUtilityClass, classes);
|
|
};
|
|
const GridHeaderCheckbox = forwardRef(function GridHeaderCheckbox(props, ref) {
|
|
const other = _objectWithoutPropertiesLoose(props, _excluded);
|
|
const [, forceUpdate] = React.useState(false);
|
|
const apiRef = useGridApiContext();
|
|
const rootProps = useGridRootProps();
|
|
const ownerState = {
|
|
classes: rootProps.classes
|
|
};
|
|
const classes = useUtilityClasses(ownerState);
|
|
const tabIndexState = useGridSelector(apiRef, gridTabIndexColumnHeaderSelector);
|
|
const selection = useGridSelector(apiRef, gridRowSelectionStateSelector);
|
|
const visibleRowIds = useGridSelector(apiRef, gridExpandedSortedRowIdsSelector);
|
|
const paginatedVisibleRowIds = useGridSelector(apiRef, gridPaginatedVisibleSortedGridRowIdsSelector);
|
|
const filteredSelection = React.useMemo(() => {
|
|
const isRowSelectable = rootProps.isRowSelectable;
|
|
if (typeof isRowSelectable !== 'function') {
|
|
return selection;
|
|
}
|
|
if (selection.type === 'exclude') {
|
|
return selection;
|
|
}
|
|
|
|
// selection.type === 'include'
|
|
const selectionModel = {
|
|
type: 'include',
|
|
ids: new Set()
|
|
};
|
|
for (const id of selection.ids) {
|
|
if (rootProps.keepNonExistentRowsSelected) {
|
|
selectionModel.ids.add(id);
|
|
}
|
|
// The row might have been deleted
|
|
if (!apiRef.current.getRow(id)) {
|
|
continue;
|
|
}
|
|
if (isRowSelectable(apiRef.current.getRowParams(id))) {
|
|
selectionModel.ids.add(id);
|
|
}
|
|
}
|
|
return selectionModel;
|
|
}, [apiRef, rootProps.isRowSelectable, rootProps.keepNonExistentRowsSelected, selection]);
|
|
|
|
// All the rows that could be selected / unselected by toggling this checkbox
|
|
const selectionCandidates = React.useMemo(() => {
|
|
const rowIds = !rootProps.pagination || !rootProps.checkboxSelectionVisibleOnly || rootProps.paginationMode === 'server' ? visibleRowIds : paginatedVisibleRowIds;
|
|
|
|
// Convert to a Set to make O(1) checking if a row exists or not
|
|
const candidates = new Set();
|
|
for (let i = 0; i < rowIds.length; i += 1) {
|
|
const id = rowIds[i];
|
|
if (!apiRef.current.getRow(id)) {
|
|
// The row could have been removed
|
|
continue;
|
|
}
|
|
if (apiRef.current.isRowSelectable(id)) {
|
|
candidates.add(id);
|
|
}
|
|
}
|
|
return candidates;
|
|
}, [apiRef, rootProps.pagination, rootProps.paginationMode, rootProps.checkboxSelectionVisibleOnly, paginatedVisibleRowIds, visibleRowIds]);
|
|
|
|
// Amount of rows selected and that are visible in the current page
|
|
const currentSelectionSize = React.useMemo(() => {
|
|
const selectionManager = createRowSelectionManager(filteredSelection);
|
|
let size = 0;
|
|
for (const id of selectionCandidates) {
|
|
if (selectionManager.has(id)) {
|
|
size += 1;
|
|
}
|
|
}
|
|
return size;
|
|
}, [filteredSelection, selectionCandidates]);
|
|
const isIndeterminate = React.useMemo(() => {
|
|
if (currentSelectionSize === 0) {
|
|
return false;
|
|
}
|
|
const selectionManager = createRowSelectionManager(filteredSelection);
|
|
for (const rowId of selectionCandidates) {
|
|
if (!selectionManager.has(rowId)) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}, [currentSelectionSize, filteredSelection, selectionCandidates]);
|
|
const isChecked = currentSelectionSize > 0;
|
|
const handleChange = event => {
|
|
const params = {
|
|
value: event.target.checked
|
|
};
|
|
apiRef.current.publishEvent('headerSelectionCheckboxChange', params);
|
|
};
|
|
const multipleSelectionEnabled = isMultipleRowSelectionEnabled(rootProps);
|
|
const tabIndex = tabIndexState !== null && tabIndexState.field === props.field && multipleSelectionEnabled ? 0 : -1;
|
|
useEnhancedEffect(() => {
|
|
const element = apiRef.current.getColumnHeaderElement(props.field);
|
|
if (tabIndex === 0 && element && multipleSelectionEnabled) {
|
|
element.tabIndex = -1;
|
|
}
|
|
}, [tabIndex, apiRef, props.field, multipleSelectionEnabled]);
|
|
const handleKeyDown = React.useCallback(event => {
|
|
if (event.key === ' ') {
|
|
// imperative toggle the checkbox because Space is disable by some preventDefault
|
|
apiRef.current.publishEvent('headerSelectionCheckboxChange', {
|
|
value: !isChecked
|
|
});
|
|
}
|
|
}, [apiRef, isChecked]);
|
|
const handleSelectionChange = React.useCallback(() => {
|
|
forceUpdate(p => !p);
|
|
}, []);
|
|
React.useEffect(() => {
|
|
return apiRef.current.subscribeEvent('rowSelectionChange', handleSelectionChange);
|
|
}, [apiRef, handleSelectionChange]);
|
|
const label = apiRef.current.getLocaleText(isChecked && !isIndeterminate ? 'checkboxSelectionUnselectAllRows' : 'checkboxSelectionSelectAllRows');
|
|
return /*#__PURE__*/_jsx(rootProps.slots.baseCheckbox, _extends({
|
|
indeterminate: isIndeterminate,
|
|
checked: isChecked && !isIndeterminate,
|
|
onChange: handleChange,
|
|
className: classes.root,
|
|
slotProps: {
|
|
htmlInput: {
|
|
'aria-label': label,
|
|
name: 'select_all_rows'
|
|
}
|
|
},
|
|
tabIndex: tabIndex,
|
|
onKeyDown: handleKeyDown,
|
|
disabled: !multipleSelectionEnabled
|
|
}, rootProps.slotProps?.baseCheckbox, other, {
|
|
ref: ref
|
|
}));
|
|
});
|
|
if (process.env.NODE_ENV !== "production") GridHeaderCheckbox.displayName = "GridHeaderCheckbox";
|
|
process.env.NODE_ENV !== "production" ? GridHeaderCheckbox.propTypes /* remove-proptypes */ = {
|
|
// ----------------------------- Warning --------------------------------
|
|
// | These PropTypes are generated from the TypeScript type definitions |
|
|
// | To update them edit the TypeScript types and run "pnpm proptypes" |
|
|
// ----------------------------------------------------------------------
|
|
/**
|
|
* The column of the current header component.
|
|
*/
|
|
colDef: PropTypes.object.isRequired,
|
|
/**
|
|
* The column field of the column that triggered the event
|
|
*/
|
|
field: PropTypes.string.isRequired
|
|
} : void 0;
|
|
export { GridHeaderCheckbox }; |