-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
country-renderer.tsx
40 lines (36 loc) · 1.04 KB
/
country-renderer.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import React from 'react';
import styled from 'styled-components';
import { CellParams } from '@material-ui-x/grid';
const Container = styled.div`
display: flex;
.country-flag {
margin-right: 5px;
margin-top: 5px;
height: 32px;
width: 32px;
font-size: 32px;
}
.country-name {
overflow: hidden;
text-overflow: ellipsis;
}
`;
// 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;
}
export const Country: React.FC<{ value: { code: string; label: string } }> = React.memo(({ value }) => {
return (
<Container>
<span className={'country-flag'}>{countryToFlag(value.code)}</span>
<span className={'country-name'}>{value.label}</span>
</Container>
);
});
Country.displayName = 'Country';
export function CountryRenderer(params: CellParams) {
return <Country value={params.value! as any} />;
}