Skip to content

ekwonye-richard/react-flags-select

Repository files navigation

react-flags-select

A React library that provides a customizable SVG flags select components and standalone SVG flags components.

Demo and Example

Live demo: ekwonye-richard.github.io/react-flags-select/

Installation

yarn add react-flags-select
npm install react-flags-select --save

Usage

ReactFlagsSelect

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;

Country Flag

import React from "react";
import { Us } from "react-flags-select";

const Region = () => (
  <div>
    <Us /> United States
  </div>
);

export default Region;

Country Codes

Full list of Country Codes.

Props

selected

selected is a required string prop that holds the current value of the input.

    <ReactFlagsSelect
      ...
      selected={selected}
    />

onSelect

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

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

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

searchPlaceholder is an optional string prop used replace the default placeholder text for the search input.

    <ReactFlagsSelect
      ...
      searchPlaceholder="Search countries"
    />

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

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

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

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

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

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

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

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

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

className is an optional string prop used to pass a className to the top container of the Select component.

    <ReactFlagsSelect
      ...
      className="menu-flags"
    />

selectButtonClassName

selectButtonClassName is an optional string prop used to pass a className to the select button.

    <ReactFlagsSelect
      ...
      selectButtonClassName="menu-flags-button"
    />

fullWidth

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

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

disabled is a boolean prop used to disable to the Select component. The default value is false.

    <ReactFlagsSelect
      ...
      disabled
    />

id

id is a string prop used to set the id of the top container of the Select component.

    <ReactFlagsSelect
      ...
      id="flags-select"
    />

rfsKey

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"
    />

Flags

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.

Contribution

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.

Installation

yarn install

Start Storybook

Builds SVG components to `src/components/Flags/Countries/

yarn start

v1 Documentation

React Flags Select v1

License

This project is distributed under the MIT license.