Giter Club home page Giter Club logo

react-select-country-list's Introduction

react-select-country-list

This package take country-list as reference, and make it more friendly to react-select

Maps ISO 3166-1-alpha-2 codes to English country names and match react-select options props.

Uses data from https://www.iso.org/iso-3166-country-codes.html

Install

npm install react-select-country-list --save

or

yarn add react-select-country-list

Example

import React, { useState, useMemo } from 'react'
import Select from 'react-select'
import countryList from 'react-select-country-list'

function CountrySelector() {
  const [value, setValue] = useState('')
  const options = useMemo(() => countryList().getData(), [])

  const changeHandler = value => {
    setValue(value)
  }

  return <Select options={options} value={value} onChange={changeHandler} />
}

export default CountrySelector

Methods

All input is case-insensitive.

getValue(label)

Expects the English country name. Returns the code for that country. If not found, it returns undefined.

getLabel(value)

Expects a two-digit country code. Returns the name for that country. If not found, it returns undefined.

getValues()

Returns an array of all country codes.

getLabels()

Returns an array of all country names.

getValueList()

Returns a key-value object of all countries using the code as key.

getLabelList()

Returns a key-value object of all countries using the name as key.

getData()

Returns an array of all country information, in the same format as it gets imported.

setLabel(value, label)

Due to different perspectives among different regions, this method can help developers customize the label of specific country. What's more, it can be chained with another methods above.

Usage

// Make 'Viet Nam' -> 'Vietnam'
countries.setLabel('VN', 'Vietnam').getLabel('VN') // 'Vietnam'

setEmpty(label)

You may want an empty value option in the list, so here's the helper function for you. Again, it can be chained with another methods above.

Usage

countries.setEmpty('Select a Country').getLabel('') // 'Select a Country'

We can even chain setLabel and setEmpty together to have list with an empty option and the modified label.

countries.setLabel('VN', 'Vietnam').setEmpty('Select a Country').getLabel('VN') // 'Vietnam'

native()

You may want to display native name of countries, this is the method for you. The data source of native names can be found here

Usage

countries.native().getLabel('TW') // 臺灣

License

MIT

react-select-country-list's People

Contributors

andrewsantarin avatar chun-lin avatar joakimeriksson avatar nishanbende 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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

react-select-country-list's Issues

error in IE

my app won't render in IE, upon checking in Console, my build throws an error

Duplicate label when using setEmpty()

When I set an initial empty value like so:

  const countries = useMemo(() => {
    return countryList().setEmpty("Select a Country").getData();
  }, []);

I end up with a duplicate empty value
Screenshot 2022-01-12 at 17 39 41

Am I using the .setEmpty() method wrong?

I messed around with the setEmpty() method and saw that changing to a .concat method instead of .unshift fixed the problem for me. Not sure why. Want me to open a PR or can you think of a cleaner way to do it?

  setEmpty(label) {
    this.data = [{
      value: '',
      label: label,
    }].concat(this.data)
    this.valueMap[''] = label
    this.labelMap[label] = ''

    return this
  }

FYI: Many states of some countries show wrong information

{name: 'Autonomous City Of Buenos Aires', isoCode: 'C', ...}
{name: 'Buenos Aires Province', isoCode: 'B', ... }

Belgrano is part of City of Buenos Aires (C)

countryCode: "AR"
latitude: "-34.56270000"
longitude: "-58.45829000"
name: "Belgrano"
stateCode: "B"

This issue is also in USA / Aruba /

Feature request

Firstly, great work!
Would adding an additional property region (e.g Europe, North-America) to pre-filter countries in the drop down, be an option?
Use cases don't always require every country in the world. :-)

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.