UNPKG

@mui/x-data-grid

Version:

The Community plan edition of the MUI X Data Grid components.

174 lines (173 loc) 5.68 kB
'use client'; import _extends from "@babel/runtime/helpers/esm/extends"; import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose"; const _excluded = ["field", "id", "formattedValue", "row", "rowNode", "colDef", "isEditable", "cellMode", "hasFocus", "tabIndex", "api"]; import * as React from 'react'; import PropTypes from 'prop-types'; import clsx from 'clsx'; import composeClasses from '@mui/utils/composeClasses'; import useEventCallback from '@mui/utils/useEventCallback'; import { forwardRef } from '@mui/x-internals/forwardRef'; import { useGridApiContext } from "../../hooks/utils/useGridApiContext.js"; import { useGridRootProps } from "../../hooks/utils/useGridRootProps.js"; import { getDataGridUtilityClass } from "../../constants/gridClasses.js"; import { useGridSelector } from "../../hooks/utils/useGridSelector.js"; import { checkboxPropsSelector } from "../../hooks/features/rowSelection/utils.js"; import { jsx as _jsx } from "react/jsx-runtime"; const useUtilityClasses = ownerState => { const { classes } = ownerState; const slots = { root: ['checkboxInput'] }; return composeClasses(slots, getDataGridUtilityClass, classes); }; const GridCellCheckboxForwardRef = forwardRef(function GridCellCheckboxRenderer(props, ref) { const { field, id, rowNode, tabIndex } = props, other = _objectWithoutPropertiesLoose(props, _excluded); const apiRef = useGridApiContext(); const rootProps = useGridRootProps(); const ownerState = { classes: rootProps.classes }; const classes = useUtilityClasses(ownerState); const { isIndeterminate, isChecked, isSelectable } = useGridSelector(apiRef, checkboxPropsSelector, { groupId: id, autoSelectParents: rootProps.rowSelectionPropagation?.parents ?? false }); const disabled = !isSelectable; const handleChange = event => { if (disabled) { return; } const params = { value: event.target.checked, id }; apiRef.current.publishEvent('rowSelectionCheckboxChange', params, event); }; React.useLayoutEffect(() => { if (tabIndex === 0 && !disabled) { const element = apiRef.current.getCellElement(id, field); if (element) { element.tabIndex = -1; } } }, [apiRef, tabIndex, id, field, disabled]); const handleKeyDown = useEventCallback(event => { if (event.key === ' ') { // We call event.stopPropagation to avoid selecting the row and also scrolling to bottom // TODO: Remove and add a check inside useGridKeyboardNavigation event.stopPropagation(); } if (disabled) { return; } }); const handleClick = useEventCallback(event => { if (disabled) { event.preventDefault(); return; } }); const handleMouseDown = useEventCallback(() => { if (disabled) { return; } }); if (rowNode.type === 'footer' || rowNode.type === 'pinnedRow') { return null; } const label = apiRef.current.getLocaleText(isChecked && !isIndeterminate ? 'checkboxSelectionUnselectRow' : 'checkboxSelectionSelectRow'); return /*#__PURE__*/_jsx(rootProps.slots.baseCheckbox, _extends({ tabIndex: disabled ? -1 : tabIndex, checked: isChecked && !isIndeterminate, onChange: handleChange, onClick: handleClick, onMouseDown: handleMouseDown, className: clsx(classes.root, disabled && 'Mui-disabled'), disabled: disabled, material: { disableRipple: disabled }, slotProps: { htmlInput: { 'aria-disabled': disabled || undefined, 'aria-label': label, name: 'select_row' } }, onKeyDown: handleKeyDown, indeterminate: isIndeterminate }, rootProps.slotProps?.baseCheckbox, other, { ref: ref })); }); if (process.env.NODE_ENV !== "production") GridCellCheckboxForwardRef.displayName = "GridCellCheckboxForwardRef"; process.env.NODE_ENV !== "production" ? GridCellCheckboxForwardRef.propTypes = { // ----------------------------- Warning -------------------------------- // | These PropTypes are generated from the TypeScript type definitions | // | To update them edit the TypeScript types and run "pnpm proptypes" | // ---------------------------------------------------------------------- /** * GridApi that let you manipulate the grid. */ api: PropTypes.object.isRequired, /** * The mode of the cell. */ cellMode: PropTypes.oneOf(['edit', 'view']).isRequired, /** * The column of the row that the current cell belongs to. */ colDef: PropTypes.object.isRequired, /** * The column field of the cell that triggered the event. */ field: PropTypes.string.isRequired, /** * The cell value formatted with the column valueFormatter. */ formattedValue: PropTypes.any, /** * If true, the cell is the active element. */ hasFocus: PropTypes.bool.isRequired, /** * The grid row id. */ id: PropTypes.oneOfType([PropTypes.number, PropTypes.string]).isRequired, /** * If true, the cell is editable. */ isEditable: PropTypes.bool, /** * The row model of the row that the current cell belongs to. */ row: PropTypes.any.isRequired, /** * The node of the row that the current cell belongs to. */ rowNode: PropTypes.object.isRequired, /** * the tabIndex value. */ tabIndex: PropTypes.oneOf([-1, 0]).isRequired, /** * The cell value. * If the column has `valueGetter`, use `params.row` to directly access the fields. */ value: PropTypes.any } : void 0; export { GridCellCheckboxForwardRef }; export const GridCellCheckboxRenderer = GridCellCheckboxForwardRef;