Skip to content

Commit

Permalink
Add transpiled files
Browse files Browse the repository at this point in the history
  • Loading branch information
arminmeh committed Jul 15, 2024
1 parent a4d4b83 commit 2505bcf
Show file tree
Hide file tree
Showing 8 changed files with 712 additions and 0 deletions.
13 changes: 13 additions & 0 deletions docs/data/data-grid/custom-columns/renderers/avatar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Avatar from '@mui/material/Avatar';

export function renderAvatar(params) {
if (params.value == null) {
return '';
}

return (
<Avatar style={{ backgroundColor: params.value.color }}>
{params.value.name.toUpperCase().substring(0, 1)}
</Avatar>
);
}
126 changes: 126 additions & 0 deletions docs/data/data-grid/custom-columns/renderers/country.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import * as React from 'react';
import { useGridApiContext } from '@mui/x-data-grid';
import { COUNTRY_ISO_OPTIONS } from '@mui/x-data-grid-generator/services/static-data';
import {
Autocomplete,
autocompleteClasses,
Box,
InputBase,
styled,
} from '@mui/material';

const Country = React.memo(function Country(props) {
const { value } = props;

return (
<Box
sx={{
width: '100%',
display: 'flex',
alignItems: 'center',
'& > img': {
mr: 0.5,
flexShrink: 0,
width: '20px',
},
}}
>
<img
loading="lazy"
width="20"
src={`https://flagcdn.com/w20/${value.code.toLowerCase()}.png`}
srcSet={`https://flagcdn.com/w40/${value.code.toLowerCase()}.png 2x`}
alt=""
/>
<Box component="span" sx={{ overflow: 'hidden', textOverflow: 'ellipsis' }}>
{value.label}
</Box>
</Box>
);
});

const StyledAutocomplete = styled(Autocomplete)(({ theme }) => ({
height: '100%',
[`& .${autocompleteClasses.inputRoot}`]: {
...theme.typography.body2,
padding: '1px 0',
height: '100%',
'& input': {
padding: '0 16px',
height: '100%',
},
},
}));

function EditCountry(props) {
const { id, value, field } = props;

const apiRef = useGridApiContext();

const handleChange = React.useCallback(
async (event, newValue) => {
await apiRef.current.setEditCellValue({ id, field, value: newValue }, event);
apiRef.current.stopCellEditMode({ id, field });
},
[apiRef, field, id],
);

return (
<StyledAutocomplete
value={value}
onChange={handleChange}
options={COUNTRY_ISO_OPTIONS}
getOptionLabel={(option) => option.label}
autoHighlight
fullWidth
open
disableClearable
renderOption={(optionProps, option) => (
<Box
component="li"
sx={{
'& > img': {
mr: 1.5,
flexShrink: 0,
},
}}
{...optionProps}
key={option.code}
>
<img
loading="lazy"
width="20"
src={`https://flagcdn.com/w20/${option.code.toLowerCase()}.png`}
srcSet={`https://flagcdn.com/w40/${option.code.toLowerCase()}.png 2x`}
alt=""
/>
{option.label}
</Box>
)}
renderInput={(params) => (
<InputBase
autoFocus
fullWidth
id={params.id}
inputProps={{
...params.inputProps,
autoComplete: 'new-password', // disable autocomplete and autofill
}}
{...params.InputProps}
/>
)}
/>
);
}

export function renderCountry(params) {
if (params.value == null) {
return '';
}

return <Country value={params.value} />;
}

export function renderEditCountry(params) {
return <EditCountry {...params} />;
}
32 changes: 32 additions & 0 deletions docs/data/data-grid/custom-columns/renderers/email.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import * as React from 'react';
import { styled } from '@mui/material';

const Link = styled('a')({
textOverflow: 'ellipsis',
whiteSpace: 'nowrap',
overflow: 'hidden',
color: 'inherit',
});

const DemoLink = React.memo(function DemoLink(props) {
const handleClick = (event) => {
event.preventDefault();
event.stopPropagation();
};

return (
<Link tabIndex={props.tabIndex} onClick={handleClick} href={props.href}>
{props.children}
</Link>
);
});

export function renderEmail(params) {
const email = params.value ?? '';

return (
<DemoLink href={`mailto:${email}`} tabIndex={params.tabIndex}>
{email}
</DemoLink>
);
}
96 changes: 96 additions & 0 deletions docs/data/data-grid/custom-columns/renderers/incoterm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import * as React from 'react';
import {
Box,
ListItemIcon,
ListItemText,
MenuItem,
Select,
Tooltip,
} from '@mui/material';
import InfoIcon from '@mui/icons-material/Info';
import { useGridApiContext } from '@mui/x-data-grid';
import { INCOTERM_OPTIONS } from '@mui/x-data-grid-generator/services/static-data';

const Incoterm = React.memo(function Incoterm(props) {
const { value } = props;

if (!value) {
return null;
}

const valueStr = value.toString();
const tooltip = valueStr.slice(valueStr.indexOf('(') + 1, valueStr.indexOf(')'));
const code = valueStr.slice(0, valueStr.indexOf('(')).trim();

return (
<Box
sx={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}
>
<span>{code}</span>
<Tooltip title={tooltip}>
<InfoIcon sx={{ color: '#2196f3', alignSelf: 'center', ml: '8px' }} />
</Tooltip>
</Box>
);
});

function EditIncoterm(props) {
const { id, value, field } = props;

const apiRef = useGridApiContext();

const handleChange = async (event) => {
await apiRef.current.setEditCellValue(
{ id, field, value: event.target.value },
event,
);
apiRef.current.stopCellEditMode({ id, field });
};

const handleClose = (event, reason) => {
if (reason === 'backdropClick') {
apiRef.current.stopCellEditMode({ id, field });
}
};

return (
<Select
value={value}
onChange={handleChange}
MenuProps={{
onClose: handleClose,
}}
sx={{
height: '100%',
'& .MuiSelect-select': {
display: 'flex',
alignItems: 'center',
pl: 1,
},
}}
autoFocus
fullWidth
open
>
{INCOTERM_OPTIONS.map((option) => {
const tooltip = option.slice(option.indexOf('(') + 1, option.indexOf(')'));
const code = option.slice(0, option.indexOf('(')).trim();

return (
<MenuItem key={option} value={option}>
<ListItemIcon sx={{ minWidth: 36 }}>{code}</ListItemIcon>
<ListItemText primary={tooltip} sx={{ overflow: 'hidden' }} />
</MenuItem>
);
})}
</Select>
);
}

export function renderIncoterm(params) {
return <Incoterm value={params.value} />;
}

export function renderEditIncoterm(params) {
return <EditIncoterm {...params} />;
}
Loading

0 comments on commit 2505bcf

Please sign in to comment.