189 lines
6.6 KiB
JavaScript
189 lines
6.6 KiB
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", "hasFocus", "inputProps", "isValidating", "isProcessingProps", "onValueChange"];
|
|
import * as React from 'react';
|
|
import PropTypes from 'prop-types';
|
|
import { unstable_composeClasses as composeClasses, unstable_useEnhancedEffect as useEnhancedEffect } from '@mui/utils';
|
|
import InputBase from '@mui/material/InputBase';
|
|
import { styled } from '@mui/material/styles';
|
|
import { getDataGridUtilityClass } from '../../constants/gridClasses';
|
|
import { useGridRootProps } from '../../hooks/utils/useGridRootProps';
|
|
import { useGridApiContext } from '../../hooks/utils/useGridApiContext';
|
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
const StyledInputBase = styled(InputBase)({
|
|
fontSize: 'inherit'
|
|
});
|
|
const useUtilityClasses = ownerState => {
|
|
const {
|
|
classes
|
|
} = ownerState;
|
|
const slots = {
|
|
root: ['editInputCell']
|
|
};
|
|
return composeClasses(slots, getDataGridUtilityClass, classes);
|
|
};
|
|
function GridEditDateCell(props) {
|
|
const {
|
|
id,
|
|
value: valueProp,
|
|
field,
|
|
colDef,
|
|
hasFocus,
|
|
inputProps,
|
|
onValueChange
|
|
} = props,
|
|
other = _objectWithoutPropertiesLoose(props, _excluded);
|
|
const isDateTime = colDef.type === 'dateTime';
|
|
const apiRef = useGridApiContext();
|
|
const inputRef = React.useRef();
|
|
const valueTransformed = React.useMemo(() => {
|
|
let parsedDate;
|
|
if (valueProp == null) {
|
|
parsedDate = null;
|
|
} else if (valueProp instanceof Date) {
|
|
parsedDate = valueProp;
|
|
} else {
|
|
parsedDate = new Date((valueProp != null ? valueProp : '').toString());
|
|
}
|
|
let formattedDate;
|
|
if (parsedDate == null || Number.isNaN(parsedDate.getTime())) {
|
|
formattedDate = '';
|
|
} else {
|
|
const localDate = new Date(parsedDate.getTime() - parsedDate.getTimezoneOffset() * 60 * 1000);
|
|
formattedDate = localDate.toISOString().substr(0, isDateTime ? 16 : 10);
|
|
}
|
|
return {
|
|
parsed: parsedDate,
|
|
formatted: formattedDate
|
|
};
|
|
}, [valueProp, isDateTime]);
|
|
const [valueState, setValueState] = React.useState(valueTransformed);
|
|
const rootProps = useGridRootProps();
|
|
const ownerState = {
|
|
classes: rootProps.classes
|
|
};
|
|
const classes = useUtilityClasses(ownerState);
|
|
const parseValueToDate = React.useCallback(value => {
|
|
if (value === '') {
|
|
return null;
|
|
}
|
|
const [date, time] = value.split('T');
|
|
const [year, month, day] = date.split('-');
|
|
const parsedDate = new Date();
|
|
parsedDate.setFullYear(Number(year), Number(month) - 1, Number(day));
|
|
parsedDate.setHours(0, 0, 0, 0);
|
|
if (time) {
|
|
const [hours, minutes] = time.split(':');
|
|
parsedDate.setHours(Number(hours), Number(minutes), 0, 0);
|
|
}
|
|
return parsedDate;
|
|
}, []);
|
|
const handleChange = React.useCallback(async event => {
|
|
const newFormattedDate = event.target.value;
|
|
const newParsedDate = parseValueToDate(newFormattedDate);
|
|
if (onValueChange) {
|
|
await onValueChange(event, newParsedDate);
|
|
}
|
|
setValueState({
|
|
parsed: newParsedDate,
|
|
formatted: newFormattedDate
|
|
});
|
|
apiRef.current.setEditCellValue({
|
|
id,
|
|
field,
|
|
value: newParsedDate
|
|
}, event);
|
|
}, [apiRef, field, id, onValueChange, parseValueToDate]);
|
|
React.useEffect(() => {
|
|
setValueState(state => {
|
|
var _valueTransformed$par, _state$parsed;
|
|
if (valueTransformed.parsed !== state.parsed && ((_valueTransformed$par = valueTransformed.parsed) == null ? void 0 : _valueTransformed$par.getTime()) !== ((_state$parsed = state.parsed) == null ? void 0 : _state$parsed.getTime())) {
|
|
return valueTransformed;
|
|
}
|
|
return state;
|
|
});
|
|
}, [valueTransformed]);
|
|
useEnhancedEffect(() => {
|
|
if (hasFocus) {
|
|
inputRef.current.focus();
|
|
}
|
|
}, [hasFocus]);
|
|
return /*#__PURE__*/_jsx(StyledInputBase, _extends({
|
|
inputRef: inputRef,
|
|
fullWidth: true,
|
|
className: classes.root,
|
|
type: isDateTime ? 'datetime-local' : 'date',
|
|
inputProps: _extends({
|
|
max: isDateTime ? '9999-12-31T23:59' : '9999-12-31'
|
|
}, inputProps),
|
|
value: valueState.formatted,
|
|
onChange: handleChange
|
|
}, other));
|
|
}
|
|
process.env.NODE_ENV !== "production" ? GridEditDateCell.propTypes = {
|
|
// ----------------------------- Warning --------------------------------
|
|
// | These PropTypes are generated from the TypeScript type definitions |
|
|
// | To update them edit the TypeScript types and run "yarn 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 {Date | null} 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 { GridEditDateCell };
|
|
export const renderEditDateCell = params => /*#__PURE__*/_jsx(GridEditDateCell, _extends({}, params)); |