UNPKG

@mui/x-data-grid

Version:

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

148 lines (147 loc) 5.66 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default; Object.defineProperty(exports, "__esModule", { value: true }); exports.GridEditBooleanCell = GridEditBooleanCell; exports.renderEditBooleanCell = void 0; var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose")); var React = _interopRequireWildcard(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _clsx = _interopRequireDefault(require("clsx")); var _utils = require("@mui/utils"); var _gridClasses = require("../../constants/gridClasses"); var _useGridRootProps = require("../../hooks/utils/useGridRootProps"); var _useGridApiContext = require("../../hooks/utils/useGridApiContext"); var _jsxRuntime = require("react/jsx-runtime"); const _excluded = ["id", "value", "formattedValue", "api", "field", "row", "rowNode", "colDef", "cellMode", "isEditable", "tabIndex", "className", "hasFocus", "isValidating", "isProcessingProps", "error", "onValueChange"]; const useUtilityClasses = ownerState => { const { classes } = ownerState; const slots = { root: ['editBooleanCell'] }; return (0, _utils.unstable_composeClasses)(slots, _gridClasses.getDataGridUtilityClass, classes); }; function GridEditBooleanCell(props) { const { id: idProp, value, field, className, hasFocus, onValueChange } = props, other = (0, _objectWithoutPropertiesLoose2.default)(props, _excluded); const apiRef = (0, _useGridApiContext.useGridApiContext)(); const inputRef = React.useRef(null); const id = (0, _utils.unstable_useId)(); const [valueState, setValueState] = React.useState(value); const rootProps = (0, _useGridRootProps.useGridRootProps)(); const ownerState = { classes: rootProps.classes }; const classes = useUtilityClasses(ownerState); const handleChange = React.useCallback(async event => { const newValue = event.target.checked; if (onValueChange) { await onValueChange(event, newValue); } setValueState(newValue); await apiRef.current.setEditCellValue({ id: idProp, field, value: newValue }, event); }, [apiRef, field, idProp, onValueChange]); React.useEffect(() => { setValueState(value); }, [value]); (0, _utils.unstable_useEnhancedEffect)(() => { if (hasFocus) { inputRef.current.focus(); } }, [hasFocus]); return /*#__PURE__*/(0, _jsxRuntime.jsx)("label", (0, _extends2.default)({ htmlFor: id, className: (0, _clsx.default)(classes.root, className) }, other, { children: /*#__PURE__*/(0, _jsxRuntime.jsx)(rootProps.slots.baseCheckbox, (0, _extends2.default)({ id: id, inputRef: inputRef, checked: Boolean(valueState), onChange: handleChange, size: "small" }, rootProps.slotProps?.baseCheckbox)) })); } process.env.NODE_ENV !== "production" ? GridEditBooleanCell.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.default.object.isRequired, /** * The mode of the cell. */ cellMode: _propTypes.default.oneOf(['edit', 'view']).isRequired, changeReason: _propTypes.default.oneOf(['debouncedSetEditCellValue', 'setEditCellValue']), /** * The column of the row that the current cell belongs to. */ colDef: _propTypes.default.object.isRequired, /** * The column field of the cell that triggered the event. */ field: _propTypes.default.string.isRequired, /** * The cell value formatted with the column valueFormatter. */ formattedValue: _propTypes.default.any, /** * If true, the cell is the active element. */ hasFocus: _propTypes.default.bool.isRequired, /** * The grid row id. */ id: _propTypes.default.oneOfType([_propTypes.default.number, _propTypes.default.string]).isRequired, /** * If true, the cell is editable. */ isEditable: _propTypes.default.bool, isProcessingProps: _propTypes.default.bool, isValidating: _propTypes.default.bool, /** * Callback called when the value is changed by the user. * @param {React.ChangeEvent<HTMLInputElement>} event The event source of the callback. * @param {boolean} newValue The value that is going to be passed to `apiRef.current.setEditCellValue`. * @returns {Promise<void> | void} A promise to be awaited before calling `apiRef.current.setEditCellValue` */ onValueChange: _propTypes.default.func, /** * The row model of the row that the current cell belongs to. */ row: _propTypes.default.any.isRequired, /** * The node of the row that the current cell belongs to. */ rowNode: _propTypes.default.object.isRequired, /** * the tabIndex value. */ tabIndex: _propTypes.default.oneOf([-1, 0]).isRequired, /** * The cell value. * If the column has `valueGetter`, use `params.row` to directly access the fields. */ value: _propTypes.default.any } : void 0; const renderEditBooleanCell = params => /*#__PURE__*/(0, _jsxRuntime.jsx)(GridEditBooleanCell, (0, _extends2.default)({}, params)); exports.renderEditBooleanCell = renderEditBooleanCell;