158 lines
4.1 KiB
JavaScript
158 lines
4.1 KiB
JavaScript
|
|
import * as React from 'react';
|
||
|
|
import Chip from '@mui/material/Chip';
|
||
|
|
import ListItemIcon from '@mui/material/ListItemIcon';
|
||
|
|
import ListItemText from '@mui/material/ListItemText';
|
||
|
|
import MenuItem from '@mui/material/MenuItem';
|
||
|
|
import Select from '@mui/material/Select';
|
||
|
|
import { styled } from '@mui/material/styles';
|
||
|
|
import ReportProblemIcon from '@mui/icons-material/ReportProblem';
|
||
|
|
import InfoIcon from '@mui/icons-material/Info';
|
||
|
|
import AutorenewIcon from '@mui/icons-material/Autorenew';
|
||
|
|
import DoneIcon from '@mui/icons-material/Done';
|
||
|
|
import {
|
||
|
|
GridEditModes,
|
||
|
|
useGridApiContext,
|
||
|
|
useGridRootProps,
|
||
|
|
} from '@mui/x-data-grid';
|
||
|
|
// eslint-disable-next-line no-restricted-imports
|
||
|
|
import { STATUS_OPTIONS } from '@mui/x-data-grid-generator/services/static-data';
|
||
|
|
|
||
|
|
const StyledChip = styled(Chip)(({ theme }) => ({
|
||
|
|
justifyContent: 'left',
|
||
|
|
'& .icon': {
|
||
|
|
color: 'inherit',
|
||
|
|
},
|
||
|
|
'&.Open': {
|
||
|
|
color: (theme.vars || theme).palette.info.dark,
|
||
|
|
border: `1px solid ${(theme.vars || theme).palette.info.main}`,
|
||
|
|
},
|
||
|
|
'&.Filled': {
|
||
|
|
color: (theme.vars || theme).palette.success.dark,
|
||
|
|
border: `1px solid ${(theme.vars || theme).palette.success.main}`,
|
||
|
|
},
|
||
|
|
'&.PartiallyFilled': {
|
||
|
|
color: (theme.vars || theme).palette.warning.dark,
|
||
|
|
border: `1px solid ${(theme.vars || theme).palette.warning.main}`,
|
||
|
|
},
|
||
|
|
'&.Rejected': {
|
||
|
|
color: (theme.vars || theme).palette.error.dark,
|
||
|
|
border: `1px solid ${(theme.vars || theme).palette.error.main}`,
|
||
|
|
},
|
||
|
|
}));
|
||
|
|
|
||
|
|
const Status = React.memo((props) => {
|
||
|
|
const { status } = props;
|
||
|
|
|
||
|
|
let icon = null;
|
||
|
|
if (status === 'Rejected') {
|
||
|
|
icon = <ReportProblemIcon className="icon" />;
|
||
|
|
} else if (status === 'Open') {
|
||
|
|
icon = <InfoIcon className="icon" />;
|
||
|
|
} else if (status === 'PartiallyFilled') {
|
||
|
|
icon = <AutorenewIcon className="icon" />;
|
||
|
|
} else if (status === 'Filled') {
|
||
|
|
icon = <DoneIcon className="icon" />;
|
||
|
|
}
|
||
|
|
|
||
|
|
let label = status;
|
||
|
|
if (status === 'PartiallyFilled') {
|
||
|
|
label = 'Partially Filled';
|
||
|
|
}
|
||
|
|
|
||
|
|
return (
|
||
|
|
<StyledChip
|
||
|
|
className={status}
|
||
|
|
icon={icon}
|
||
|
|
size="small"
|
||
|
|
label={label}
|
||
|
|
variant="outlined"
|
||
|
|
/>
|
||
|
|
);
|
||
|
|
});
|
||
|
|
|
||
|
|
function EditStatus(props) {
|
||
|
|
const { id, value, field } = props;
|
||
|
|
const rootProps = useGridRootProps();
|
||
|
|
const apiRef = useGridApiContext();
|
||
|
|
|
||
|
|
const handleChange = async (event) => {
|
||
|
|
const isValid = await apiRef.current.setEditCellValue({
|
||
|
|
id,
|
||
|
|
field,
|
||
|
|
value: event.target.value,
|
||
|
|
});
|
||
|
|
|
||
|
|
if (isValid && rootProps.editMode === GridEditModes.Cell) {
|
||
|
|
apiRef.current.stopCellEditMode({ id, field, cellToFocusAfter: 'below' });
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
const handleClose = (event, reason) => {
|
||
|
|
if (reason === 'backdropClick') {
|
||
|
|
apiRef.current.stopCellEditMode({ id, field, ignoreModifications: true });
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
return (
|
||
|
|
<Select
|
||
|
|
value={value}
|
||
|
|
onChange={handleChange}
|
||
|
|
MenuProps={{
|
||
|
|
onClose: handleClose,
|
||
|
|
}}
|
||
|
|
sx={{
|
||
|
|
height: '100%',
|
||
|
|
'& .MuiSelect-select': {
|
||
|
|
display: 'flex',
|
||
|
|
alignItems: 'center',
|
||
|
|
pl: 1,
|
||
|
|
},
|
||
|
|
}}
|
||
|
|
autoFocus
|
||
|
|
fullWidth
|
||
|
|
open
|
||
|
|
>
|
||
|
|
{STATUS_OPTIONS.map((option) => {
|
||
|
|
let IconComponent = null;
|
||
|
|
if (option === 'Rejected') {
|
||
|
|
IconComponent = ReportProblemIcon;
|
||
|
|
} else if (option === 'Open') {
|
||
|
|
IconComponent = InfoIcon;
|
||
|
|
} else if (option === 'PartiallyFilled') {
|
||
|
|
IconComponent = AutorenewIcon;
|
||
|
|
} else if (option === 'Filled') {
|
||
|
|
IconComponent = DoneIcon;
|
||
|
|
}
|
||
|
|
|
||
|
|
let label = option;
|
||
|
|
if (option === 'PartiallyFilled') {
|
||
|
|
label = 'Partially Filled';
|
||
|
|
}
|
||
|
|
|
||
|
|
return (
|
||
|
|
<MenuItem key={option} value={option}>
|
||
|
|
<ListItemIcon sx={{ minWidth: 36 }}>
|
||
|
|
<IconComponent fontSize="small" />
|
||
|
|
</ListItemIcon>
|
||
|
|
<ListItemText primary={label} sx={{ overflow: 'hidden' }} />
|
||
|
|
</MenuItem>
|
||
|
|
);
|
||
|
|
})}
|
||
|
|
</Select>
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
export function renderStatus(params) {
|
||
|
|
if (params.value == null) {
|
||
|
|
return '';
|
||
|
|
}
|
||
|
|
|
||
|
|
return <Status status={params.value} />;
|
||
|
|
}
|
||
|
|
|
||
|
|
export function renderEditStatus(params) {
|
||
|
|
return <EditStatus {...params} />;
|
||
|
|
}
|
||
|
|
|
||
|
|
export default renderStatus;
|