A React library that provides a customizable SVG flags select components and standalone SVG flags components.
Live demo: ekwonye-richard.github.io/react-flags-select/
yarn add react-flags-select
npm install react-flags-select --save
import React, { useState } from "react";
import ReactFlagsSelect from "react-flags-select";
const App = () => {
const [selected, setSelected] = useState("");
<ReactFlagsSelect
selected={selected}
onSelect={(code) => setSelected(code)}
/>;
};
export default App;
import React from "react";
import { Us } from "react-flags-select";
const Region = () => (
<div>
<Us /> United States
</div>
);
export default Region;
Full list of Country Codes.
selected
is a required string
prop that holds the current value of the input.
<ReactFlagsSelect
...
selected={selected}
/>
onSelect
is a required function prop which recieves the user selected countryCode which should be used to update the selected value.
<ReactFlagsSelect
...
onSelect={code => setSelected(code)}
/>
placeholder
is an optional string
prop used replace the default placeholder text for the select input.
<ReactFlagsSelect
countries={["US", "GB", "FR", "DE", "IT"]}
customLabels={{ US: "EN-US", GB: "EN-GB", FR: "FR", DE: "DE", IT: "IT" }}
placeholder="Select Language"
/>
searchable
is an optional boolean
prop used which add the option to search through the options list. The default value is false
.
<ReactFlagsSelect
...
searchable
/>
searchPlaceholder
is an optional string
prop used replace the default placeholder text for the search input.
<ReactFlagsSelect
...
searchPlaceholder="Search countries"
/>
countries
is an optional array
of string
used replace the default full list of countries. Only countries included in the full list are valid.
<ReactFlagsSelect
...
countries={["US", "GB", "FR", "DE", "IT", "NG"]}
/>
blacklistCountries
is an optional boolean
prop used to indicate the countries
prop should be used as blacklisted, hence these countries will be excluded in the options. The defaut value is false
.
<ReactFlagsSelect
...
countries={["US", "GB", "FR", "DE", "IT", "NG"]}
blacklistCountries
/>
customLabels
is an optional object
prop used to define custom labels. The default country name for a country code will be used when the country code has no label passed.
<ReactFlagsSelect
...
customLabels={{"US": "EN-US","GB": "EN-GB","FR": "FR","DE": "DE","IT": "IT"}} />
/>
customLabels
now also accepts an array of objects (CustomLabel
) instead of an array of strings. The default country name will still be shown if no label is passed. Also the secondary label will only be shown if there is one present.
<ReactFlagsSelect
...
customLabels={{
"US": { primary: "EN-US", secondary: "+1" },
"GB": { primary: "EN-GB", secondary: "+44" },
"FR": { primary: "FR" }
}}
/>
/>
customLabels
can also be mixed between CustomLabel
and string
.
<ReactFlagsSelect
...
customLabels={{
"US": { primary: "EN-US", secondary: "+1" },
"GB": { primary: "EN-GB", secondary: "+44" },
"FR": "FR"
}}
/>
/>
showSelectedLabel
is a an optional boolean
prop used to show or hide the label text of a selected country. The default value is true
.
<ReactFlagsSelect
...
showSelectedLabel={false}
/>
showSecondarySelectedLabel
is an optional boolean
prop used to show or hide the secondary label text of a selected country. The default value is true
.
<ReactFlagsSelect
...
showSecondarySelectedLabel={false}
/>
showOptionLabel
is a an optional boolean
prop used to show or hide the label text of countries in the options dropdown. The default value is true
.
<ReactFlagsSelect
...
showOptionLabel={false}
/>
showSecondaryOptionLabel
is a an optional boolean
prop used to show or hide the secondary label text of a countries in the options dropdown. The default value is true
.
<ReactFlagsSelect
...
showSecondaryOptionLabel={false}
/>
selectedSize
is an optional number
prop used to set the size in pixels of the selected label and the corresponding flag.
<ReactFlagsSelect
...
selectedSize={14}
/>
optionsSize
is an optional number
prop used to set the size in pixels of the options labels and the corresponding flags.
<ReactFlagsSelect
...
optionsSize={14}
/>
className
is an optional string
prop used to pass a className
to the top container of the Select component.
<ReactFlagsSelect
...
className="menu-flags"
/>
selectButtonClassName
is an optional string
prop used to pass a className
to the select button.
<ReactFlagsSelect
...
selectButtonClassName="menu-flags-button"
/>
fullWidth
is an optional boolean
prop used to decide if the Select component should render as a block element or inline element. The default value is true
which is a block element.
<ReactFlagsSelect
...
fullWidth={false}
/>
alignOptionsToRight
is an optional boolean
prop useful when fullWidth
is false
to set the options alignment to right. The default value is false
.
<ReactFlagsSelect
...
alignOptionsToRight
/>
disabled
is a boolean
prop used to disable to the Select component. The default value is false
.
<ReactFlagsSelect
...
disabled
/>
id
is a string
prop used to set the id of the top container of the Select component.
<ReactFlagsSelect
...
id="flags-select"
/>
rfsKey
is a string
prop used to generate ids, data-testids and the search input's name. The default value us rfs
. You should have unique rfsKey
props when rendering multiple ReactFlagsSelect
at the same time.
<ReactFlagsSelect
...
rfsKey="app-lang"
/>
Each country flag can be rendered as a SVG component. The components are named by countries Iso2 codes in pascal case.
<GB />
The SVG viewBox
is preserved and SVG inherits it's parent element's text size.
This project is written in Typescript and developed with Storybook. Tests are written with Jest and React Testing Library. Raise a pull request with your changes.
yarn install
Builds SVG components to `src/components/Flags/Countries/
yarn start
This project is distributed under the MIT license.