Pick - List

Write less - pick more 😉

A lightweight, customizable HTML web component for creating drop-down lists that are searchable by keywords of your choice.

ðŸ“Ķ Zero Dependencies

Pure vanilla JavaScript implementation with no external dependencies

ðŸŠķ Lightweight

Minimal footprint with only essential features

ðŸ“ą Responsive

Works seamlessly across all device sizes and works in all browsers

Get Started

Quick Start

Get up and running with pick-list in minutes. Here's a complete working example:

<!DOCTYPE html>
<html>
<head>
    <title>Pick-list Quick Start</title>
    <script src="https://cdn.jsdelivr.net/gh/GiladMeirson/picklist.js@main/pickList.min.js"></script>
</head>
<body>
    <h3>Select a fruit by keywords:</h3>
    <pick-list id="FruitPickList" placeholder="Choose a fruit..."></pick-list>

    <script>
        const picker = document.getElementById('FruitPickList');
        picker.setItems([
        { value: '🍎', keywords: ['apple', 'red', 'fruit', 'sweet'] },
        { value: '🍏', keywords: ['apple', 'green', 'fruit', 'sour'] },
        { value: '🍌', keywords: ['banana', 'yellow', 'fruit', 'tropical'] },
        { value: 'ðŸĨ­', keywords: ['mango', 'orange', 'fruit', 'tropical'] },
        { value: '🍉', keywords: ['watermelon', 'red', 'fruit', 'summer'] },
        { value: '🍇', keywords: ['grapes', 'purple', 'fruit', 'wine'] }
        ]);

        picker.addEventListener('picklist-select', (e) => {
            alert(`Selected: ${e.detail.value}`);
        });
    </script>
</body>
</html>

Result:

Select a Country:

Try searching for keywords like "watermelon", "red", "tropical", etc.

Selected Value: none

Installation

Install pick-list via NPM or include it directly in your HTML. Follow the guide below to get started:

1. Install via NPM
npm install picklist.js

After installing, import or require the library in your project:

// Using ES6 import
import picklist from 'picklist.js';
// or using CommonJS require
const Picklist = require('picklist.js');
2. Include via <script> Tag

Alternatively, use a CDN to include the library in your HTML:

<script src="https://cdn.jsdelivr.net/gh/GiladMeirson/picklist.js@main/pickList.min.js"></script>

If you are using plain HTML and vanilla JS, use this link in the head or at the end of the body.

Usage

Using the pick-list component is straightforward:

1. Import the Web Component
<script src="https://cdn.jsdelivr.net/gh/GiladMeirson/picklist.js@main/pickList.min.js"></script>
2. Use the custom element in your HTML
<pick-list placeholder="Select a fruit..."></pick-list>
or you can also use it like this:
<pick-list placeholder="Select a fruit..." />
3. Initialize with items in JavaScript
const picklist = document.querySelector('pick-list');
picklist.setItems([
  { value: 'Apple', keywords: ['fruit', 'red', 'sweet'] },
  { value: 'Banana', keywords: ['fruit', 'yellow', 'tropical'] },
  { value: 'Orange', keywords: ['fruit', 'citrus', 'vitamin c'] }
]);

API Reference

The pick-list component provides a comprehensive API for managing picklist functionality. Here are all available properties, methods and events:

Properties
Methods
Events
Data Structure

Items can be passed in two formats:

// Option 1: Object with value and keywords
const items = [
    { value: 'Apple', keywords: ['fruit', 'red', 'sweet'] }
];

// Option 2: Simple string (keyword will be the lowercase value)
const items = ['Apple', 'Banana', 'Orange'];

Events

The picklist library emits custom events to help you hook into user interactions. You can listen for these events on the picklist element:

Below is an example of attaching event listeners to capture these events:

<script>
  const picklistElement = document.getElementById('myPicklist');
  // Listen for selection events
  picklistElement.addEventListener('picklist-select', function(event) {
    console.log('Selected item:', event.detail.value);
    // You can also update your UI or perform actions based on the selection
  });
  // Listen for input (search) events
  picklistElement.addEventListener('picklist-input', function(event) {
    console.log('Current search text:', event.detail.search);
    // This event can be used to implement dynamic suggestions or other interactions
  });
</script>

By leveraging these events, you can integrate custom logic whenever a user selects an item or types in the picklist's search field. This makes the component highly extensible and adaptable to various use cases.

Live Demo

Try out the picklist component with keyword search functionality:

Selected: none

Search Keywords: none

Demo Data Structure:
    const fruits = [
{ value: "Apple", keywords: ["fruit", "red", "sweet", "crisp"] },
{ value: "Banana", keywords: ["fruit", "yellow", "tropical", "potassium"] },
{ value: "Orange", keywords: ["fruit", "citrus", "vitamin c", "juice"] },
{ value: "Grape", keywords: ["fruit", "purple", "wine", "antioxidants"] },
{ value: "Mango", keywords: ["fruit", "tropical", "sweet", "yellow"] },
{ value: "Pineapple", keywords: ["fruit", "tropical", "sweet", "tart"] },
{ value: "Strawberry", keywords: ["fruit", "red", "sweet", "berries"] },
{ value: "Blueberry", keywords: ["fruit", "blue", "antioxidants", "berries"] }
            ];
  // ... more fruits
];

Download

You can obtain picklist from the following sources:

Once downloaded or installed, you can include the library in your project and follow the Usage guide to integrate it.