react-country-choices 0.2.1
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/dist/index.css +65 -0
- package/dist/index.d.mts +51 -0
- package/dist/index.d.ts +51 -0
- package/dist/index.js +269 -0
- package/dist/index.mjs +237 -0
- package/package.json +38 -0
package/dist/index.css
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/* src/components/styles/Checkbox.css */
|
|
2
|
+
.checkbox-group-container {
|
|
3
|
+
display: flex;
|
|
4
|
+
flex-direction: column;
|
|
5
|
+
gap: 10px;
|
|
6
|
+
max-height: 300px;
|
|
7
|
+
overflow-y: auto;
|
|
8
|
+
padding: 10px;
|
|
9
|
+
border: 1px solid #d4d4d4;
|
|
10
|
+
border-radius: 8px;
|
|
11
|
+
background-color: #fefefe;
|
|
12
|
+
font-family: Arial, sans-serif;
|
|
13
|
+
}
|
|
14
|
+
.checkbox-country-item {
|
|
15
|
+
display: flex;
|
|
16
|
+
align-items: center;
|
|
17
|
+
gap: 10px;
|
|
18
|
+
padding: 6px 10px;
|
|
19
|
+
border-radius: 5px;
|
|
20
|
+
transition: background 0.2s ease;
|
|
21
|
+
cursor: pointer;
|
|
22
|
+
}
|
|
23
|
+
.checkbox-country-item:hover {
|
|
24
|
+
background-color: #f0f0f0;
|
|
25
|
+
}
|
|
26
|
+
.checkbox-country-item input[type=checkbox] {
|
|
27
|
+
transform: scale(1.2);
|
|
28
|
+
cursor: pointer;
|
|
29
|
+
}
|
|
30
|
+
.checkbox-country-flag {
|
|
31
|
+
border-radius: 3px;
|
|
32
|
+
box-shadow: 0 0 2px rgba(0, 0, 0, 0.2);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/* src/components/styles/Select.css */
|
|
36
|
+
.country-option {
|
|
37
|
+
display: flex;
|
|
38
|
+
padding: 1rem;
|
|
39
|
+
}
|
|
40
|
+
.country-overall {
|
|
41
|
+
margin-top: 1rem;
|
|
42
|
+
}
|
|
43
|
+
.country-option:hover,
|
|
44
|
+
.isSelected {
|
|
45
|
+
background-color: rgba(0, 200, 222, 0.51);
|
|
46
|
+
}
|
|
47
|
+
.country-option span {
|
|
48
|
+
margin-left: 0.5rem;
|
|
49
|
+
}
|
|
50
|
+
.container-textField {
|
|
51
|
+
position: relative;
|
|
52
|
+
}
|
|
53
|
+
.selected-country-choice {
|
|
54
|
+
position: absolute;
|
|
55
|
+
height: 50%;
|
|
56
|
+
left: 1%;
|
|
57
|
+
top: 22%;
|
|
58
|
+
}
|
|
59
|
+
.selected-country-choice img {
|
|
60
|
+
object-fit: contain;
|
|
61
|
+
height: 100%;
|
|
62
|
+
}
|
|
63
|
+
#country-select {
|
|
64
|
+
padding-left: 70px;
|
|
65
|
+
}
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import React__default, { ReactNode } from 'react';
|
|
3
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
4
|
+
|
|
5
|
+
type CountryCheckboxProps = {
|
|
6
|
+
translateTo?: string;
|
|
7
|
+
flags?: boolean;
|
|
8
|
+
desc?: boolean;
|
|
9
|
+
onChangeCountry: (countries: string[]) => void;
|
|
10
|
+
defaultSelected?: string[];
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
type CountrySelectProps = {
|
|
14
|
+
translateTo?: string;
|
|
15
|
+
flags?: boolean;
|
|
16
|
+
desc?: boolean;
|
|
17
|
+
onChangeCountry: (countryName: string) => void;
|
|
18
|
+
label?: string;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
declare const Country: {
|
|
22
|
+
Select: React.FC<CountrySelectProps>;
|
|
23
|
+
Checkbox: React.FC<CountryCheckboxProps>;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/** === Types === */
|
|
27
|
+
type CountryParameters = {
|
|
28
|
+
translateTo: string;
|
|
29
|
+
flags: boolean | null;
|
|
30
|
+
desc: boolean | null;
|
|
31
|
+
};
|
|
32
|
+
type CountryOption$1 = {
|
|
33
|
+
name: string;
|
|
34
|
+
flag: string;
|
|
35
|
+
};
|
|
36
|
+
type CountryContextValue = {
|
|
37
|
+
parameters: CountryParameters;
|
|
38
|
+
setParameters: React__default.Dispatch<React__default.SetStateAction<CountryParameters>>;
|
|
39
|
+
newCountries: CountryOption$1[];
|
|
40
|
+
};
|
|
41
|
+
declare const useCountryContext: () => CountryContextValue;
|
|
42
|
+
declare const CountryContextProvider: ({ children }: {
|
|
43
|
+
children: ReactNode;
|
|
44
|
+
}) => react_jsx_runtime.JSX.Element;
|
|
45
|
+
|
|
46
|
+
type CountryOption = {
|
|
47
|
+
name: string;
|
|
48
|
+
flag: string;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export { CountryContextProvider, type CountryOption, Country as default, useCountryContext };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import React__default, { ReactNode } from 'react';
|
|
3
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
4
|
+
|
|
5
|
+
type CountryCheckboxProps = {
|
|
6
|
+
translateTo?: string;
|
|
7
|
+
flags?: boolean;
|
|
8
|
+
desc?: boolean;
|
|
9
|
+
onChangeCountry: (countries: string[]) => void;
|
|
10
|
+
defaultSelected?: string[];
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
type CountrySelectProps = {
|
|
14
|
+
translateTo?: string;
|
|
15
|
+
flags?: boolean;
|
|
16
|
+
desc?: boolean;
|
|
17
|
+
onChangeCountry: (countryName: string) => void;
|
|
18
|
+
label?: string;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
declare const Country: {
|
|
22
|
+
Select: React.FC<CountrySelectProps>;
|
|
23
|
+
Checkbox: React.FC<CountryCheckboxProps>;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/** === Types === */
|
|
27
|
+
type CountryParameters = {
|
|
28
|
+
translateTo: string;
|
|
29
|
+
flags: boolean | null;
|
|
30
|
+
desc: boolean | null;
|
|
31
|
+
};
|
|
32
|
+
type CountryOption$1 = {
|
|
33
|
+
name: string;
|
|
34
|
+
flag: string;
|
|
35
|
+
};
|
|
36
|
+
type CountryContextValue = {
|
|
37
|
+
parameters: CountryParameters;
|
|
38
|
+
setParameters: React__default.Dispatch<React__default.SetStateAction<CountryParameters>>;
|
|
39
|
+
newCountries: CountryOption$1[];
|
|
40
|
+
};
|
|
41
|
+
declare const useCountryContext: () => CountryContextValue;
|
|
42
|
+
declare const CountryContextProvider: ({ children }: {
|
|
43
|
+
children: ReactNode;
|
|
44
|
+
}) => react_jsx_runtime.JSX.Element;
|
|
45
|
+
|
|
46
|
+
type CountryOption = {
|
|
47
|
+
name: string;
|
|
48
|
+
flag: string;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export { CountryContextProvider, type CountryOption, Country as default, useCountryContext };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
CountryContextProvider: () => CountryContextProvider,
|
|
34
|
+
default: () => index_default,
|
|
35
|
+
useCountryContext: () => useCountryContext
|
|
36
|
+
});
|
|
37
|
+
module.exports = __toCommonJS(index_exports);
|
|
38
|
+
|
|
39
|
+
// src/components/imports/Checkbox.tsx
|
|
40
|
+
var import_react2 = require("react");
|
|
41
|
+
|
|
42
|
+
// src/components/context/CountryContext.tsx
|
|
43
|
+
var import_axios = __toESM(require("axios"));
|
|
44
|
+
var import_react = require("react");
|
|
45
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
46
|
+
var CountryContext = (0, import_react.createContext)(void 0);
|
|
47
|
+
var useCountryContext = () => {
|
|
48
|
+
const ctx = (0, import_react.useContext)(CountryContext);
|
|
49
|
+
if (!ctx) {
|
|
50
|
+
throw new Error("useCountryContext must be used within a CountryContextProvider");
|
|
51
|
+
}
|
|
52
|
+
return ctx;
|
|
53
|
+
};
|
|
54
|
+
var CountryContextProvider = ({ children }) => {
|
|
55
|
+
const [parameters, setParameters] = (0, import_react.useState)({
|
|
56
|
+
translateTo: "",
|
|
57
|
+
flags: null,
|
|
58
|
+
desc: null
|
|
59
|
+
});
|
|
60
|
+
const [countries, setCountries] = (0, import_react.useState)([]);
|
|
61
|
+
const [newCountries, setNewCountries] = (0, import_react.useState)([]);
|
|
62
|
+
const formatCountries = (0, import_react.useMemo)(
|
|
63
|
+
() => ({
|
|
64
|
+
name: "",
|
|
65
|
+
flag: ""
|
|
66
|
+
}),
|
|
67
|
+
[]
|
|
68
|
+
);
|
|
69
|
+
(0, import_react.useEffect)(() => {
|
|
70
|
+
const getAllCountries = async () => {
|
|
71
|
+
var _a;
|
|
72
|
+
try {
|
|
73
|
+
const response = await import_axios.default.get(
|
|
74
|
+
"https://restcountries.com/v3.1/all?fields=name,flags,translations"
|
|
75
|
+
);
|
|
76
|
+
setCountries((_a = response.data) != null ? _a : []);
|
|
77
|
+
} catch (error) {
|
|
78
|
+
console.log(error);
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
getAllCountries();
|
|
82
|
+
}, []);
|
|
83
|
+
(0, import_react.useEffect)(() => {
|
|
84
|
+
const safeLower = (v) => String(v != null ? v : "").toLowerCase();
|
|
85
|
+
const getTranslatedName = (c, code) => {
|
|
86
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
87
|
+
if (!code || code === "eng") return (_b = (_a = c.name) == null ? void 0 : _a.common) != null ? _b : "";
|
|
88
|
+
if (code === "") return (_d = (_c = c.name) == null ? void 0 : _c.common) != null ? _d : "";
|
|
89
|
+
return (_i = (_h = (_f = (_e = c.translations) == null ? void 0 : _e[code]) == null ? void 0 : _f.common) != null ? _h : (_g = c.name) == null ? void 0 : _g.common) != null ? _i : "";
|
|
90
|
+
};
|
|
91
|
+
const treatCountries = () => {
|
|
92
|
+
const code = parameters.translateTo;
|
|
93
|
+
const sorted = [...countries].sort((a, b) => {
|
|
94
|
+
const an = getTranslatedName(a, code);
|
|
95
|
+
const bn = getTranslatedName(b, code);
|
|
96
|
+
return safeLower(an).localeCompare(safeLower(bn));
|
|
97
|
+
});
|
|
98
|
+
setNewCountries(
|
|
99
|
+
sorted.map((country) => {
|
|
100
|
+
var _a, _b;
|
|
101
|
+
return {
|
|
102
|
+
...formatCountries,
|
|
103
|
+
name: getTranslatedName(country, code),
|
|
104
|
+
flag: (_b = (_a = country.flags) == null ? void 0 : _a.png) != null ? _b : ""
|
|
105
|
+
};
|
|
106
|
+
})
|
|
107
|
+
);
|
|
108
|
+
};
|
|
109
|
+
treatCountries();
|
|
110
|
+
}, [countries, parameters, formatCountries]);
|
|
111
|
+
(0, import_react.useEffect)(() => {
|
|
112
|
+
const handleOrder = () => {
|
|
113
|
+
if (parameters.desc === true) {
|
|
114
|
+
const sortedDesc = [...newCountries].sort(
|
|
115
|
+
(a, b) => {
|
|
116
|
+
var _a, _b;
|
|
117
|
+
return String((_a = b.name) != null ? _a : "").toLowerCase().localeCompare(String((_b = a.name) != null ? _b : "").toLowerCase());
|
|
118
|
+
}
|
|
119
|
+
);
|
|
120
|
+
setNewCountries(sortedDesc);
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
handleOrder();
|
|
124
|
+
}, [parameters.desc]);
|
|
125
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(CountryContext.Provider, { value: { parameters, setParameters, newCountries }, children });
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
// src/components/imports/Checkbox.tsx
|
|
129
|
+
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
130
|
+
var Checkbox = ({
|
|
131
|
+
translateTo = "",
|
|
132
|
+
flags = false,
|
|
133
|
+
desc = null,
|
|
134
|
+
onChangeCountry,
|
|
135
|
+
defaultSelected = []
|
|
136
|
+
}) => {
|
|
137
|
+
const { newCountries, setParameters } = useCountryContext();
|
|
138
|
+
const [selectedCountries, setSelectedCountries] = (0, import_react2.useState)(
|
|
139
|
+
Array.isArray(defaultSelected) ? defaultSelected : []
|
|
140
|
+
);
|
|
141
|
+
(0, import_react2.useEffect)(() => {
|
|
142
|
+
setParameters((state) => ({
|
|
143
|
+
...state,
|
|
144
|
+
translateTo,
|
|
145
|
+
flags,
|
|
146
|
+
desc
|
|
147
|
+
}));
|
|
148
|
+
}, [translateTo, flags, desc, setParameters]);
|
|
149
|
+
const selectedSet = (0, import_react2.useMemo)(() => new Set(selectedCountries), [selectedCountries]);
|
|
150
|
+
const handleCheckboxChange = (countryName) => {
|
|
151
|
+
setSelectedCountries((prev) => {
|
|
152
|
+
const exists = prev.includes(countryName);
|
|
153
|
+
const updated = exists ? prev.filter((n) => n !== countryName) : [...prev, countryName];
|
|
154
|
+
onChangeCountry(updated);
|
|
155
|
+
return updated;
|
|
156
|
+
});
|
|
157
|
+
};
|
|
158
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { className: "checkbox-group-container", children: newCountries.map((country) => /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("label", { className: "checkbox-country-item", children: [
|
|
159
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
160
|
+
"input",
|
|
161
|
+
{
|
|
162
|
+
type: "checkbox",
|
|
163
|
+
value: country.name,
|
|
164
|
+
checked: selectedSet.has(country.name),
|
|
165
|
+
onChange: () => handleCheckboxChange(country.name)
|
|
166
|
+
}
|
|
167
|
+
),
|
|
168
|
+
flags && country.flag ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
169
|
+
"img",
|
|
170
|
+
{
|
|
171
|
+
src: country.flag,
|
|
172
|
+
alt: `${country.name} flag`,
|
|
173
|
+
height: 20,
|
|
174
|
+
className: "checkbox-country-flag"
|
|
175
|
+
}
|
|
176
|
+
) : null,
|
|
177
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { children: country.name })
|
|
178
|
+
] }, country.name)) });
|
|
179
|
+
};
|
|
180
|
+
var Checkbox_default = Checkbox;
|
|
181
|
+
|
|
182
|
+
// src/components/imports/Select.tsx
|
|
183
|
+
var import_react3 = require("react");
|
|
184
|
+
var import_material = require("@mui/material");
|
|
185
|
+
var import_jsx_runtime3 = require("react/jsx-runtime");
|
|
186
|
+
var Select = ({
|
|
187
|
+
translateTo = "",
|
|
188
|
+
flags = false,
|
|
189
|
+
desc = null,
|
|
190
|
+
onChangeCountry,
|
|
191
|
+
label = "Select..."
|
|
192
|
+
}) => {
|
|
193
|
+
const [selectedOption, setSelectedOption] = (0, import_react3.useState)({
|
|
194
|
+
name: "",
|
|
195
|
+
flag: ""
|
|
196
|
+
});
|
|
197
|
+
const { setParameters, newCountries } = useCountryContext();
|
|
198
|
+
(0, import_react3.useEffect)(() => {
|
|
199
|
+
setSelectedOption(null);
|
|
200
|
+
setParameters((state) => ({
|
|
201
|
+
...state,
|
|
202
|
+
translateTo,
|
|
203
|
+
flags,
|
|
204
|
+
desc
|
|
205
|
+
}));
|
|
206
|
+
}, [translateTo, flags, desc, setParameters]);
|
|
207
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
208
|
+
import_material.Autocomplete,
|
|
209
|
+
{
|
|
210
|
+
id: "country-select",
|
|
211
|
+
className: "country-overall",
|
|
212
|
+
options: newCountries,
|
|
213
|
+
value: selectedOption,
|
|
214
|
+
onChange: (_event, value) => {
|
|
215
|
+
setSelectedOption(value);
|
|
216
|
+
if (value == null ? void 0 : value.name) onChangeCountry(value.name);
|
|
217
|
+
},
|
|
218
|
+
getOptionLabel: (option) => {
|
|
219
|
+
var _a;
|
|
220
|
+
return (_a = option == null ? void 0 : option.name) != null ? _a : "";
|
|
221
|
+
},
|
|
222
|
+
isOptionEqualToValue: (option, value) => option.name === value.name,
|
|
223
|
+
renderOption: (optionProps, option) => {
|
|
224
|
+
var _a;
|
|
225
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("li", { ...optionProps, className: "country-option", children: [
|
|
226
|
+
flags ? /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
227
|
+
"img",
|
|
228
|
+
{
|
|
229
|
+
height: 20,
|
|
230
|
+
src: (_a = option.flag) != null ? _a : "",
|
|
231
|
+
alt: `${option.name} flag`
|
|
232
|
+
}
|
|
233
|
+
) : null,
|
|
234
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { children: option.name })
|
|
235
|
+
] });
|
|
236
|
+
},
|
|
237
|
+
renderInput: (params) => {
|
|
238
|
+
var _a;
|
|
239
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { className: "container-textField", children: [
|
|
240
|
+
flags ? /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
241
|
+
"img",
|
|
242
|
+
{
|
|
243
|
+
className: "selected-country-choice",
|
|
244
|
+
src: (_a = selectedOption == null ? void 0 : selectedOption.flag) != null ? _a : "",
|
|
245
|
+
alt: ""
|
|
246
|
+
}
|
|
247
|
+
) : null,
|
|
248
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_material.TextField, { ...params, label })
|
|
249
|
+
] });
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
);
|
|
253
|
+
};
|
|
254
|
+
var Select_default = Select;
|
|
255
|
+
|
|
256
|
+
// src/Country.ts
|
|
257
|
+
var Country = {
|
|
258
|
+
"Select": Select_default,
|
|
259
|
+
"Checkbox": Checkbox_default
|
|
260
|
+
};
|
|
261
|
+
var Country_default = Country;
|
|
262
|
+
|
|
263
|
+
// src/index.ts
|
|
264
|
+
var index_default = Country_default;
|
|
265
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
266
|
+
0 && (module.exports = {
|
|
267
|
+
CountryContextProvider,
|
|
268
|
+
useCountryContext
|
|
269
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
// src/components/imports/Checkbox.tsx
|
|
2
|
+
import { useEffect as useEffect2, useMemo as useMemo2, useState as useState2 } from "react";
|
|
3
|
+
|
|
4
|
+
// src/components/context/CountryContext.tsx
|
|
5
|
+
import axios from "axios";
|
|
6
|
+
import {
|
|
7
|
+
createContext,
|
|
8
|
+
useContext,
|
|
9
|
+
useEffect,
|
|
10
|
+
useMemo,
|
|
11
|
+
useState
|
|
12
|
+
} from "react";
|
|
13
|
+
import { jsx } from "react/jsx-runtime";
|
|
14
|
+
var CountryContext = createContext(void 0);
|
|
15
|
+
var useCountryContext = () => {
|
|
16
|
+
const ctx = useContext(CountryContext);
|
|
17
|
+
if (!ctx) {
|
|
18
|
+
throw new Error("useCountryContext must be used within a CountryContextProvider");
|
|
19
|
+
}
|
|
20
|
+
return ctx;
|
|
21
|
+
};
|
|
22
|
+
var CountryContextProvider = ({ children }) => {
|
|
23
|
+
const [parameters, setParameters] = useState({
|
|
24
|
+
translateTo: "",
|
|
25
|
+
flags: null,
|
|
26
|
+
desc: null
|
|
27
|
+
});
|
|
28
|
+
const [countries, setCountries] = useState([]);
|
|
29
|
+
const [newCountries, setNewCountries] = useState([]);
|
|
30
|
+
const formatCountries = useMemo(
|
|
31
|
+
() => ({
|
|
32
|
+
name: "",
|
|
33
|
+
flag: ""
|
|
34
|
+
}),
|
|
35
|
+
[]
|
|
36
|
+
);
|
|
37
|
+
useEffect(() => {
|
|
38
|
+
const getAllCountries = async () => {
|
|
39
|
+
var _a;
|
|
40
|
+
try {
|
|
41
|
+
const response = await axios.get(
|
|
42
|
+
"https://restcountries.com/v3.1/all?fields=name,flags,translations"
|
|
43
|
+
);
|
|
44
|
+
setCountries((_a = response.data) != null ? _a : []);
|
|
45
|
+
} catch (error) {
|
|
46
|
+
console.log(error);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
getAllCountries();
|
|
50
|
+
}, []);
|
|
51
|
+
useEffect(() => {
|
|
52
|
+
const safeLower = (v) => String(v != null ? v : "").toLowerCase();
|
|
53
|
+
const getTranslatedName = (c, code) => {
|
|
54
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
55
|
+
if (!code || code === "eng") return (_b = (_a = c.name) == null ? void 0 : _a.common) != null ? _b : "";
|
|
56
|
+
if (code === "") return (_d = (_c = c.name) == null ? void 0 : _c.common) != null ? _d : "";
|
|
57
|
+
return (_i = (_h = (_f = (_e = c.translations) == null ? void 0 : _e[code]) == null ? void 0 : _f.common) != null ? _h : (_g = c.name) == null ? void 0 : _g.common) != null ? _i : "";
|
|
58
|
+
};
|
|
59
|
+
const treatCountries = () => {
|
|
60
|
+
const code = parameters.translateTo;
|
|
61
|
+
const sorted = [...countries].sort((a, b) => {
|
|
62
|
+
const an = getTranslatedName(a, code);
|
|
63
|
+
const bn = getTranslatedName(b, code);
|
|
64
|
+
return safeLower(an).localeCompare(safeLower(bn));
|
|
65
|
+
});
|
|
66
|
+
setNewCountries(
|
|
67
|
+
sorted.map((country) => {
|
|
68
|
+
var _a, _b;
|
|
69
|
+
return {
|
|
70
|
+
...formatCountries,
|
|
71
|
+
name: getTranslatedName(country, code),
|
|
72
|
+
flag: (_b = (_a = country.flags) == null ? void 0 : _a.png) != null ? _b : ""
|
|
73
|
+
};
|
|
74
|
+
})
|
|
75
|
+
);
|
|
76
|
+
};
|
|
77
|
+
treatCountries();
|
|
78
|
+
}, [countries, parameters, formatCountries]);
|
|
79
|
+
useEffect(() => {
|
|
80
|
+
const handleOrder = () => {
|
|
81
|
+
if (parameters.desc === true) {
|
|
82
|
+
const sortedDesc = [...newCountries].sort(
|
|
83
|
+
(a, b) => {
|
|
84
|
+
var _a, _b;
|
|
85
|
+
return String((_a = b.name) != null ? _a : "").toLowerCase().localeCompare(String((_b = a.name) != null ? _b : "").toLowerCase());
|
|
86
|
+
}
|
|
87
|
+
);
|
|
88
|
+
setNewCountries(sortedDesc);
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
handleOrder();
|
|
92
|
+
}, [parameters.desc]);
|
|
93
|
+
return /* @__PURE__ */ jsx(CountryContext.Provider, { value: { parameters, setParameters, newCountries }, children });
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
// src/components/imports/Checkbox.tsx
|
|
97
|
+
import { jsx as jsx2, jsxs } from "react/jsx-runtime";
|
|
98
|
+
var Checkbox = ({
|
|
99
|
+
translateTo = "",
|
|
100
|
+
flags = false,
|
|
101
|
+
desc = null,
|
|
102
|
+
onChangeCountry,
|
|
103
|
+
defaultSelected = []
|
|
104
|
+
}) => {
|
|
105
|
+
const { newCountries, setParameters } = useCountryContext();
|
|
106
|
+
const [selectedCountries, setSelectedCountries] = useState2(
|
|
107
|
+
Array.isArray(defaultSelected) ? defaultSelected : []
|
|
108
|
+
);
|
|
109
|
+
useEffect2(() => {
|
|
110
|
+
setParameters((state) => ({
|
|
111
|
+
...state,
|
|
112
|
+
translateTo,
|
|
113
|
+
flags,
|
|
114
|
+
desc
|
|
115
|
+
}));
|
|
116
|
+
}, [translateTo, flags, desc, setParameters]);
|
|
117
|
+
const selectedSet = useMemo2(() => new Set(selectedCountries), [selectedCountries]);
|
|
118
|
+
const handleCheckboxChange = (countryName) => {
|
|
119
|
+
setSelectedCountries((prev) => {
|
|
120
|
+
const exists = prev.includes(countryName);
|
|
121
|
+
const updated = exists ? prev.filter((n) => n !== countryName) : [...prev, countryName];
|
|
122
|
+
onChangeCountry(updated);
|
|
123
|
+
return updated;
|
|
124
|
+
});
|
|
125
|
+
};
|
|
126
|
+
return /* @__PURE__ */ jsx2("div", { className: "checkbox-group-container", children: newCountries.map((country) => /* @__PURE__ */ jsxs("label", { className: "checkbox-country-item", children: [
|
|
127
|
+
/* @__PURE__ */ jsx2(
|
|
128
|
+
"input",
|
|
129
|
+
{
|
|
130
|
+
type: "checkbox",
|
|
131
|
+
value: country.name,
|
|
132
|
+
checked: selectedSet.has(country.name),
|
|
133
|
+
onChange: () => handleCheckboxChange(country.name)
|
|
134
|
+
}
|
|
135
|
+
),
|
|
136
|
+
flags && country.flag ? /* @__PURE__ */ jsx2(
|
|
137
|
+
"img",
|
|
138
|
+
{
|
|
139
|
+
src: country.flag,
|
|
140
|
+
alt: `${country.name} flag`,
|
|
141
|
+
height: 20,
|
|
142
|
+
className: "checkbox-country-flag"
|
|
143
|
+
}
|
|
144
|
+
) : null,
|
|
145
|
+
/* @__PURE__ */ jsx2("span", { children: country.name })
|
|
146
|
+
] }, country.name)) });
|
|
147
|
+
};
|
|
148
|
+
var Checkbox_default = Checkbox;
|
|
149
|
+
|
|
150
|
+
// src/components/imports/Select.tsx
|
|
151
|
+
import { useEffect as useEffect3, useState as useState3 } from "react";
|
|
152
|
+
import { Autocomplete, TextField } from "@mui/material";
|
|
153
|
+
import { jsx as jsx3, jsxs as jsxs2 } from "react/jsx-runtime";
|
|
154
|
+
var Select = ({
|
|
155
|
+
translateTo = "",
|
|
156
|
+
flags = false,
|
|
157
|
+
desc = null,
|
|
158
|
+
onChangeCountry,
|
|
159
|
+
label = "Select..."
|
|
160
|
+
}) => {
|
|
161
|
+
const [selectedOption, setSelectedOption] = useState3({
|
|
162
|
+
name: "",
|
|
163
|
+
flag: ""
|
|
164
|
+
});
|
|
165
|
+
const { setParameters, newCountries } = useCountryContext();
|
|
166
|
+
useEffect3(() => {
|
|
167
|
+
setSelectedOption(null);
|
|
168
|
+
setParameters((state) => ({
|
|
169
|
+
...state,
|
|
170
|
+
translateTo,
|
|
171
|
+
flags,
|
|
172
|
+
desc
|
|
173
|
+
}));
|
|
174
|
+
}, [translateTo, flags, desc, setParameters]);
|
|
175
|
+
return /* @__PURE__ */ jsx3(
|
|
176
|
+
Autocomplete,
|
|
177
|
+
{
|
|
178
|
+
id: "country-select",
|
|
179
|
+
className: "country-overall",
|
|
180
|
+
options: newCountries,
|
|
181
|
+
value: selectedOption,
|
|
182
|
+
onChange: (_event, value) => {
|
|
183
|
+
setSelectedOption(value);
|
|
184
|
+
if (value == null ? void 0 : value.name) onChangeCountry(value.name);
|
|
185
|
+
},
|
|
186
|
+
getOptionLabel: (option) => {
|
|
187
|
+
var _a;
|
|
188
|
+
return (_a = option == null ? void 0 : option.name) != null ? _a : "";
|
|
189
|
+
},
|
|
190
|
+
isOptionEqualToValue: (option, value) => option.name === value.name,
|
|
191
|
+
renderOption: (optionProps, option) => {
|
|
192
|
+
var _a;
|
|
193
|
+
return /* @__PURE__ */ jsxs2("li", { ...optionProps, className: "country-option", children: [
|
|
194
|
+
flags ? /* @__PURE__ */ jsx3(
|
|
195
|
+
"img",
|
|
196
|
+
{
|
|
197
|
+
height: 20,
|
|
198
|
+
src: (_a = option.flag) != null ? _a : "",
|
|
199
|
+
alt: `${option.name} flag`
|
|
200
|
+
}
|
|
201
|
+
) : null,
|
|
202
|
+
/* @__PURE__ */ jsx3("span", { children: option.name })
|
|
203
|
+
] });
|
|
204
|
+
},
|
|
205
|
+
renderInput: (params) => {
|
|
206
|
+
var _a;
|
|
207
|
+
return /* @__PURE__ */ jsxs2("div", { className: "container-textField", children: [
|
|
208
|
+
flags ? /* @__PURE__ */ jsx3(
|
|
209
|
+
"img",
|
|
210
|
+
{
|
|
211
|
+
className: "selected-country-choice",
|
|
212
|
+
src: (_a = selectedOption == null ? void 0 : selectedOption.flag) != null ? _a : "",
|
|
213
|
+
alt: ""
|
|
214
|
+
}
|
|
215
|
+
) : null,
|
|
216
|
+
/* @__PURE__ */ jsx3(TextField, { ...params, label })
|
|
217
|
+
] });
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
);
|
|
221
|
+
};
|
|
222
|
+
var Select_default = Select;
|
|
223
|
+
|
|
224
|
+
// src/Country.ts
|
|
225
|
+
var Country = {
|
|
226
|
+
"Select": Select_default,
|
|
227
|
+
"Checkbox": Checkbox_default
|
|
228
|
+
};
|
|
229
|
+
var Country_default = Country;
|
|
230
|
+
|
|
231
|
+
// src/index.ts
|
|
232
|
+
var index_default = Country_default;
|
|
233
|
+
export {
|
|
234
|
+
CountryContextProvider,
|
|
235
|
+
index_default as default,
|
|
236
|
+
useCountryContext
|
|
237
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "react-country-choices",
|
|
3
|
+
"version": "0.2.1",
|
|
4
|
+
"main": "./dist/index.js",
|
|
5
|
+
"module": "./dist/index.mjs",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"files": ["dist"],
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.mjs",
|
|
12
|
+
"require": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "tsup src/index.ts --format esm,cjs --dts --clean --external react --external react-dom --external axios --external @mui/material --external @emotion/react --external @emotion/styled",
|
|
17
|
+
"prepublishOnly": "npm run build"
|
|
18
|
+
},
|
|
19
|
+
"peerDependencies": {
|
|
20
|
+
"react": ">=18",
|
|
21
|
+
"react-dom": ">=18",
|
|
22
|
+
"@mui/material": ">=5",
|
|
23
|
+
"@emotion/react": ">=11",
|
|
24
|
+
"@emotion/styled": ">=11"
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"axios": "^1.7.2"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@mui/material": "^7.3.8",
|
|
31
|
+
"@emotion/react": "^11.14.0",
|
|
32
|
+
"@emotion/styled": "^11.14.1",
|
|
33
|
+
"@types/react": "^19.2.14",
|
|
34
|
+
"@types/react-dom": "^19.2.3",
|
|
35
|
+
"tsup": "^8.5.1",
|
|
36
|
+
"typescript": "^5.9.3"
|
|
37
|
+
}
|
|
38
|
+
}
|