react-all-countries 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +84 -0
- package/data/countries.json +1780 -0
- package/index.d.ts +18 -0
- package/index.js +66 -0
- package/package.json +41 -0
package/README.md
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# react-all-countries
|
|
2
|
+
|
|
3
|
+
All countries data — name, ISO short code, phone prefix, timezone, and flag.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install react-all-countries
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
const countries = require("react-all-countries");
|
|
15
|
+
|
|
16
|
+
const {
|
|
17
|
+
getCountryByCode,
|
|
18
|
+
getCountryByName,
|
|
19
|
+
getCountriesByCallingCode,
|
|
20
|
+
searchCountries,
|
|
21
|
+
} = require("react-all-countries");
|
|
22
|
+
|
|
23
|
+
console.log(countries.length); // 254
|
|
24
|
+
|
|
25
|
+
const bd = getCountryByCode("BD");
|
|
26
|
+
// {
|
|
27
|
+
// name: 'Bangladesh',
|
|
28
|
+
// shortName: 'BD',
|
|
29
|
+
// phonePrefix: '+880',
|
|
30
|
+
// timezones: 'UTC+06:00',
|
|
31
|
+
// flag: 'https://flags.restcountries.com/v5/svg/bd.svg'
|
|
32
|
+
// }
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### React example
|
|
36
|
+
|
|
37
|
+
```jsx
|
|
38
|
+
import countries, { getCountryByCode } from "react-all-countries";
|
|
39
|
+
|
|
40
|
+
function CountrySelect() {
|
|
41
|
+
return (
|
|
42
|
+
<select>
|
|
43
|
+
{countries.map((c) => (
|
|
44
|
+
<option key={c.shortName || c.name} value={c.shortName}>
|
|
45
|
+
{c.name} ({c.phonePrefix})
|
|
46
|
+
</option>
|
|
47
|
+
))}
|
|
48
|
+
</select>
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function Flag({ code }) {
|
|
53
|
+
const country = getCountryByCode(code);
|
|
54
|
+
if (!country?.flag) return null;
|
|
55
|
+
return <img src={country.flag} alt={country.name} width={24} />;
|
|
56
|
+
}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Data shape
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
interface Country {
|
|
63
|
+
name: string; // common name
|
|
64
|
+
shortName: string; // ISO alpha-2
|
|
65
|
+
phonePrefix: string; // e.g. '+880'
|
|
66
|
+
timezones: string; // first timezone
|
|
67
|
+
flag: string; // SVG flag URL
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## API
|
|
72
|
+
|
|
73
|
+
| Export | Description |
|
|
74
|
+
|--------|-------------|
|
|
75
|
+
| `countries` / default | Full country array |
|
|
76
|
+
| `getCountries()` | Same as `countries` |
|
|
77
|
+
| `getCountryByCode(code)` | Find by `shortName` |
|
|
78
|
+
| `getCountryByName(name)` | Find by exact `name` |
|
|
79
|
+
| `getCountriesByCallingCode(code)` | Filter by `phonePrefix` (`880` or `+880`) |
|
|
80
|
+
| `searchCountries(query)` | Partial name search |
|
|
81
|
+
|
|
82
|
+
## License
|
|
83
|
+
|
|
84
|
+
MIT
|