@mui/x-data-grid
Version:
The Community plan edition of the Data Grid components (MUI X).
139 lines • 4.88 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
const _excluded = ["id", "value", "formattedValue", "api", "field", "row", "rowNode", "colDef", "cellMode", "isEditable", "tabIndex", "className", "hasFocus", "isValidating", "isProcessingProps", "error", "onValueChange"];
import * as React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import { unstable_composeClasses as composeClasses, unstable_useId as useId, unstable_useEnhancedEffect as useEnhancedEffect } from '@mui/utils';
import { getDataGridUtilityClass } from "../../constants/gridClasses.js";
import { useGridRootProps } from "../../hooks/utils/useGridRootProps.js";
import { useGridApiContext } from "../../hooks/utils/useGridApiContext.js";
import { jsx as _jsx } from "react/jsx-runtime";
const useUtilityClasses = ownerState => {
const {
classes
} = ownerState;
const slots = {
root: ['editBooleanCell']
};
return composeClasses(slots, getDataGridUtilityClass, classes);
};
function GridEditBooleanCell(props) {
const {
id: idProp,
value,
field,
className,
hasFocus,
onValueChange
} = props,
other = _objectWithoutPropertiesLoose(props, _excluded);
const apiRef = useGridApiContext();
const inputRef = React.useRef(null);
const id = useId();
const [valueState, setValueState] = React.useState(value);
const rootProps = 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]);
useEnhancedEffect(() => {
if (hasFocus) {
inputRef.current.focus();
}
}, [hasFocus]);
return /*#__PURE__*/_jsx("label", _extends({
htmlFor: id,
className: clsx(classes.root, className)
}, other, {
children: /*#__PURE__*/_jsx(rootProps.slots.baseCheckbox, _extends({
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.object.isRequired,
/**
* The mode of the cell.
*/
cellMode: PropTypes.oneOf(['edit', 'view']).isRequired,
changeReason: PropTypes.oneOf(['debouncedSetEditCellValue', 'setEditCellValue']),
/**
* 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,
isProcessingProps: PropTypes.bool,
isValidating: PropTypes.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.func,
/**
* 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 { GridEditBooleanCell };
export const renderEditBooleanCell = params => /*#__PURE__*/_jsx(GridEditBooleanCell, _extends({}, params));