1.0.3 • Published 10 months ago

np-select v1.0.3

Weekly downloads
-
License
ISC
Repository
github
Last release
10 months ago

Description

This library is created to make life easier for fellows who needs to implement, NovaPoshta © address and warehouse selects. It contains two selects, which requires almost zero configuration.

Advantages:

⭐ TypeScript Types

⭐ Zero configuration

⭐ Robust API

Table of Contents


Installation

Script tag:

<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/np-select@1.0.3/build/np-select.umd.js"></script>
  • Also you can go to /build folder and download np-select.umd.js, np-select.d.ts if you want to have .ts types

Now select is availiable under NpSelect global variable:

document.addEventListener('DOMContentLoaded', () => {
  NpSelect.NpCitySelect({...});
  NpSelect.NpWarehoseSelect({...});
});

Package managers:

npm install --save np-select

yarn add np-select
import { NpCitySelect, NpWarehouseSelect, utils } from 'np-select';

NpCitySelect({...});
NpWarehouseSelect({...});

NpCitySelect:

This select is searchable and it fetches Nova Poshta cities on user input.

  NpCitySelect({
    apiKey: API_KEY,
    input: {
      name: 'city',
      placeholder: 'Select City',
    },
    button: {
      text: 'Select City',
    },
    root: document.querySelector('#city'),
  });

NpWarehouseSelect:

NameTypeDescription
city?stringif passed select will fetch warehouses for this city

This select is filterable and it filters passed options on user input.

  • If passed city NpCitySelect will fetch all warehouses for this city when mounted
  NpCitySelect({
    apiKey: API_KEY,
    input: {
      name: 'city',
      placeholder: 'Select City',
    },
    button: {
      text: 'Select City',
    },
    root: document.querySelector('#city'),
    city: 'Київ'
  });

Shared Properties:

List of configuration properties when you creating selects

NameTypeDescription
rootHTMLElementroot html element
apiKeystringYour NovaPoshta API_KEY
input?{ name: string, placeholder: string }input props
button?{ text: string }button props
placeholder?{ text: string }placeholder props
options?{ label: string; value: string }[]initial list of options
getOption?(item: ApiResponse) => {label: string, value: string}[]method to extract property and value from ApiResponse

Hooks:

NameTypeDescription
onMounted(select) => voidcalled after select is mounted
onSelect(item, select) => voidcalled when item is selected.
onOpen(select) => voidcalled when select opened, if onOpen returns false, select will not be opened
onInput(value: string, select) => voidcalled when input value changes

Methods

NameTypeDescription
validate() => booleanvalidates select
getFiltered() => {label: string, value: string}[]returns filtered options
setFiltered(options: {label: string, value: string}[]) => voidset filtered options
getOptions() => {label: string, value: string}[]returns all options
setOptions(options: {label: string, value: string}[]) => voidset all options
setOpen(open: boolean) => voidopen or close select
getOpen() => booleanreturn is select open
setDisabled(disabled: boolean) => voiddisable or enable select
getDisabled() => booleanreturns is select disabled
getValue() => stringget input value
setValue(value: string) => stringset input value
setLoading(loading: boolean) => voidset select loading
getLoading() => booleanget is select loading

Styling

ClassNames:

ClassType
.np-selectSelect classs
.np-select__buttonSelect button
.np-select__inputSelect input
.np-select__boxOptions box class
.np-select__optionOption class

Active states:

ClassType
.np-select[aria-invalid='true']Invalid/error class
.np-select[aria-busy='true']Loading class
.np-select[aria-disabled='true']Disabled class
.np-select.openSelect open class
.np-select__option.selectedOption selected class

CSS variables:

NameDescriptionDefault Value
--np-select-errorError colortomato
--np-select-whiteWhite color#fff
--np-select-textText color#221f1f
--np-select-activeActive color#e5f5ec
--np-select-disabledDisabled color.#d2d2d2
--np-select-box-shadowBox shadow color#221f1f40

Example usage:

import NpSelect from 'np-select';

NpSelect.NpCitySelect({
  root: document.querySelector('#city'),
  apiKey: API_KEY,
  input: {
    name: 'city',
  },
  button: {
    text: 'Select City',
  },
});

NpSelect.NpWarehouseSelect({
  root: document.querySelector('#warehouse'),
  apiKey: API_KEY,
  input: {
    name: 'warehouse',
  },
  button: {
    text: 'Select Warehouse',
  },
});

Common cases:

Warehouse select disabled untill city is not selected:

Most common case:

const warehouseSelect = NpWarehouseSelect({
  apiKey: API_KEY,
  input: {
    name: 'warehouse',
    placeholder: 'Select Warehouse',
  },
  button: {
    text: 'Select Warehouse',
  },
  root: document.querySelector('#warehouse'),
  onMounted: select => select.setDisabled(true),
});

NpCitySelect({
    apiKey: API_KEY,
    input: {
    name: 'city',
     placeholder: 'Select City',
    },
    button: {
     text: 'Select City',
    },
    root: document.querySelector('#city'),
      onSelect: async (item, select) => {
      const warehouses = await select.api.getNpWarehouses(item.value);
    
      warehouseSelect.setOptions(warehouses);
      warehouseSelect.setDisabled(false);
      warehouseSelect.setOpen(true);
    },
  });
});

Validate select on form submit:

Library provides error styles for select, which you can modify with css.

form.addEventListener('submit', e => {
  e.preventDefault();
  const isValid = warehouseSelect.validate();

  if (!isValid) {
    return;
  }
});

Validate multiple selects on form submit:

For this case you can use utility method validateMultiple()

form.addEventListener('submit', e => {
  e.preventDefault();
  const isValid = NpSelect.validateMultiple([warehouseSelect, citySelect]);

  if (!isValid) {
    return;
  }
});

Get select value:

Getting value as easy as getting it from <input /> element, or using getValue method

form.addEventListener('submit', e => {
  e.preventDefault();
  const isValid = NpSelect.validate(citySelect);

  if (!isValid) {
    return;
  }
  
  // Using getValue
  const city = citySelect.getValue();

  // Using form data
  const form = new FormData(e.target);
  const city = form.get('city');

  // Using querySelector
  const city = document.querySelector('[name="city"]').value;
});