|
| 1 | +import * as React from "react"; |
| 2 | +import MenuItem from "@material-ui/core/MenuItem/MenuItem" |
| 3 | +import Grid from "@material-ui/core/Grid/Grid" |
| 4 | +import Icon from "@material-ui/core/Icon/Icon" |
| 5 | +import Typography from "@material-ui/core/Typography" |
| 6 | + |
| 7 | +const styles = { |
| 8 | + countryName: { |
| 9 | + fontSize: "1em", |
| 10 | + paddingLeft: "0.5em", |
| 11 | + fontFamily: "AvertaCY" |
| 12 | + }, |
| 13 | + dialNumber: { |
| 14 | + marginLeft: "auto", |
| 15 | + fontSize: "1em", |
| 16 | + fontFamily: "AvertaCY", |
| 17 | + }, |
| 18 | + countryCode: { |
| 19 | + color: "#1D0047", |
| 20 | + fontSize: "1em", |
| 21 | + }, |
| 22 | + countryNameList: { |
| 23 | + color: "#1D0047", fontSize: "1em", fontWeight: 300 |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +export interface CountryItemProps extends CountryDataItem { |
| 28 | + flagSVG: string; |
| 29 | + name: string; |
| 30 | + search: string; |
| 31 | + |
| 32 | + onClick: (event: any) => void |
| 33 | +} |
| 34 | + |
| 35 | +export interface CountryDataItem { |
| 36 | + countryCode: number; |
| 37 | +} |
| 38 | + |
| 39 | +export class CountryMenuItem extends React.Component<CountryItemProps, {}> { |
| 40 | + |
| 41 | + state = { |
| 42 | + value: this.props.countryCode |
| 43 | + } |
| 44 | + |
| 45 | + highlightSearch(name: string, search: string) { |
| 46 | + if (search) { |
| 47 | + const parts = name.split(new RegExp(`(${search})`, "gi")) |
| 48 | + return <Typography style={styles.countryNameList}> |
| 49 | + {parts.map((part, index) => part.toLowerCase() === search.toLowerCase() ? |
| 50 | + <b key={index}>{part}</b> : part)}</Typography> |
| 51 | + } |
| 52 | + return <Typography style={styles.countryNameList}>{name}</Typography> |
| 53 | + } |
| 54 | + |
| 55 | + render() { |
| 56 | + const {onClick, flagSVG, countryCode, name, search} = this.props |
| 57 | + return name.toLowerCase().indexOf(search.toLowerCase()) != -1 && |
| 58 | + <MenuItem onClick={onClick} key={countryCode} style={{padding: "0.5em"}}> |
| 59 | + <Grid container direction="row"> |
| 60 | + <Grid item> |
| 61 | + <Icon>{flagSVG}</Icon> |
| 62 | + </Grid> |
| 63 | + <Grid item style={styles.countryName}> |
| 64 | + {this.highlightSearch(name, search)} |
| 65 | + </Grid> |
| 66 | + <Grid item style={styles.dialNumber}> |
| 67 | + <Typography style={styles.countryCode}>{countryCode}</Typography> |
| 68 | + </Grid> |
| 69 | + </Grid> |
| 70 | + </MenuItem> |
| 71 | + } |
| 72 | + |
| 73 | +} |
0 commit comments