Tom Select

repository·master·Indexed 24 days ago

https://github.com/orchidjs/tom-select

A lightweight (~16kb gzipped), framework-agnostic, and dynamic UI control for <select> elements. Forked from Selectize.js, it provides an autocomplete widget with native-feeling keyboard navigation, smart searching, and remote data loading, making it suitable for tagging, contact lists, and large datasets.

Tokens
10.6K
Snippets
19
Records
63
Agent score
80%

What's inside tom-select

  1. How to create a Tom Select plugin

    master

    Plugins are implemented as functions that receive plugin_options and are executed within the context of a TomSelect instance (this).

    Structure Requirements

    • Location: Files should live in src/plugins/[plugin_name]/.
    • Naming: Plugin names must follow the format /[a-z_]+$/.
    • Files:
      • plugin.js (Required): Contains the exported function.
      • plugin.scss (Optional): CSS that will be bundled at build time.
    • Registration: Do not call TomSelect.define inside the plugin file itself; call it when importing the plugin.

    Plugin Lifecycle and Hooks

    Plugins are initialized right before the control is setup. To interact with the control's lifecycle, use the following patterns:

    Adding Dependencies

    Use this.require('plugin_name') to ensure other plugins are loaded.

    Method Hooks

    Use this.hook(type, method, callback) to execute code around existing methods.

    • after: Runs after the method.
    • before: Runs before the method.
    • instead: Used to override a method. Note: If the original method returns a value, your overridden function must also return a value.

    DOM Events

    To add event listeners to DOM elements, use the after hook on the setup method to ensure the control elements exist.

    // Boilerplate: src/plugins/plugin_name/plugin.js
    export default function(plugin_options) {
    	// plugin_options: plugin-specific options
    	// this: TomSelect instance
    };
    
    // Adding Dependencies
    export default function(plugin_options) {
    	this.require('another_plugin');
    };
    
    // Method Hooks (after)
    export default function(plugin_options) {
    	this.hook('after', 'setup', function() {
    		// .. additional setup
    	});
    };
    
    // Overriding Methods (instead)
    export default function(plugin_options) {
    	var original_setup = this.setup;
    	this.hook('instead', 'setup', function() {
    		// .. custom setup
    		return original_setup.apply(this, arguments);
    	});
    };
    
    // DOM Events
    export default function(plugin_options) {
    	this.hook('after', 'setup', function() {
    		this.control.addEventListener('click',function(evt){
    			alert('the control was clicked');
    		});
    	});
    };
  2. Understand the relationship between Tom Select and selectize.js

    master
    Tom Select is a fork of selectize.js designed with four primary goals: modernizing the codebase, decoupling from jQuery, expanding functionality, and addressing existing issue backlogs. Unlike the original selectize.js, Tom Select features a framework-agnostic design that works without jQuery or any other JavaScript framework.
  3. Understand TomSelect terminology: Settings, Options, and Items

    master

    To use TomSelect effectively, understand these core concepts:

    • Settings: Configuration parameters passed to the constructor. They are accessible via the settings property on the instance.
    • Options: The list of available objects to display. Each object must have a unique value (defined by valueField) and a label (defined by labelField).
    • Items: The list of currently selected values (the values of the selected options).
  4. Use plugins in Tom Select

    master

    You can add features to Tom Select using the plugins option in the configuration object. The plugin system uses the microplugin interface, allowing you to pass either an array of plugin names or an object where keys are plugin names and values are their specific configuration options.

    To use plugins, ensure they have been defined using TomSelect.define before initializing the instance.

  5. Initialize multiple TomSelect instances using a class selector

    master

    To apply TomSelect to multiple elements on a page, select the elements (e.g., using document.querySelectorAll) and iterate over them, calling the TomSelect constructor for each individual element.

    <script src="tom-select.complete.js"></script>
    <link href="tom-select.bootstrap4.css" rel="stylesheet" />
    <input class="select" /> ... <input class="select" />
    <script>
    document.querySelectorAll('.select').forEach((el)=>{
    	let settings = {};
     	new TomSelect(el,settings);
    });
    </script>
  6. Create a custom Tom Select bundle via NPM

    master

    If you want to create a optimized /dist/js/tom-select.custom.js file containing only specific plugins, you can build it from the repository source:

    # clone the repo
    git clone https://github.com/orchidjs/tom-select.git
    cd tom-select
    
    # install dev dependencies
    npm install
    
    # create /dist/js/tom-select.custom.js
    npm run build -- --plugins=remove_button,restore_on_backspace
  7. Identify breaking changes from selectize.js

    master

    If you are migrating from selectize.js to Tom Select, be aware of the following breaking changes:

    • CSS Class Names: Tom Select uses .ts-* class names instead of .selectize-*. These are customizable via SCSS and JS.
    • Stylesheets: The project uses SCSS instead of LESS.
    • Data Attributes: The dataAttr option now defaults to null instead of "data-data".
    • Custom Optgroups: When using a custom optgroup template, options must be explicitly appended to the optgroup.
    • Browser Support: Support for older browsers, including IE11, has been removed.