Giter Club home page Giter Club logo

react-flags-select's Introduction

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.

react-flags-select's People

Contributors

a-tokyo avatar dependabot[bot] avatar ekwonye-richard avatar eokoneyo avatar floriancassayre avatar henri-vdm avatar ikorovictor avatar itamarmoa avatar kevinbull avatar semantic-release-bot avatar semreg avatar sreenathmp avatar thomasdseao avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

react-flags-select's Issues

Support custom country codes / svg images.

I am using this library for showing the translation options in the application. But I also want to introduce my "own" language and flag to the platform but this is not possible now.
Is this gonna be considered at some point? I would be happy to contribute.

searchable={true} doesnt work

Hi there,
Great component firstly. I am wondering if there is any reason that searchable={true} would not be working for me?
I have tried inspecting the HTML and it doesnt look like it is being generated.

Country name

Hello, is there any possible way to get full country name ? and thank you.

ModuleNotFoundError in CodeSandBox

Hi,

I added the dependency in CodeSandbox and I have the following error :

ModuleNotFoundError
Could not find module in path: 'react-flags-select/flags/us.svg' relative to '/node_modules/react-flags-select/lib/index.js'

Remove `require` from inline usage

Inside the code base there are few inline instances of require inside the src attribute which should not be there.

This also causes problems with the rollup bundler

Is there a way to avoid including all the flags in the bundled output?

I've been evaluating this for use in a project with a really aggressive performance budget, and this library seems to be adding hundreds of KB of flags I'm not using. In fact I really only want a max of 10 flags.

Is there a way I could exclude all the ones I'm not using at build-time, that I'm missing?

flags select box initial loading country flag didn't show

React-flags-select initial loading country flag didn't show . When I select one region, then the flag showed.

import ReactFlagsSelect from 'react-flags-select';
import 'react-flags-select/scss/react-flags-select.scss';

const LocalSelect = () => {

return ( 

 <ReactFlagsSelect
  searchable={true}
  searchPlaceholder="Search"
  className="menu-flags"
  defaultCountry="US"
  showSelectedLabel={true} />     

 )
}

Screenshot_2020-03-12 React App

None of the flag icons are not displaying in the select dropdown

Hello Team,

Thanks for creating this library to display countries list along with their flag icons. I'm trying to get this 'react-flags-select' library working in our app but for some reason, in the select dropdown it only displays countries list and not the flag icons. I'm guessing I'm missing something basic here. Here's what i did so far:

  1. npm install react-flags-select --save
  2. In one of our components, imported this: import ReactFlagsSelect from 'react-flags-select';
  3. Have attached the screenshot of the select dropdown below.

image

