From 0ca26490407c3623443cd6eb9e720d9e95b5e5f1 Mon Sep 17 00:00:00 2001 From: Tiago Ribeiro Date: Tue, 19 Sep 2023 21:58:03 +0100 Subject: [PATCH] Improved the Country selector --- package.json | 1 + .../DemographicInformationInput.tsx | 18 +---- src/components/Low/CountrySelect.tsx | 73 +++++++++++++++++++ src/pages/profile.tsx | 19 +---- yarn.lock | 5 ++ 5 files changed, 84 insertions(+), 32 deletions(-) create mode 100644 src/components/Low/CountrySelect.tsx diff --git a/package.json b/package.json index 2d65e5ba..3bdfcf9a 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "axios": "^1.3.5", "chart.js": "^4.2.1", "clsx": "^1.2.1", + "countries-list": "^3.0.1", "country-codes-list": "^1.6.11", "daisyui": "^3.1.5", "eslint": "8.33.0", diff --git a/src/components/DemographicInformationInput.tsx b/src/components/DemographicInformationInput.tsx index 64c3087e..acb26176 100644 --- a/src/components/DemographicInformationInput.tsx +++ b/src/components/DemographicInformationInput.tsx @@ -9,6 +9,7 @@ import {BsArrowRepeat} from "react-icons/bs"; import axios from "axios"; import {toast} from "react-toastify"; import {KeyedMutator} from "swr"; +import CountrySelect from "./Low/CountrySelect"; interface Props { mutateUser: KeyedMutator; @@ -54,22 +55,7 @@ export default function DemographicInformationInput({mutateUser}: Props) {
- +
setPhone(e)} placeholder="Enter phone number" required />
diff --git a/src/components/Low/CountrySelect.tsx b/src/components/Low/CountrySelect.tsx new file mode 100644 index 00000000..6dace578 --- /dev/null +++ b/src/components/Low/CountrySelect.tsx @@ -0,0 +1,73 @@ +import {countries, TCountries} from "countries-list"; +import {Fragment, useState} from "react"; +import {Combobox, Transition} from "@headlessui/react"; +import {BsChevronExpand} from "react-icons/bs"; + +interface Props { + value?: string; + onChange: (value: string) => void; +} + +const mapCountries = (codes: string[]) => { + return codes.map((code) => ({ + label: countries[code as unknown as keyof TCountries].name, + code, + })); +}; + +export default function CountrySelect({value, onChange}: Props) { + const [query, setQuery] = useState(""); + + const filteredCountries = + query === "" + ? mapCountries(Object.keys(countries)) + : mapCountries( + Object.keys(countries).filter((x) => + countries[x as unknown as keyof TCountries].name.toLowerCase().includes(query.toLowerCase()), + ), + ); + + return ( + <> + +
+
+ setQuery(e.target.value)} + displayValue={(code: string) => countries[code as unknown as keyof TCountries].name} + /> + + + +
+ setQuery("")}> + + {filteredCountries.length === 0 && query !== "" ? ( +
Nothing found.
+ ) : ( + filteredCountries.map((country) => ( + + `relative cursor-default select-none py-2 pl-10 pr-4 ${ + active ? "bg-mti-purple-light text-white" : "text-gray-900" + }` + }> + {country.label} + + )) + )} +
+
+
+
+ + ); +} diff --git a/src/pages/profile.tsx b/src/pages/profile.tsx index b60aaff7..5948607f 100644 --- a/src/pages/profile.tsx +++ b/src/pages/profile.tsx @@ -15,6 +15,8 @@ import {RadioGroup} from "@headlessui/react"; import clsx from "clsx"; import {EmploymentStatus, EMPLOYMENT_STATUS, Gender, User} from "@/interfaces/user"; import countryCodes from "country-codes-list"; +import {countries, TCountries} from "countries-list"; +import CountrySelect from "@/components/Low/CountrySelect"; export const getServerSideProps = withIronSessionSsr(({req, res}) => { const user = req.session.user; @@ -186,22 +188,7 @@ export default function Home() {
- +