๐๐๐Œ P๐š๐œ๐ค๐š๐ ๐ž ๐Ÿ๐จ๐ซ C๐จ๐ง๐๐ข๐ญ๐ข๐จ๐ง๐š๐ฅ S๐ž๐ฅ๐ž๐œ๐ญ๐ข๐จ๐ง ๐จ๐Ÿ ๐‚๐จ๐ฎ๐ง๐ญ๐ซ๐ฒ-๐๐ซ๐จ๐ฏ๐ข๐ง๐œ๐ž-๐ƒ๐ข๐ฌ๐ญ๐ซ๐ข๐œ๐ญ-๐Œ๐ฎ๐ง๐ข๐œ๐ข๐ฉ๐š๐ฅ๐ข๐ญ๐ฒ/๐•๐ƒ๐‚

Khemraj Shrestha
6 min readApr 15, 2024

Greetings!

Hey developer, welcome! I know youโ€™re seeking a perfect and easy-to-use NPM package for your project that provides a feature for conditional selection options based on Country-Province-District and Municipality/VDC. Right? Well, congratulations, youโ€™re in the perfect place. After reading this blog, Iโ€™m sure youโ€™ll love using this package as I have created this package by fully considering the flexibility, convenience, and ease of use.

Background!

During our minor project in our 3rd year, we created an EMIS software for our college and needed to implement a feature for conditional selection of country-province-district-Municipality/VDC. I spent an entire day searching for the best NPM package, hoping it would save my time. Unfortunately, I couldnโ€™t find one and had to build the logic and feature from scratch, which took me over 2 days to make it function perfectly. After that experience, I wondered why there wasnโ€™t an existing package that provided this feature, and why I shouldnโ€™t create my own. Following the completion of my minor project and my 3rd year, I began developing this package, and now here I am.

Introduction!

Youโ€™re here, which means youโ€™re likely a developer or a tech enthusiast with some understanding of NPM packages. However, for formalityโ€™s sake, let me give a short introduction to โ€œNPM Packageโ€.

Imagine a NPM package as a ready-made recipe for baking cookies. Instead of gathering all the ingredients and figuring out the steps yourself, you can use a recipe that someone else has created. Similarly, a NPM package provides developers with ready-to-use code for their projects, making coding easier.

Now that you know or have a basic understanding of what a NPM package is and how it simplifies coding.

So, whatโ€™s the use of this specific package, and how does it simplify coding?

Great question! If youโ€™re a curious project digger or a software developer, you might encounter situations where you need to capture a userโ€™s country and their complete address, including district/city and more. Until now, you might either enter the userโ€™s entire address manually or create a select field without a comprehensive list. The problem with this approach is that if you enter one user from โ€œProvince 1โ€ and another from โ€œKoshi Provinceโ€, both logically belong to the same province. However, when visualizing data based on provinces, you might get unexpected results, which you donโ€™t want.

To address this issue and provide a comprehensive list of provinces, districts, and Municipality/VDC with uniformity, Iโ€™ve created this NPM package. Unfortunately, as of now, Iโ€™ve only collected data for the country โ€œNepalโ€. However, Iโ€™ll cover other countries in the near future.

Now you understand why and how this package helps developers in creating their projects.

How do I use it, then?

For Installation, type:- npm i country-province-district-munvdc

OR
You can also visit npmjs.com website and search country-province-district-munvdc and follow the procedure.

OR
Follow this link.

After successfully installing the package, the first thing you need to do is check the package.json file. In the dependencies section, you should see โ€œcountry-province-district-munvdcโ€: โ€œ^{version}โ€.

( You might have a different version than this one. )

Now you need to import the necessary components from this package.

import {
CountryList,
ProvinceList,
DistrictList,
MunVdcList
} from "country-province-district-munvdc";

You have the flexibility to import only CountryList, or a combination of CountryList and ProvinceList, or any other combination as per your requirements. However, the essential requirement is that you must import CountryList because as of now Iโ€™ve enabled options for Province, District, and Municipality/VDC only if you choose โ€œNepalโ€ as the country. For other countries, currently, you can only select the country with the other fields disabled.

Now, Inside your main component, type these code snippets.

const [selectedCountry, setSelectedCountry] = useState<string>("");
const [selectedProvince, setSelectedProvince] = useState<string>("");
const [selectedDistrict, setSelectedDistrict] = useState<string>("");
const [selectedMunVdc, setSelectedMunVdc] = useState<string>("");

const [selectedProvinceId, setSelectedProvinceId] = useState<number>(0);

const [selectedDistrictId, setSelectedDistrictId] = useState<number>(0);