Chrome Dev Tools:

  1. On the chrome Dev tools under 'Source' tab (Page > webpack:// ), I could see the all svgs loaded but still the flag icons wont display. Also, I tried installing new svg loader from here https://www.npmjs.com/package/svg-inline-loader but still its not working.

image

  1. Upon clicking the dropdown in the UI, on the network tab in Dev Tools, I see many URLs(i guess corresponding to countries) like this:

http://localhost:3000/payments/%3Csvg%20viewBox=%220%200%20512%20336%22%20xmlns=%22http://www.w3.org/2000/svg%22%3E%3Cg%20fill=%22none%22%3E%3Cpath%20d=%22M503.172%20335.724H8.828A8.829%208.829%200%200%201%200%20326.896V9.104A8.829%208.829%200%200%201%208.828.276h494.345a8.829%208.829%200%200%201%208.828%208.828v317.792a8.83%208.83%200%200%201-8.829%208.828z%22%20fill=%22

Configs:
Reactjs: 16.5.x
Webpack: 3.5.5
OS: Mac-Mojave

There is no option to clear the selection.

I do not found any option to clear the selection.
updateSelected() method is also not applicable in this case, as it first check for the validate the parameter as valid countryCode before update.

Showing multiple times the same flag for different locales

I'm trying to so a language selector using this component but the issue is that for the same country, there are different languages, but only the last one is kept from the customLabels object.

Here is how I'm trying to use customLabels:

<ReactFlagsSelect
  countries={['BE', 'FR']}
  customLabels={{
    BE: 'FR',
    BE: 'NL',
    FR: 'FR'
  }}
/>

Would you mind to implement this use case please?


Adding another BE in the countries list helped a little bit:

<ReactFlagsSelect
  countries={['BE', 'BE', 'FR']}
  customLabels={{
    BE: 'FR',
    BE: 'NL',
    FR: 'FR'
  }}
/>

Screenshot 2020-10-26 at 16 23 28

Page Reloading Issue

There is an issue we can't use react-flags-select in more than one module if we did it than we click on it its automatically refresh the page and POST the form.

Accessibility issue

There is no support provided for inaccessible users .
The following accessibility fixes needs to be added to the component

  1. aria-label for for country name
    2.aria-role and aria-label for the dropdown
  2. aria-role combobox to be added to the dropdown so that screen readers users know how to interact with the control.

Search field is case sensitive

As of right now, the search option looks for identical strings, so searching for "france" returns nothing while searching for "France" returns the right option. Is it possible to make the search case insensitive?

Using string ref is discouraged

Including this library in a modern version of React recites so:

A string ref, "selectedFlag", has been found within a strict mode tree. String refs are a source of potential bugs and should be avoided. We recommend using useRef() or createRef() instead. Learn more about using refs safely here: https://fb.me/react-strict-mode-string-ref

Directly render the options

Hi,

as far as I can see there is no way to automatically open the options, but only clicking on the country-label span.
In my use case I should render this element directly in a modal....and it should be already displaying the options (I see that it depends on the openOptions state param), without clicking anything.
Is it something possible, now or in the near future?

Thanks in advance!
Lorenzo

How to add key attribute?

Because of the country or country I get from database, how could I put key for each option's select?

After closing the dropdown searchable field is not getting cleared

after searching any country from the list if I don't select the country & close the dropdown country remains on the list.

In the attached example see I have searched for Cuba & close the dropdown without selecting Cuba from the list. After that again when I open the list search field is cleared but the list is not updated.
Screen Shot 2020-06-11 at 11 34 17 AM

Thanks in advance!

Unexpected token in every .svg file.

Awesome library, however I'm having issues getting it to work. First of all i'd like to bring to your attention that travis CI reports the build is failing. https://travis-ci.org/ekwonye-richard/react-flags-select

In addition, In every .svg file there seems to be an Unexpected token, it throws the following error for each svg:

WARNING in ./node_modules/react-flags-select/flags/zw.svg
Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: /Users/User/Documents/react-template/node_modules/react-flags-select/flags/zw.svg: Unexpected token (10:9)

Anyone else experiencing this issue?

I tried running the demo on React v16+ and it won't run. Is it possible the library is not compatible with v16?

SyntaxError: Unexpected token < /node_modules/react-flags-select/flags/us.svg:1

I'm using this package in my next.js application. I had no issue integrating it in my react-native and create-react-app. I have tried many different solutions but had no luck
//my package.json "dependencies": { "@zeit/next-css": "^1.0.1", "@zeit/next-sass": "^1.0.1", "next": "9.2.2", "next-images": "^1.3.1", "node-sass": "^4.13.1", "react": "16.12.0", "react-dom": "16.12.0", "react-flags-select": "^1.1.10" }, "devDependencies": { "babel-plugin-inline-react-svg": "^1.1.1" }

How can I make it work with my next.js application

All SVG flags are included during build

Thanks for making this useful library.

I was running into an issue I can't quite seem to debug. I'm using your package in one of the described use cases: a static list of countries should be available in the dropdown.

// Unicode language subtags I want available
const languages = ['cs', 'de', 'en', 'es', 'fr', 'it', 'ja', 'ko', 'pl', 'ru', 'uk', 'zh'];
// Language codes as defined by react-flags-select, along with their language
const languageCodes = {
  cs: ['CZ', 'čeština'],
  de: ['DE', 'Deutsche'],
  en: ['GB', 'English'],
  es: ['ES', 'Español'],
  fr: ['FR', 'Français'],
  it: ['IT', 'Italiano'],
  ja: ['JP', '日本人'],
  ko: ['KR', '한국어'],
  pl: ['PL', 'Polskie'],
  ru: ['RU', 'русский'],
  uk: ['UA', 'Українська'],
  zh: ['CN', '中文'],
};
// Create an array from the unicode subtags to the react-flags-select tags:
const countries = languages.map(language => languageCodes[language][0]);
// Also create a mapping of the custom labels
const countryLabels = Object.values(languageCodes).reduce((acc, [code, label]) => {
  acc[code] = label;
  return acc;
}, {});
...
...
...
<ReactFlagsSelect
    alignOptions="left"
    className={styles.language}
    countries={countries}
    customLabels={countryLabels}
    defaultCountry={languageCodes[language][0]}
    onSelect={this.onSelect}
/>

As it stands, when I run my build I end up with all of the SVG files in the flags folder in my dist folder. I looked at your code and from what I can see you dynamically require the SVG files. I also checked that the countries in your this.state.countries only includes my list of countries during runtime.

I'm not quite sure why all the SVGs end up in my dist folder and thought perhaps you had run into this before. I'm using webpack 4+ as my bundler with a pretty standard setup (can paste if necessary).

Thanks!

Keyboard operation for accessibility

There is currently no support for keyboard navigation through the component. It should be possible to be able to open the dropdown and select a country without using the mouse, but it currently requires clicking.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.