Skip to content

Commit

Permalink
Make flags work on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
m4theushw committed Jan 8, 2022
1 parent c0e5abc commit 0a2da36
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 50 deletions.
36 changes: 15 additions & 21 deletions packages/grid/x-data-grid-generator/src/renderer/renderCountry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,6 @@ import * as React from 'react';
import Box from '@mui/material/Box';
import { GridRenderCellParams } from '@mui/x-data-grid-pro';

// ISO 3166-1 alpha-2
// ⚠️ No support for IE 11
function countryToFlag(isoCode: string) {
return typeof String.fromCodePoint !== 'undefined' && isoCode
? isoCode
.toUpperCase()
.replace(/./g, (char) => String.fromCodePoint(char.charCodeAt(0) + 127397))
: isoCode;
}

interface CountryProps {
value: {
code: string;
Expand All @@ -23,18 +13,22 @@ const Country = React.memo(function Country(props: CountryProps) {
const { value } = props;

return (
<Box sx={{ display: 'flex', alignItems: 'center' }}>
<Box
component="span"
sx={{
<Box
sx={{
display: 'flex',
alignItems: 'center',
'& > img': {
mr: '4px',
height: '32px',
width: '32px',
fontSize: '28px',
}}
>
{value.code && countryToFlag(value.code)}
</Box>
width: '20px',
},
}}
>
<img
loading="lazy"
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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,6 @@ import Box from '@mui/material/Box';
import { styled } from '@mui/material/styles';
import { COUNTRY_ISO_OPTIONS } from '../services/static-data';

// ISO 3166-1 alpha-2
// ⚠️ No support for IE 11
function countryToFlag(isoCode: string) {
return typeof String.fromCodePoint !== 'undefined'
? isoCode
.toUpperCase()
.replace(/./g, (char) => String.fromCodePoint(char.charCodeAt(0) + 127397))
: isoCode;
}

const StyledAutocomplete = styled(Autocomplete)(({ theme }) => ({
[`& .${autocompleteClasses.inputRoot}`]: {
...theme.typography.body2,
Expand Down Expand Up @@ -56,14 +46,20 @@ function EditCountry(props: GridRenderEditCellParams) {
<Box
component="li"
sx={{
'& > span': {
mr: '10px',
fontSize: '18px',
'& > img': {
mr: 2,
flexShrink: 0,
},
}}
{...optionProps}
>
<span>{countryToFlag(option.code)}</span>
<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>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,6 @@ import Box from '@mui/material/Box';
import { styled } from '@mui/material/styles';
import { CURRENCY_OPTIONS } from '../services/static-data';

// ISO 3166-1 alpha-2
// ⚠️ No support for IE 11
function countryToFlag(isoCode: string) {
return typeof String.fromCodePoint !== 'undefined'
? isoCode
.toUpperCase()
.replace(/./g, (char) => String.fromCodePoint(char.charCodeAt(0) + 127397))
: isoCode;
}

const StyledAutocomplete = styled(Autocomplete)(({ theme }) => ({
[`& .${autocompleteClasses.inputRoot}`]: {
...theme.typography.body2,
Expand Down Expand Up @@ -51,18 +41,24 @@ function EditCurrency(props: GridRenderEditCellParams) {
fullWidth
open
disableClearable
renderOption={(optionProps, option) => (
renderOption={(optionProps, option: any) => (
<Box
component="li"
sx={{
'& > span': {
mr: '10px',
fontSize: '18px',
'& > img': {
mr: 2,
flexShrink: 0,
},
}}
{...optionProps}
>
<span>{countryToFlag(String(option).slice(0, -1))}</span>
<img
loading="lazy"
width="20"
src={`https://flagcdn.com/w20/${option.slice(0, -1).toLowerCase()}.png`}
srcSet={`https://flagcdn.com/w40/${option.slice(0, -1).toLowerCase()}.png 2x`}
alt=""
/>
{option}
</Box>
)}
Expand Down

0 comments on commit 0a2da36

Please sign in to comment.