UNPKG

@mui/x-data-grid

Version:

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

158 lines (157 loc) 5.39 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 composeClasses from '@mui/utils/composeClasses'; 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 handleChange = event => { const params = { value: event.target.checked, id }; apiRef.current.publishEvent('rowSelectionCheckboxChange', params, event); }; React.useLayoutEffect(() => { if (tabIndex === 0) { const element = apiRef.current.getCellElement(id, field); if (element) { element.tabIndex = -1; } } }, [apiRef, tabIndex, id, field]); const handleKeyDown = React.useCallback(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(); } }, []); const isSelectable = apiRef.current.isRowSelectable(id); const { isIndeterminate, isChecked } = useGridSelector(apiRef, checkboxPropsSelector, { groupId: id, autoSelectParents: rootProps.rowSelectionPropagation?.parents ?? false }); 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: tabIndex, checked: isChecked && !isIndeterminate, onChange: handleChange, className: classes.root, slotProps: { htmlInput: { 'aria-label': label, name: 'select_row' } }, onKeyDown: handleKeyDown, indeterminate: isIndeterminate, disabled: !isSelectable }, 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, /** * A ref allowing to set imperative focus. * It can be passed to the element that should receive focus. * @ignore - do not document. */ focusElementRef: PropTypes.oneOfType([PropTypes.func, PropTypes.shape({ current: PropTypes.shape({ focus: PropTypes.func.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;