Country Selector Extension
A Directus interface extension that provides a comprehensive country selection dropdown with support for country codes, names, and descriptions.
Features
- Comprehensive Country List: Includes all countries with ISO codes and names
- Search Functionality: Easy search through countries by name
- Custom Labels: Configurable field labels and descriptions
- Required Field Support: Optional required field validation
- JSON Storage: Stores country data in a structured JSON format
- Description Support: Additional description field for each country selection
- Directus UI Integration: Uses Directus theme variables for consistent styling
- TypeScript Support: Full TypeScript support with type definitions
Installation
From NPM (Recommended)
npm install directus-extension-country-selector
Manual Installation
- Download or clone this repository
- Install dependencies with
npm install
- Build the extension with
npm run build
- Move the built extension to your Directus extensions folder
Configuration
The interface has the following configuration options:
Field Label
Customize the label that appears above the country selector field.
Description
Add helper text that appears below the field to guide users.
Required
Mark the field as required for validation purposes.
Usage
-
Field Setup:
- Create a field with type
string
- Select “Country Selector” as the interface
- Configure the field label, description, and required status
- Create a field with type
-
Data Storage: The extension stores data in the following JSON format:
{ "countryCode": "US", "defaultLocale": "us", "description": "United States of America" }
-
Display Options:
- Raw: Shows the JSON data as stored
- Formatted: Shows the country name in a readable format
Example Configuration
// Interface configuration
{
label: "Select Country",
description: "Choose the country for this record",
required: true
}
Data Format
Input Format
The extension accepts various input formats:
- JSON Object:
{"countryCode": "US", "defaultLocale": "us", "description": "United States"}
- Country Code:
"US"
- Country Name:
"United States"
Output Format
The extension always outputs data in this structured format:
{
"countryCode": "US",
"defaultLocale": "us",
"description": "United States of America"
}
Country Data
The extension uses the countries-list
package which provides:
- 250+ Countries: Complete list of world countries
- ISO Codes: Standard ISO 3166-1 alpha-2 country codes
- Country Names: Official country names in English
- Consistent Format: Standardized data structure
Example Countries
- πΊπΈ United States (US)
- π¬π§ United Kingdom (GB)
- π©πͺ Germany (DE)
- π«π· France (FR)
- π―π΅ Japan (JP)
- π¨π¦ Canada (CA)
- π¦πΊ Australia (AU)
Integration Examples
Basic Usage
// Simple country selection
const countryData = {
countryCode: "US",
defaultLocale: "us",
description: "United States of America"
};
With Validation
// Required field validation
if (!countryData.countryCode) {
throw new Error("Country is required");
}
Display Integration
// Display the country name
const countryName = countriesList[countryData.countryCode]?.name || countryData.countryCode;
Customization
Styling
The extension uses Directus theme variables for consistent styling:
.country-selector {
color: var(--theme-foreground);
font-size: var(--theme-font-size-base);
}
.country-dropdown {
background: var(--theme-background-subdued);
border: var(--theme-border-width) solid var(--theme-border-color);
}
Custom Country List
You can extend the country list by modifying the component:
// Add custom countries
const customCountries = [
{ text: "Custom Country", value: "CC" },
...countries.value
];
Compatibility
- Directus 10.x and later
- Field types: string
- Node.js >= 16.0.0
Development
Project Setup
The project requires @directus/extensions-sdk
to be installed as a development dependency:
npm install --save-dev @directus/extensions-sdk
Build Commands
# Build for production
npm run build
# Build for development (watch mode)
npm run dev
# Prepare for publishing
npm run prepublishOnly
Project Structure
src/
βββ interface.vue # Main interface component
βββ index.ts # Extension entry point
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
License
MIT License - see LICENSE file for details.