-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFlag.jsx
71 lines (63 loc) · 1.65 KB
/
Flag.jsx
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* TEAM: frontend_infra
* WATCHERS: notandrewkaye
* @flow
*/
import * as React from "react";
import {css, StyleSheet} from "aphrodite";
import typeof {countries as countriesType} from "./constants/AddressOptionConstants.generated";
type Country = $Keys<countriesType>;
const PATH = "https://assets.flexport.com/flags/svg/1/";
const EXTENSION = ".svg";
type Props = {|
/** The `ISO 3166-1 aplpha` 2 character country code that you wish to display */
+countryCode: Country,
/** Flags have square dimensions and will default to width: 100% unless a maxWidth is specified. */
+maxWidth?: number,
|};
type State = {|
loaded: boolean,
error?: any,
|};
/**
* @short An SVG icon component specifically for country flags.
* @brandStatus V2
* @status Stable
* @category General
* @extends React.Component */
export default class Flag extends React.PureComponent<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
loaded: false,
};
}
handleLoaded = () => {
this.setState({loaded: true});
};
render() {
const {countryCode, maxWidth} = this.props;
return (
<img
src={`${PATH}${countryCode}${EXTENSION}`}
alt={`${countryCode} Flag`}
className={css(styles.fade, this.state.loaded && styles.fadeIn)}
style={{
width: "100%",
maxWidth: maxWidth ? `${maxWidth}px` : null,
height: maxWidth ? `${maxWidth}px` : "auto",
}}
onLoad={this.handleLoaded}
/>
);
}
}
const styles = StyleSheet.create({
fade: {
opacity: 0,
transition: "opacity 0.2s cubic-bezier(.42,0,.58,1)",
},
fadeIn: {
opacity: 1,
},
});