The state variables themselves reflect their use cases, but let me also explain them briefly. selectedCountry, selectedProvince, selectedDistrict, and selectedMunVdc are used to receive the values of the selected Country, Province, District, and Municipality/VDC chosen by the user. You can log these values through these state variables. selectedProvinceId is used to send the Province ID to fetch a particular district list based on the province ID, while selectedDistrictId is used to send the District ID to fetch a specific Municipality/VDC list based on the district ID received.

After this, you have to create one function whose task is to console log the selected value.

  const handleSubmit = (selectedCountry: string, selectedProvince:string, selectedDistrict:string, selectedMunVdc:string) => {
console.log({
country: selectedCountry,
province: selectedProvince,
district: selectedDistrict,
munvdc: selectedMunVdc
});
};

This function receives four parameters for this case as I have imported all the four components and it console log the value in key-value pair.

After that, put these code snippets inside the return ( <> โ€ฆ </> ) section.

<CountryList 
onCountrySelect={setSelectedCountry}
/>

<ProvinceList
selectedCountry={selectedCountry}
setSelectedProvinceId={setSelectedProvinceId}
onProvinceSelect={setSelectedProvince}
/>

<DistrictList
selectedProvinceId={selectedProvinceId}
setSelectedDistrictId={setSelectedDistrictId}
onDistrictSelect={setSelectedDistrict}
/>

<MunVdcList
selectedDistrictId={selectedDistrictId}
selectedProvinceId={selectedProvinceId}
onMunVdcSelect={setSelectedMunVdc}
/>

Lastly, you need to create a button that is used to send the selected value in the backend (console in this case).

<button type="submit" onClick={() => handleSubmit(selectedCountry, selectedProvince, selectedDistrict, selectedMunVdc)}>
Submit
</button>

And yes, youโ€™ve successfully implemented the conditional selection functionality in your project. Congratulations!

Hereโ€™s complete code for implementing this feature.

"use client";
import React, { useState } from "react";
import {
CountryList,
ProvinceList,
DistrictList,
MunVdcList
} from "country-province-district-munvdc";

const ConditionalSelect= () => {
const [selectedCountry, setSelectedCountry] = useState<string>("");
const [selectedProvince, setSelectedProvince] = useState<string>("");
const [selectedDistrict, setSelectedDistrict] = useState<string>("");
const [selectedMunVdc, setSelectedMunVdc] = useState<string>("");

const [selectedProvinceId, setSelectedProvinceId] = useState<number>(0);

const [selectedDistrictId, setSelectedDistrictId] = useState<number>(0);

const handleSubmit = (selectedCountry: string, selectedProvince:string, selectedDistrict:string, selectedMunVdc:string) => {
console.log({
country: selectedCountry,
province: selectedProvince,
district: selectedDistrict,
munvdc: selectedMunVdc
});
};

return (
<>
<CountryList onCountrySelect={setSelectedCountry} />

<ProvinceList
selectedCountry={selectedCountry}
setSelectedProvinceId={setSelectedProvinceId}
onProvinceSelect={setSelectedProvince}
/>

<DistrictList
selectedProvinceId={selectedProvinceId}
setSelectedDistrictId={setSelectedDistrictId}
onDistrictSelect={setSelectedDistrict}
/>

<MunVdcList
selectedDistrictId={selectedDistrictId}
selectedProvinceId={selectedProvinceId}
onMunVdcSelect={setSelectedMunVdc}
/>

<button type="submit" onClick={() => handleSubmit(selectedCountry, selectedProvince, selectedDistrict, selectedMunVdc)}>
Submit
</button>
</>
);
};

export default ConditionalSelect;

Let me also provide a bit more clarity on the feature for cases where you only want to implement country and province.

Hereโ€™s the code snippets for this case.

"use client";
import React, { useState } from "react";
import {
CountryList,
ProvinceList
} from "country-province-district-munvdc";

const ConditionalSelect= () => {
const [selectedCountry, setSelectedCountry] = useState<string>("");
const [selectedProvince, setSelectedProvince] = useState<string>("");

const [selectedProvinceId, setSelectedProvinceId] = useState<number>(0);

const handleSubmit = (selectedCountry: string, selectedProvince:string) => {
console.log({
country: selectedCountry,
province: selectedProvince
});
};

return (
<>
<CountryList onCountrySelect={setSelectedCountry} />

<ProvinceList
selectedCountry={selectedCountry}
setSelectedProvinceId={setSelectedProvinceId}
onProvinceSelect={setSelectedProvince}
/>

<button type="submit" onClick={() => handleSubmit(selectedCountry, selectedProvince)}>
Submit
</button>
</>
);
};

export default ConditionalSelect;

Thank you dude, for spending your valuable time on this blog. Thatโ€™s all I have for today. Keep learning and stay motivated!

Writer:- Khemraj Shrestha

For more:- www.khemrajshrestha.com.np

Cover Image

Stackademic ๐ŸŽ“

Thank you for reading until the end. Before you go:

--

--