Skip to content

Commit

Permalink
[docs-infra] Support code tabs overflow (mui#42913)
Browse files Browse the repository at this point in the history
  • Loading branch information
arminmeh authored and joserodolfofreitas committed Jul 29, 2024
1 parent 315ce5a commit 9d868e7
Show file tree
Hide file tree
Showing 12 changed files with 890 additions and 117 deletions.
16 changes: 16 additions & 0 deletions docs/data/experiments/renderers/renderAvatar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as React from 'react';
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>
);
}

export default renderAvatar;
129 changes: 129 additions & 0 deletions docs/data/experiments/renderers/renderCountry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import * as React from 'react';
import { useGridApiContext } from '@mui/x-data-grid';
// eslint-disable-next-line no-restricted-imports
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} />;
}

export default renderCountry;
34 changes: 34 additions & 0 deletions docs/data/experiments/renderers/renderEmail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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>
);
}

export default renderEmail;
99 changes: 99 additions & 0 deletions docs/data/experiments/renderers/renderIncoterm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
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';
// eslint-disable-next-line no-restricted-imports
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} />;
}

export default renderIncoterm;
Loading

0 comments on commit 9d868e7

Please sign in to comment.