mirror of
https://github.com/Choices-js/Choices.git
synced 2024-05-23 16:02:21 +02:00
rename addItemFilterFn (#699)
This commit is contained in:
parent
2a03d9be12
commit
b080bcda7d
18
README.md
18
README.md
|
@ -92,7 +92,7 @@ will be returned. If you target one element, that instance will be returned.
|
||||||
renderChoiceLimit: -1,
|
renderChoiceLimit: -1,
|
||||||
maxItemCount: -1,
|
maxItemCount: -1,
|
||||||
addItems: true,
|
addItems: true,
|
||||||
addItemFilterFn: null,
|
addItemFilter: null,
|
||||||
removeItems: true,
|
removeItems: true,
|
||||||
removeItemButton: false,
|
removeItemButton: false,
|
||||||
editItems: false,
|
editItems: false,
|
||||||
|
@ -370,23 +370,29 @@ Pass an array of objects:
|
||||||
|
|
||||||
**Usage:** Whether the scroll position should reset after adding an item.
|
**Usage:** Whether the scroll position should reset after adding an item.
|
||||||
|
|
||||||
### addItemFilterFn
|
### addItemFilter
|
||||||
|
|
||||||
**Type:** `Function` **Default:** `null`
|
**Type:** `string | RegExp | Function` **Default:** `null`
|
||||||
|
|
||||||
**Input types affected:** `text`
|
**Input types affected:** `text`
|
||||||
|
|
||||||
**Usage:** A filter function that will need to return `true` for a user to successfully add an item.
|
**Usage:** A RegExp or string (will be passed to RegExp constructor internally) or filter function that will need to return `true` for a user to successfully add an item.
|
||||||
|
|
||||||
**Example:**
|
**Example:**
|
||||||
|
|
||||||
```js
|
```js
|
||||||
// Only adds items matching the text test
|
// Only adds items matching the text test
|
||||||
new Choices(element, {
|
new Choices(element, {
|
||||||
addItemFilterFn: (value) => {
|
addItemFilter: (value) => {
|
||||||
return (value !== 'test');
|
return ['orange', 'apple', 'banana'].includes(value);
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// only items ending to `-red`
|
||||||
|
new Choices(element, {
|
||||||
|
addItemFilter: '-red$';
|
||||||
|
});
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### shouldSort
|
### shouldSort
|
||||||
|
|
|
@ -1,22 +1,49 @@
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
|
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8" />
|
||||||
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
|
<meta
|
||||||
|
name="viewport"
|
||||||
|
content="width=device-width,initial-scale=1,user-scalable=no"
|
||||||
|
/>
|
||||||
<title>Choices</title>
|
<title>Choices</title>
|
||||||
<meta name=description itemprop=description content="A lightweight, configurable select box/text input plugin. Similar to Select2 and Selectize but without the jQuery dependency.">
|
<meta
|
||||||
<link rel="apple-touch-icon" sizes="180x180" href="assets/images/apple-touch-icon.png">
|
name="description"
|
||||||
<link rel="icon" type="image/png" href="assets/images/favicon-32x32.png" sizes="32x32">
|
itemprop="description"
|
||||||
<link rel="icon" type="image/png" href="assets/images/favicon-16x16.png" sizes="16x16">
|
content="A lightweight, configurable select box/text input plugin. Similar to Select2 and Selectize but without the jQuery dependency."
|
||||||
<link rel="manifest" href="assets/images/manifest.json">
|
/>
|
||||||
<link rel="mask-icon" href="assets/images/safari-pinned-tab.svg" color="#00bcd4">
|
<link
|
||||||
<link rel="shortcut icon" href="assets/images/favicon.ico">
|
rel="apple-touch-icon"
|
||||||
<meta name="msapplication-config" content="/assets/images/browserconfig.xml">
|
sizes="180x180"
|
||||||
<meta name="theme-color" content="#ffffff">
|
href="assets/images/apple-touch-icon.png"
|
||||||
|
/>
|
||||||
|
<link
|
||||||
|
rel="icon"
|
||||||
|
type="image/png"
|
||||||
|
href="assets/images/favicon-32x32.png"
|
||||||
|
sizes="32x32"
|
||||||
|
/>
|
||||||
|
<link
|
||||||
|
rel="icon"
|
||||||
|
type="image/png"
|
||||||
|
href="assets/images/favicon-16x16.png"
|
||||||
|
sizes="16x16"
|
||||||
|
/>
|
||||||
|
<link rel="manifest" href="assets/images/manifest.json" />
|
||||||
|
<link
|
||||||
|
rel="mask-icon"
|
||||||
|
href="assets/images/safari-pinned-tab.svg"
|
||||||
|
color="#00bcd4"
|
||||||
|
/>
|
||||||
|
<link rel="shortcut icon" href="assets/images/favicon.ico" />
|
||||||
|
<meta
|
||||||
|
name="msapplication-config"
|
||||||
|
content="/assets/images/browserconfig.xml"
|
||||||
|
/>
|
||||||
|
<meta name="theme-color" content="#ffffff" />
|
||||||
|
|
||||||
<!-- Ignore these -->
|
<!-- Ignore these -->
|
||||||
<link rel="stylesheet" href="assets/styles/base.min.css">
|
<link rel="stylesheet" href="assets/styles/base.min.css" />
|
||||||
<!-- End ignore these -->
|
<!-- End ignore these -->
|
||||||
|
|
||||||
<!-- Optional includes -->
|
<!-- Optional includes -->
|
||||||
|
@ -24,14 +51,18 @@
|
||||||
<!-- End optional includes -->
|
<!-- End optional includes -->
|
||||||
|
|
||||||
<!-- Choices includes -->
|
<!-- Choices includes -->
|
||||||
<link rel="stylesheet" href="assets/styles/choices.min.css">
|
<link rel="stylesheet" href="assets/styles/choices.min.css" />
|
||||||
<script src="assets/scripts/choices.min.js"></script>
|
<script src="assets/scripts/choices.min.js"></script>
|
||||||
<!-- End Choices includes -->
|
<!-- End Choices includes -->
|
||||||
|
|
||||||
<!--[if lt IE 9]>
|
<!--[if lt IE 9]>
|
||||||
<style>
|
<style>
|
||||||
.hidden-ie { display: none; }
|
.hidden-ie {
|
||||||
.visible-ie { display: block; }
|
display: none;
|
||||||
|
}
|
||||||
|
.visible-ie {
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
<![endif]-->
|
<![endif]-->
|
||||||
</head>
|
</head>
|
||||||
|
@ -40,51 +71,121 @@
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<div class="section">
|
<div class="section">
|
||||||
<a href="https://github.com/jshjohnson/Choices" class="logo">
|
<a href="https://github.com/jshjohnson/Choices" class="logo">
|
||||||
<img src="assets/images/logo.svg" alt="Choices.js logo" class="logo__img hidden-ie">
|
<img
|
||||||
|
src="assets/images/logo.svg"
|
||||||
|
alt="Choices.js logo"
|
||||||
|
class="logo__img hidden-ie"
|
||||||
|
/>
|
||||||
<h1 class="visible-ie">Choices.js</h1>
|
<h1 class="visible-ie">Choices.js</h1>
|
||||||
</a>
|
</a>
|
||||||
<p>Choices.js is a lightweight, configurable select box/text input plugin. Similar to Select2 and Selectize but without
|
<p>
|
||||||
the jQuery dependency.</p>
|
Choices.js is a lightweight, configurable select box/text input
|
||||||
<p>For all config options, visit the <a href="https://github.com/jshjohnson/Choices">GitHub repo</a>.</p>
|
plugin. Similar to Select2 and Selectize but without the jQuery
|
||||||
|
dependency.
|
||||||
<hr>
|
|
||||||
<p class="h4 text-center">
|
|
||||||
<strong>Interested in writing your own ES6 JavaScript plugins? Check out <a href="https://ES6.io/friend/JOHNSON">ES6.io</a> for great tutorials! 💪🏼</strong>
|
|
||||||
</p>
|
</p>
|
||||||
<hr>
|
<p>
|
||||||
|
For all config options, visit the
|
||||||
|
<a href="https://github.com/jshjohnson/Choices">GitHub repo</a>.
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<hr />
|
||||||
|
<p class="h4 text-center">
|
||||||
|
<strong
|
||||||
|
>Interested in writing your own ES6 JavaScript plugins? Check out
|
||||||
|
<a href="https://ES6.io/friend/JOHNSON">ES6.io</a> for great
|
||||||
|
tutorials! 💪🏼</strong
|
||||||
|
>
|
||||||
|
</p>
|
||||||
|
<hr />
|
||||||
|
|
||||||
<h2>Text inputs</h2>
|
<h2>Text inputs</h2>
|
||||||
<label for="choices-text-remove-button">Limited to 5 values with remove button</label>
|
<label for="choices-text-remove-button"
|
||||||
<input class="form-control" id="choices-text-remove-button" type="text" value="preset-1,preset-2" placeholder="Enter something">
|
>Limited to 5 values with remove button</label
|
||||||
|
>
|
||||||
|
<input
|
||||||
|
class="form-control"
|
||||||
|
id="choices-text-remove-button"
|
||||||
|
type="text"
|
||||||
|
value="preset-1,preset-2"
|
||||||
|
placeholder="Enter something"
|
||||||
|
/>
|
||||||
|
|
||||||
<label for="choices-text-unique-values">Unique values only, no pasting</label>
|
<label for="choices-text-unique-values"
|
||||||
<input class="form-control" id="choices-text-unique-values" type="text" value="preset-1, preset-2" placeholder="This is a placeholder"
|
>Unique values only, no pasting</label
|
||||||
class="custom class">
|
>
|
||||||
|
<input
|
||||||
|
class="form-control"
|
||||||
|
id="choices-text-unique-values"
|
||||||
|
type="text"
|
||||||
|
value="preset-1, preset-2"
|
||||||
|
placeholder="This is a placeholder"
|
||||||
|
class="custom class"
|
||||||
|
/>
|
||||||
|
|
||||||
<label for="choices-text-email-filter">Email addresses only</label>
|
<label for="choices-text-email-filter">Email addresses only</label>
|
||||||
<input class="form-control" id="choices-text-email-filter" type="text" placeholder="This is a placeholder">
|
<input
|
||||||
|
class="form-control"
|
||||||
|
id="choices-text-email-filter"
|
||||||
|
type="text"
|
||||||
|
placeholder="This is a placeholder"
|
||||||
|
/>
|
||||||
|
|
||||||
<label for="choices-text-disabled">Disabled</label>
|
<label for="choices-text-disabled">Disabled</label>
|
||||||
<input class="form-control" id="choices-text-disabled" type="text" value="josh@joshuajohnson.co.uk, joe@bloggs.co.uk" placeholder="This is a placeholder">
|
<input
|
||||||
|
class="form-control"
|
||||||
|
id="choices-text-disabled"
|
||||||
|
type="text"
|
||||||
|
value="josh@joshuajohnson.co.uk, joe@bloggs.co.uk"
|
||||||
|
placeholder="This is a placeholder"
|
||||||
|
/>
|
||||||
|
|
||||||
<label for="choices-text-prepend-append-value">Prepends and appends a value to each items return value</label>
|
<label for="choices-text-prepend-append-value"
|
||||||
<input class="form-control" id="choices-text-prepend-append-value" type="text" value="preset-1, preset-2" placeholder="This is a placeholder">
|
>Prepends and appends a value to each items return value</label
|
||||||
|
>
|
||||||
|
<input
|
||||||
|
class="form-control"
|
||||||
|
id="choices-text-prepend-append-value"
|
||||||
|
type="text"
|
||||||
|
value="preset-1, preset-2"
|
||||||
|
placeholder="This is a placeholder"
|
||||||
|
/>
|
||||||
|
|
||||||
<label for="choices-text-preset-values">Preset values passed through options</label>
|
<label for="choices-text-preset-values"
|
||||||
<input class="form-control" id="choices-text-preset-values" type="text" value="Michael Smith" placeholder="This is a placeholder">
|
>Preset values passed through options</label
|
||||||
|
>
|
||||||
|
<input
|
||||||
|
class="form-control"
|
||||||
|
id="choices-text-preset-values"
|
||||||
|
type="text"
|
||||||
|
value="Michael Smith"
|
||||||
|
placeholder="This is a placeholder"
|
||||||
|
/>
|
||||||
|
|
||||||
<label for="choices-text-i18n">I18N labels</label>
|
<label for="choices-text-i18n">I18N labels</label>
|
||||||
<input class="form-control" id="choices-text-i18n" type="text">
|
<input class="form-control" id="choices-text-i18n" type="text" />
|
||||||
|
|
||||||
<label for="choices-text-rtl">Right-to-left</label>
|
<label for="choices-text-rtl">Right-to-left</label>
|
||||||
<input data-trigger class="form-control" id="choices-text-rtl" type="text" value="Value 1, Value 2" dir="rtl">
|
<input
|
||||||
|
data-trigger
|
||||||
|
class="form-control"
|
||||||
|
id="choices-text-rtl"
|
||||||
|
type="text"
|
||||||
|
value="Value 1, Value 2"
|
||||||
|
dir="rtl"
|
||||||
|
/>
|
||||||
|
|
||||||
<hr>
|
<hr />
|
||||||
|
|
||||||
<h2>Multiple select input</h2>
|
<h2>Multiple select input</h2>
|
||||||
<label for="choices-multiple-default">Default</label>
|
<label for="choices-multiple-default">Default</label>
|
||||||
<select class="form-control" data-trigger name="choices-multiple-default" id="choices-multiple-default" placeholder="This is a placeholder"
|
<select
|
||||||
multiple>
|
class="form-control"
|
||||||
|
data-trigger
|
||||||
|
name="choices-multiple-default"
|
||||||
|
id="choices-multiple-default"
|
||||||
|
placeholder="This is a placeholder"
|
||||||
|
multiple
|
||||||
|
>
|
||||||
<option value="Choice 1" selected>Choice 1</option>
|
<option value="Choice 1" selected>Choice 1</option>
|
||||||
<option value="Choice 2">Choice 2</option>
|
<option value="Choice 2">Choice 2</option>
|
||||||
<option value="Choice 3">Choice 3</option>
|
<option value="Choice 3">Choice 3</option>
|
||||||
|
@ -92,8 +193,13 @@
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<label for="choices-multiple-remove-button">With remove button</label>
|
<label for="choices-multiple-remove-button">With remove button</label>
|
||||||
<select class="form-control" name="choices-multiple-remove-button" id="choices-multiple-remove-button" placeholder="This is a placeholder"
|
<select
|
||||||
multiple>
|
class="form-control"
|
||||||
|
name="choices-multiple-remove-button"
|
||||||
|
id="choices-multiple-remove-button"
|
||||||
|
placeholder="This is a placeholder"
|
||||||
|
multiple
|
||||||
|
>
|
||||||
<option value="Choice 1" selected>Choice 1</option>
|
<option value="Choice 1" selected>Choice 1</option>
|
||||||
<option value="Choice 2">Choice 2</option>
|
<option value="Choice 2">Choice 2</option>
|
||||||
<option value="Choice 3">Choice 3</option>
|
<option value="Choice 3">Choice 3</option>
|
||||||
|
@ -101,8 +207,13 @@
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<label for="choices-multiple-groups">Option groups</label>
|
<label for="choices-multiple-groups">Option groups</label>
|
||||||
<select class="form-control" name="choices-multiple-groups" id="choices-multiple-groups" placeholder="This is a placeholder"
|
<select
|
||||||
multiple>
|
class="form-control"
|
||||||
|
name="choices-multiple-groups"
|
||||||
|
id="choices-multiple-groups"
|
||||||
|
placeholder="This is a placeholder"
|
||||||
|
multiple
|
||||||
|
>
|
||||||
<optgroup label="UK">
|
<optgroup label="UK">
|
||||||
<option value="London">London</option>
|
<option value="London">London</option>
|
||||||
<option value="Manchester">Manchester</option>
|
<option value="Manchester">Manchester</option>
|
||||||
|
@ -135,48 +246,98 @@
|
||||||
</optgroup>
|
</optgroup>
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<p><small>If the following example do not load, the Discogs rate limit has probably been reached. Try again later!</small></p>
|
<p>
|
||||||
|
<small
|
||||||
|
>If the following example do not load, the Discogs rate limit has
|
||||||
|
probably been reached. Try again later!</small
|
||||||
|
>
|
||||||
|
</p>
|
||||||
|
|
||||||
<label for="choices-multiple-remote-fetch">Options from remote source (Fetch API) & limited to 5</label>
|
<label for="choices-multiple-remote-fetch"
|
||||||
<select class="form-control" name="choices-multiple-remote-fetch" id="choices-multiple-remote-fetch" multiple></select>
|
>Options from remote source (Fetch API) & limited to 5</label
|
||||||
|
>
|
||||||
|
<select
|
||||||
|
class="form-control"
|
||||||
|
name="choices-multiple-remote-fetch"
|
||||||
|
id="choices-multiple-remote-fetch"
|
||||||
|
multiple
|
||||||
|
></select>
|
||||||
|
|
||||||
<label for="choices-multiple-rtl">Right-to-left</label>
|
<label for="choices-multiple-rtl">Right-to-left</label>
|
||||||
<select class="form-control" data-trigger name="choices-multiple-rtl" id="choices-multiple-rtl" placeholder="This is a placeholder"
|
<select
|
||||||
multiple dir="rtl">
|
class="form-control"
|
||||||
|
data-trigger
|
||||||
|
name="choices-multiple-rtl"
|
||||||
|
id="choices-multiple-rtl"
|
||||||
|
placeholder="This is a placeholder"
|
||||||
|
multiple
|
||||||
|
dir="rtl"
|
||||||
|
>
|
||||||
<option value="Choice 1" selected>Choice 1</option>
|
<option value="Choice 1" selected>Choice 1</option>
|
||||||
<option value="Choice 2">Choice 2</option>
|
<option value="Choice 2">Choice 2</option>
|
||||||
<option value="Choice 3">Choice 3</option>
|
<option value="Choice 3">Choice 3</option>
|
||||||
<option value="Choice 4" disabled>Choice 4</option>
|
<option value="Choice 4" disabled>Choice 4</option>
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<label for="choices-multiple-labels">Use label in event (add/remove)</label>
|
<label for="choices-multiple-labels"
|
||||||
|
>Use label in event (add/remove)</label
|
||||||
|
>
|
||||||
<p id="message"></p>
|
<p id="message"></p>
|
||||||
<select id="choices-multiple-labels" multiple></select>
|
<select id="choices-multiple-labels" multiple></select>
|
||||||
<hr>
|
<hr />
|
||||||
|
|
||||||
<h2>Single select input</h2>
|
<h2>Single select input</h2>
|
||||||
<label for="choices-single-default">Default</label>
|
<label for="choices-single-default">Default</label>
|
||||||
<select class="form-control" data-trigger name="choices-single-default" id="choices-single-default" placeholder="This is a search placeholder">
|
<select
|
||||||
|
class="form-control"
|
||||||
|
data-trigger
|
||||||
|
name="choices-single-default"
|
||||||
|
id="choices-single-default"
|
||||||
|
placeholder="This is a search placeholder"
|
||||||
|
>
|
||||||
<option value="">This is a placeholder</option>
|
<option value="">This is a placeholder</option>
|
||||||
<option value="Choice 1">Choice 1</option>
|
<option value="Choice 1">Choice 1</option>
|
||||||
<option value="Choice 2">Choice 2</option>
|
<option value="Choice 2">Choice 2</option>
|
||||||
<option value="Choice 3">Choice 3</option>
|
<option value="Choice 3">Choice 3</option>
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<p><small>If the following two examples do not load, the Discogs rate limit has probably been reached. Try again later!</small></p>
|
<p>
|
||||||
|
<small
|
||||||
|
>If the following two examples do not load, the Discogs rate limit
|
||||||
|
has probably been reached. Try again later!</small
|
||||||
|
>
|
||||||
|
</p>
|
||||||
|
|
||||||
<label for="choices-single-remote-fetch">Options from remote source (Fetch API)</label>
|
<label for="choices-single-remote-fetch"
|
||||||
<select class="form-control" name="choices-single-remote-fetch" id="choices-single-remote-fetch">
|
>Options from remote source (Fetch API)</label
|
||||||
|
>
|
||||||
|
<select
|
||||||
|
class="form-control"
|
||||||
|
name="choices-single-remote-fetch"
|
||||||
|
id="choices-single-remote-fetch"
|
||||||
|
>
|
||||||
<option value="">Pick an Arctic Monkeys' record</option>
|
<option value="">Pick an Arctic Monkeys' record</option>
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<label for="choices-single-remove-xhr">Options from remote source (XHR) & remove button</label>
|
<label for="choices-single-remove-xhr"
|
||||||
<select class="form-control" name="choices-single-remove-xhr" id="choices-single-remove-xhr">
|
>Options from remote source (XHR) & remove button</label
|
||||||
|
>
|
||||||
|
<select
|
||||||
|
class="form-control"
|
||||||
|
name="choices-single-remove-xhr"
|
||||||
|
id="choices-single-remove-xhr"
|
||||||
|
>
|
||||||
<option value="">Pick a Smiths' record</option>
|
<option value="">Pick a Smiths' record</option>
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<label for="choices-single-groups">Option groups</label>
|
<label for="choices-single-groups">Option groups</label>
|
||||||
<select class="form-control" data-trigger name="choices-single-groups" id="choices-single-groups" placeholder="This is a placeholder">
|
<select
|
||||||
|
class="form-control"
|
||||||
|
data-trigger
|
||||||
|
name="choices-single-groups"
|
||||||
|
id="choices-single-groups"
|
||||||
|
placeholder="This is a placeholder"
|
||||||
|
>
|
||||||
<optgroup label="UK">
|
<optgroup label="UK">
|
||||||
<option value="London">London</option>
|
<option value="London">London</option>
|
||||||
<option value="Manchester">Manchester</option>
|
<option value="Manchester">Manchester</option>
|
||||||
|
@ -210,35 +371,77 @@
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<label for="choices-single-rtl">Right-to-left</label>
|
<label for="choices-single-rtl">Right-to-left</label>
|
||||||
<select class="form-control" data-trigger name="choices-single-rtl" id="choices-single-rtl" placeholder="This is a placeholder"
|
<select
|
||||||
dir="rtl">
|
class="form-control"
|
||||||
|
data-trigger
|
||||||
|
name="choices-single-rtl"
|
||||||
|
id="choices-single-rtl"
|
||||||
|
placeholder="This is a placeholder"
|
||||||
|
dir="rtl"
|
||||||
|
>
|
||||||
<option value="Choice 1">Choice 1</option>
|
<option value="Choice 1">Choice 1</option>
|
||||||
<option value="Choice 2">Choice 2</option>
|
<option value="Choice 2">Choice 2</option>
|
||||||
<option value="Choice 3">Choice 3</option>
|
<option value="Choice 3">Choice 3</option>
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<label for="choices-single-no-search">Options added via config with no search</label>
|
<label for="choices-single-no-search"
|
||||||
<select class="form-control" name="choices-single-no-search" id="choices-single-no-search">
|
>Options added via config with no search</label
|
||||||
|
>
|
||||||
|
<select
|
||||||
|
class="form-control"
|
||||||
|
name="choices-single-no-search"
|
||||||
|
id="choices-single-no-search"
|
||||||
|
>
|
||||||
<option value="0">Zero</option>
|
<option value="0">Zero</option>
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<label for="choices-single-preset-options">Option and option groups added via config</label>
|
<label for="choices-single-preset-options"
|
||||||
<select class="form-control" name="choices-single-preset-options" id="choices-single-preset-options" placeholder="This is a placeholder"></select>
|
>Option and option groups added via config</label
|
||||||
|
>
|
||||||
|
<select
|
||||||
|
class="form-control"
|
||||||
|
name="choices-single-preset-options"
|
||||||
|
id="choices-single-preset-options"
|
||||||
|
placeholder="This is a placeholder"
|
||||||
|
></select>
|
||||||
|
|
||||||
<label for="choices-single-selected-option">Option selected via config with custom properties</label>
|
<label for="choices-single-selected-option"
|
||||||
<p><small>Try searching for 'fantastic', "Label 3" should display</small></p>
|
>Option selected via config with custom properties</label
|
||||||
<select class="form-control" name="choices-single-selected-option" id="choices-single-selected-option" placeholder="This is a placeholder"></select>
|
>
|
||||||
|
<p>
|
||||||
|
<small>Try searching for 'fantastic', "Label 3" should display</small>
|
||||||
|
</p>
|
||||||
|
<select
|
||||||
|
class="form-control"
|
||||||
|
name="choices-single-selected-option"
|
||||||
|
id="choices-single-selected-option"
|
||||||
|
placeholder="This is a placeholder"
|
||||||
|
></select>
|
||||||
|
|
||||||
<label for="choices-with-custom-props-via-html">Option searchable by custom properties via <code>data-custom-properties</code> attribute</label>
|
<label for="choices-with-custom-props-via-html"
|
||||||
<p><small>Try searching for 'fantastic', "Label 3" should display</small></p>
|
>Option searchable by custom properties via
|
||||||
|
<code>data-custom-properties</code> attribute</label
|
||||||
|
>
|
||||||
|
<p>
|
||||||
|
<small>Try searching for 'fantastic', "Label 3" should display</small>
|
||||||
|
</p>
|
||||||
<select class="form-control" id="choices-with-custom-props-via-html">
|
<select class="form-control" id="choices-with-custom-props-via-html">
|
||||||
<option value="Dropdown item 1">Label One</option>
|
<option value="Dropdown item 1">Label One</option>
|
||||||
<option value="Dropdown item 2" selected disabled>Label Two</option>
|
<option value="Dropdown item 2" selected disabled>Label Two</option>
|
||||||
<option value="Dropdown item 3" data-custom-properties='This option is fantastic'>Label Three</option>
|
<option
|
||||||
|
value="Dropdown item 3"
|
||||||
|
data-custom-properties="This option is fantastic"
|
||||||
|
>Label Three</option
|
||||||
|
>
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<label for="choices-single-no-sorting">Options without sorting</label>
|
<label for="choices-single-no-sorting">Options without sorting</label>
|
||||||
<select class="form-control" name="choices-single-no-sorting" id="choices-single-no-sorting" placeholder="This is a placeholder">
|
<select
|
||||||
|
class="form-control"
|
||||||
|
name="choices-single-no-sorting"
|
||||||
|
id="choices-single-no-sorting"
|
||||||
|
placeholder="This is a placeholder"
|
||||||
|
>
|
||||||
<option value="Madrid">Madrid</option>
|
<option value="Madrid">Madrid</option>
|
||||||
<option value="Toronto">Toronto</option>
|
<option value="Toronto">Toronto</option>
|
||||||
<option value="Vancouver">Vancouver</option>
|
<option value="Vancouver">Vancouver</option>
|
||||||
|
@ -260,17 +463,30 @@
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<label for="choices-single-custom-templates">Custom templates</label>
|
<label for="choices-single-custom-templates">Custom templates</label>
|
||||||
<select class="form-control" name="choices-single-custom-templates" id="choices-single-custom-templates" placeholder="This is a placeholder">
|
<select
|
||||||
|
class="form-control"
|
||||||
|
name="choices-single-custom-templates"
|
||||||
|
id="choices-single-custom-templates"
|
||||||
|
placeholder="This is a placeholder"
|
||||||
|
>
|
||||||
<option value="React">React</option>
|
<option value="React">React</option>
|
||||||
<option value="Angular">Angular</option>
|
<option value="Angular">Angular</option>
|
||||||
<option value="Ember">Ember</option>
|
<option value="Ember">Ember</option>
|
||||||
<option value="Vue">Vue</option>
|
<option value="Vue">Vue</option>
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<p>Below is an example of how you could have two select inputs depend on eachother. 'Tube stations' will only be enabled if
|
<p>
|
||||||
the value of 'Cities' is 'London'</p>
|
Below is an example of how you could have two select inputs depend on
|
||||||
|
eachother. 'Tube stations' will only be enabled if the value of
|
||||||
|
'Cities' is 'London'
|
||||||
|
</p>
|
||||||
<label for="cities">Cities</label>
|
<label for="cities">Cities</label>
|
||||||
<select class="form-control" name="cities" id="cities" placeholder="Choose a city">
|
<select
|
||||||
|
class="form-control"
|
||||||
|
name="cities"
|
||||||
|
id="cities"
|
||||||
|
placeholder="Choose a city"
|
||||||
|
>
|
||||||
<option value="Leeds">Leeds</option>
|
<option value="Leeds">Leeds</option>
|
||||||
<option value="Manchester">Manchester</option>
|
<option value="Manchester">Manchester</option>
|
||||||
<option value="London">London</option>
|
<option value="London">London</option>
|
||||||
|
@ -279,20 +495,32 @@
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<label for="tube-stations">Tube stations</label>
|
<label for="tube-stations">Tube stations</label>
|
||||||
<select class="form-control" name="tube-stations" id="tube-stations" placeholder="Choose a tube station">
|
<select
|
||||||
|
class="form-control"
|
||||||
|
name="tube-stations"
|
||||||
|
id="tube-stations"
|
||||||
|
placeholder="Choose a tube station"
|
||||||
|
>
|
||||||
<option value="Moorgate">Moorgate</option>
|
<option value="Moorgate">Moorgate</option>
|
||||||
<option value="St Pauls">St Pauls</option>
|
<option value="St Pauls">St Pauls</option>
|
||||||
<option value="Old Street">Old Street</option>
|
<option value="Old Street">Old Street</option>
|
||||||
<option value="Liverpool Street">Liverpool Street</option>
|
<option value="Liverpool Street">Liverpool Street</option>
|
||||||
<option value="Kings Cross St. Pancras">Kings Cross St. Pancras</option>
|
<option value="Kings Cross St. Pancras"
|
||||||
|
>Kings Cross St. Pancras</option
|
||||||
|
>
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<hr>
|
<hr />
|
||||||
<h2>Form interaction</h2>
|
<h2>Form interaction</h2>
|
||||||
<p>Change the values and press reset to restore to initial state.</p>
|
<p>Change the values and press reset to restore to initial state.</p>
|
||||||
<form>
|
<form>
|
||||||
<label for="reset-simple">Change me!</label>
|
<label for="reset-simple">Change me!</label>
|
||||||
<select class="form-control" name="reset-simple" id="reset-simple" placeholder="Choose an option">
|
<select
|
||||||
|
class="form-control"
|
||||||
|
name="reset-simple"
|
||||||
|
id="reset-simple"
|
||||||
|
placeholder="Choose an option"
|
||||||
|
>
|
||||||
<option value="Option 1">Option 1</option>
|
<option value="Option 1">Option 1</option>
|
||||||
<option value="Option 2" selected>Option 2</option>
|
<option value="Option 2" selected>Option 2</option>
|
||||||
<option value="Option 3">Option 3</option>
|
<option value="Option 3">Option 3</option>
|
||||||
|
@ -301,8 +529,13 @@
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<label for="reset-multiple">And me!</label>
|
<label for="reset-multiple">And me!</label>
|
||||||
<select class="form-control" name="reset-multiple" id="reset-multiple" placeholder="This is a placeholder"
|
<select
|
||||||
multiple>
|
class="form-control"
|
||||||
|
name="reset-multiple"
|
||||||
|
id="reset-multiple"
|
||||||
|
placeholder="This is a placeholder"
|
||||||
|
multiple
|
||||||
|
>
|
||||||
<option value="Choice 1" selected>Choice 1</option>
|
<option value="Choice 1" selected>Choice 1</option>
|
||||||
<option value="Choice 2">Choice 2</option>
|
<option value="Choice 2">Choice 2</option>
|
||||||
<option value="Choice 3">Choice 3</option>
|
<option value="Choice 3">Choice 3</option>
|
||||||
|
@ -315,12 +548,15 @@
|
||||||
</div>
|
</div>
|
||||||
<script>
|
<script>
|
||||||
document.addEventListener('DOMContentLoaded', function() {
|
document.addEventListener('DOMContentLoaded', function() {
|
||||||
var textRemove = new Choices(document.getElementById('choices-text-remove-button'), {
|
var textRemove = new Choices(
|
||||||
|
document.getElementById('choices-text-remove-button'),
|
||||||
|
{
|
||||||
delimiter: ',',
|
delimiter: ',',
|
||||||
editItems: true,
|
editItems: true,
|
||||||
maxItemCount: 5,
|
maxItemCount: 5,
|
||||||
removeItemButton: true,
|
removeItemButton: true,
|
||||||
});
|
},
|
||||||
|
);
|
||||||
|
|
||||||
var textUniqueVals = new Choices('#choices-text-unique-values', {
|
var textUniqueVals = new Choices('#choices-text-unique-values', {
|
||||||
paste: false,
|
paste: false,
|
||||||
|
@ -334,7 +570,9 @@
|
||||||
editItems: true,
|
editItems: true,
|
||||||
maxItemCount: 5,
|
maxItemCount: 5,
|
||||||
addItemText: function(value) {
|
addItemText: function(value) {
|
||||||
return 'Appuyez sur Entrée pour ajouter <b>"' + String(value) + '"</b>';
|
return (
|
||||||
|
'Appuyez sur Entrée pour ajouter <b>"' + String(value) + '"</b>'
|
||||||
|
);
|
||||||
},
|
},
|
||||||
maxItemText: function(maxItemCount) {
|
maxItemText: function(maxItemCount) {
|
||||||
return String(maxItemCount) + 'valeurs peuvent être ajoutées';
|
return String(maxItemCount) + 'valeurs peuvent être ajoutées';
|
||||||
|
@ -344,12 +582,12 @@
|
||||||
|
|
||||||
var textEmailFilter = new Choices('#choices-text-email-filter', {
|
var textEmailFilter = new Choices('#choices-text-email-filter', {
|
||||||
editItems: true,
|
editItems: true,
|
||||||
addItemFilterFn: function (value) {
|
addItemFilter: function(value) {
|
||||||
if (!value) {
|
if (!value) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const regex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
|
const regex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
|
||||||
const expression = new RegExp(regex.source, 'i');
|
const expression = new RegExp(regex.source, 'i');
|
||||||
return expression.test(value);
|
return expression.test(value);
|
||||||
},
|
},
|
||||||
|
@ -360,29 +598,39 @@
|
||||||
removeItems: false,
|
removeItems: false,
|
||||||
}).disable();
|
}).disable();
|
||||||
|
|
||||||
var textPrependAppendVal = new Choices('#choices-text-prepend-append-value', {
|
var textPrependAppendVal = new Choices(
|
||||||
|
'#choices-text-prepend-append-value',
|
||||||
|
{
|
||||||
prependValue: 'item-',
|
prependValue: 'item-',
|
||||||
appendValue: '-' + Date.now(),
|
appendValue: '-' + Date.now(),
|
||||||
}).removeActiveItems();
|
},
|
||||||
|
).removeActiveItems();
|
||||||
|
|
||||||
var textPresetVal = new Choices('#choices-text-preset-values', {
|
var textPresetVal = new Choices('#choices-text-preset-values', {
|
||||||
items: ['Josh Johnson', {
|
items: [
|
||||||
|
'Josh Johnson',
|
||||||
|
{
|
||||||
value: 'joe@bloggs.co.uk',
|
value: 'joe@bloggs.co.uk',
|
||||||
label: 'Joe Bloggs',
|
label: 'Joe Bloggs',
|
||||||
customProperties: {
|
customProperties: {
|
||||||
description: 'Joe Blogg is such a generic name'
|
description: 'Joe Blogg is such a generic name',
|
||||||
}
|
},
|
||||||
}],
|
},
|
||||||
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
var multipleDefault = new Choices(document.getElementById('choices-multiple-groups'));
|
var multipleDefault = new Choices(
|
||||||
|
document.getElementById('choices-multiple-groups'),
|
||||||
|
);
|
||||||
|
|
||||||
var multipleFetch = new Choices('#choices-multiple-remote-fetch', {
|
var multipleFetch = new Choices('#choices-multiple-remote-fetch', {
|
||||||
placeholder: true,
|
placeholder: true,
|
||||||
placeholderValue: 'Pick an Strokes record',
|
placeholderValue: 'Pick an Strokes record',
|
||||||
maxItemCount: 5,
|
maxItemCount: 5,
|
||||||
}).ajax(function(callback) {
|
}).ajax(function(callback) {
|
||||||
fetch('https://api.discogs.com/artists/55980/releases?token=QBRmstCkwXEvCjTclCpumbtNwvVkEzGAdELXyRyW')
|
fetch(
|
||||||
|
'https://api.discogs.com/artists/55980/releases?token=QBRmstCkwXEvCjTclCpumbtNwvVkEzGAdELXyRyW',
|
||||||
|
)
|
||||||
.then(function(response) {
|
.then(function(response) {
|
||||||
response.json().then(function(data) {
|
response.json().then(function(data) {
|
||||||
callback(data.releases, 'title', 'title');
|
callback(data.releases, 'title', 'title');
|
||||||
|
@ -393,9 +641,12 @@
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
var multipleCancelButton = new Choices('#choices-multiple-remove-button', {
|
var multipleCancelButton = new Choices(
|
||||||
|
'#choices-multiple-remove-button',
|
||||||
|
{
|
||||||
removeItemButton: true,
|
removeItemButton: true,
|
||||||
});
|
},
|
||||||
|
);
|
||||||
|
|
||||||
/* Use label on event */
|
/* Use label on event */
|
||||||
var choicesSelect = new Choices('#choices-multiple-labels', {
|
var choicesSelect = new Choices('#choices-multiple-labels', {
|
||||||
|
@ -405,24 +656,39 @@
|
||||||
{ value: 'Two', label: 'Label Two', disabled: true },
|
{ value: 'Two', label: 'Label Two', disabled: true },
|
||||||
{ value: 'Three', label: 'Label Three' },
|
{ value: 'Three', label: 'Label Three' },
|
||||||
],
|
],
|
||||||
}).setChoices([
|
}).setChoices(
|
||||||
|
[
|
||||||
{ value: 'Four', label: 'Label Four', disabled: true },
|
{ value: 'Four', label: 'Label Four', disabled: true },
|
||||||
{ value: 'Five', label: 'Label Five' },
|
{ value: 'Five', label: 'Label Five' },
|
||||||
{ value: 'Six', label: 'Label Six', selected: true },
|
{ value: 'Six', label: 'Label Six', selected: true },
|
||||||
], 'value', 'label', false);
|
],
|
||||||
|
'value',
|
||||||
|
'label',
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
|
||||||
choicesSelect.passedElement.element.addEventListener('addItem', function(event) {
|
choicesSelect.passedElement.element.addEventListener(
|
||||||
document.getElementById('message').innerHTML = 'You just added "' + event.detail.label + '"';
|
'addItem',
|
||||||
});
|
function(event) {
|
||||||
|
document.getElementById('message').innerHTML =
|
||||||
|
'You just added "' + event.detail.label + '"';
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
choicesSelect.passedElement.element.addEventListener('removeItem', function(event) {
|
choicesSelect.passedElement.element.addEventListener(
|
||||||
document.getElementById('message').innerHTML = 'You just removed "' + event.detail.label + '"';
|
'removeItem',
|
||||||
});
|
function(event) {
|
||||||
|
document.getElementById('message').innerHTML =
|
||||||
|
'You just removed "' + event.detail.label + '"';
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
var singleFetch = new Choices('#choices-single-remote-fetch', {
|
var singleFetch = new Choices('#choices-single-remote-fetch', {
|
||||||
searchPlaceholderValue: 'Search for an Arctic Monkeys record',
|
searchPlaceholderValue: 'Search for an Arctic Monkeys record',
|
||||||
}).ajax(function(callback) {
|
}).ajax(function(callback) {
|
||||||
fetch('https://api.discogs.com/artists/391170/releases?token=QBRmstCkwXEvCjTclCpumbtNwvVkEzGAdELXyRyW')
|
fetch(
|
||||||
|
'https://api.discogs.com/artists/391170/releases?token=QBRmstCkwXEvCjTclCpumbtNwvVkEzGAdELXyRyW',
|
||||||
|
)
|
||||||
.then(function(response) {
|
.then(function(response) {
|
||||||
response.json().then(function(data) {
|
response.json().then(function(data) {
|
||||||
callback(data.releases, 'title', 'title');
|
callback(data.releases, 'title', 'title');
|
||||||
|
@ -436,10 +702,14 @@
|
||||||
|
|
||||||
var singleXhrRemove = new Choices('#choices-single-remove-xhr', {
|
var singleXhrRemove = new Choices('#choices-single-remove-xhr', {
|
||||||
removeItemButton: true,
|
removeItemButton: true,
|
||||||
searchPlaceholderValue: 'Search for a Smiths\' record'
|
searchPlaceholderValue: "Search for a Smiths' record",
|
||||||
}).ajax(function(callback) {
|
}).ajax(function(callback) {
|
||||||
var request = new XMLHttpRequest();
|
var request = new XMLHttpRequest();
|
||||||
request.open('get', 'https://api.discogs.com/artists/83080/releases?token=QBRmstCkwXEvCjTclCpumbtNwvVkEzGAdELXyRyW', true);
|
request.open(
|
||||||
|
'get',
|
||||||
|
'https://api.discogs.com/artists/83080/releases?token=QBRmstCkwXEvCjTclCpumbtNwvVkEzGAdELXyRyW',
|
||||||
|
true,
|
||||||
|
);
|
||||||
request.onreadystatechange = function() {
|
request.onreadystatechange = function() {
|
||||||
var status;
|
var status;
|
||||||
var data;
|
var data;
|
||||||
|
@ -453,13 +723,13 @@
|
||||||
console.error(status);
|
console.error(status);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
request.send();
|
request.send();
|
||||||
});
|
});
|
||||||
|
|
||||||
var genericExamples = new Choices('[data-trigger]', {
|
var genericExamples = new Choices('[data-trigger]', {
|
||||||
placeholderValue: 'This is a placeholder set in the config',
|
placeholderValue: 'This is a placeholder set in the config',
|
||||||
searchPlaceholderValue: 'This is a search placeholder'
|
searchPlaceholderValue: 'This is a search placeholder',
|
||||||
});
|
});
|
||||||
|
|
||||||
var singleNoSearch = new Choices('#choices-single-no-search', {
|
var singleNoSearch = new Choices('#choices-single-no-search', {
|
||||||
|
@ -470,15 +740,22 @@
|
||||||
{ value: 'Two', label: 'Label Two', disabled: true },
|
{ value: 'Two', label: 'Label Two', disabled: true },
|
||||||
{ value: 'Three', label: 'Label Three' },
|
{ value: 'Three', label: 'Label Three' },
|
||||||
],
|
],
|
||||||
}).setChoices([
|
}).setChoices(
|
||||||
|
[
|
||||||
{ value: 'Four', label: 'Label Four', disabled: true },
|
{ value: 'Four', label: 'Label Four', disabled: true },
|
||||||
{ value: 'Five', label: 'Label Five' },
|
{ value: 'Five', label: 'Label Five' },
|
||||||
{ value: 'Six', label: 'Label Six', selected: true },
|
{ value: 'Six', label: 'Label Six', selected: true },
|
||||||
], 'value', 'label', false);
|
],
|
||||||
|
'value',
|
||||||
|
'label',
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
|
||||||
var singlePresetOpts = new Choices('#choices-single-preset-options', {
|
var singlePresetOpts = new Choices('#choices-single-preset-options', {
|
||||||
placeholder: true,
|
placeholder: true,
|
||||||
}).setChoices([{
|
}).setChoices(
|
||||||
|
[
|
||||||
|
{
|
||||||
label: 'Group one',
|
label: 'Group one',
|
||||||
id: 1,
|
id: 1,
|
||||||
disabled: false,
|
disabled: false,
|
||||||
|
@ -486,7 +763,7 @@
|
||||||
{ value: 'Child One', label: 'Child One', selected: true },
|
{ value: 'Child One', label: 'Child One', selected: true },
|
||||||
{ value: 'Child Two', label: 'Child Two', disabled: true },
|
{ value: 'Child Two', label: 'Child Two', disabled: true },
|
||||||
{ value: 'Child Three', label: 'Child Three' },
|
{ value: 'Child Three', label: 'Child Three' },
|
||||||
]
|
],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: 'Group two',
|
label: 'Group two',
|
||||||
|
@ -496,8 +773,12 @@
|
||||||
{ value: 'Child Four', label: 'Child Four', disabled: true },
|
{ value: 'Child Four', label: 'Child Four', disabled: true },
|
||||||
{ value: 'Child Five', label: 'Child Five' },
|
{ value: 'Child Five', label: 'Child Five' },
|
||||||
{ value: 'Child Six', label: 'Child Six' },
|
{ value: 'Child Six', label: 'Child Six' },
|
||||||
]
|
],
|
||||||
}], 'value', 'label');
|
},
|
||||||
|
],
|
||||||
|
'value',
|
||||||
|
'label',
|
||||||
|
);
|
||||||
|
|
||||||
var singleSelectedOpt = new Choices('#choices-single-selected-option', {
|
var singleSelectedOpt = new Choices('#choices-single-selected-option', {
|
||||||
searchFields: ['label', 'value', 'customProperties.description'],
|
searchFields: ['label', 'value', 'customProperties.description'],
|
||||||
|
@ -505,23 +786,30 @@
|
||||||
{ value: 'One', label: 'Label One', selected: true },
|
{ value: 'One', label: 'Label One', selected: true },
|
||||||
{ value: 'Two', label: 'Label Two', disabled: true },
|
{ value: 'Two', label: 'Label Two', disabled: true },
|
||||||
{
|
{
|
||||||
value: 'Three', label: 'Label Three', customProperties: {
|
value: 'Three',
|
||||||
description: 'This option is fantastic'
|
label: 'Label Three',
|
||||||
}
|
customProperties: {
|
||||||
|
description: 'This option is fantastic',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
}).setChoiceByValue('Two');
|
}).setChoiceByValue('Two');
|
||||||
|
|
||||||
var customChoicesPropertiesViaDataAttributes = new Choices('#choices-with-custom-props-via-html', {
|
var customChoicesPropertiesViaDataAttributes = new Choices(
|
||||||
searchFields: ['label', 'value', 'customProperties']
|
'#choices-with-custom-props-via-html',
|
||||||
})
|
{
|
||||||
|
searchFields: ['label', 'value', 'customProperties'],
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
var singleNoSorting = new Choices('#choices-single-no-sorting', {
|
var singleNoSorting = new Choices('#choices-single-no-sorting', {
|
||||||
shouldSort: false,
|
shouldSort: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
var cities = new Choices(document.getElementById('cities'));
|
var cities = new Choices(document.getElementById('cities'));
|
||||||
var tubeStations = new Choices(document.getElementById('tube-stations')).disable();
|
var tubeStations = new Choices(
|
||||||
|
document.getElementById('tube-stations'),
|
||||||
|
).disable();
|
||||||
|
|
||||||
cities.passedElement.element.addEventListener('change', function(e) {
|
cities.passedElement.element.addEventListener('change', function(e) {
|
||||||
if (e.detail.value === 'London') {
|
if (e.detail.value === 'London') {
|
||||||
|
@ -531,43 +819,96 @@
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
var customTemplates = new Choices(document.getElementById('choices-single-custom-templates'), {
|
var customTemplates = new Choices(
|
||||||
|
document.getElementById('choices-single-custom-templates'),
|
||||||
|
{
|
||||||
callbackOnCreateTemplates: function(strToEl) {
|
callbackOnCreateTemplates: function(strToEl) {
|
||||||
var classNames = this.config.classNames;
|
var classNames = this.config.classNames;
|
||||||
var itemSelectText = this.config.itemSelectText;
|
var itemSelectText = this.config.itemSelectText;
|
||||||
return {
|
return {
|
||||||
item: function(classNames, data) {
|
item: function(classNames, data) {
|
||||||
return strToEl('\
|
return strToEl(
|
||||||
|
'\
|
||||||
<div\
|
<div\
|
||||||
class="'+ String(classNames.item) + ' ' + String(data.highlighted ? classNames.highlightedState : classNames.itemSelectable) + '"\
|
class="' +
|
||||||
|
String(classNames.item) +
|
||||||
|
' ' +
|
||||||
|
String(
|
||||||
|
data.highlighted
|
||||||
|
? classNames.highlightedState
|
||||||
|
: classNames.itemSelectable,
|
||||||
|
) +
|
||||||
|
'"\
|
||||||
data-item\
|
data-item\
|
||||||
data-id="'+ String(data.id) + '"\
|
data-id="' +
|
||||||
data-value="'+ String(data.value) + '"\
|
String(data.id) +
|
||||||
'+ String(data.active ? 'aria-selected="true"' : '') + '\
|
'"\
|
||||||
'+ String(data.disabled ? 'aria-disabled="true"' : '') + '\
|
data-value="' +
|
||||||
|
String(data.value) +
|
||||||
|
'"\
|
||||||
|
' +
|
||||||
|
String(data.active ? 'aria-selected="true"' : '') +
|
||||||
|
'\
|
||||||
|
' +
|
||||||
|
String(data.disabled ? 'aria-disabled="true"' : '') +
|
||||||
|
'\
|
||||||
>\
|
>\
|
||||||
<span style="margin-right:10px;">🎉</span> ' + String(data.label) + '\
|
<span style="margin-right:10px;">🎉</span> ' +
|
||||||
|
String(data.label) +
|
||||||
|
'\
|
||||||
</div>\
|
</div>\
|
||||||
');
|
',
|
||||||
|
);
|
||||||
},
|
},
|
||||||
choice: function(classNames, data) {
|
choice: function(classNames, data) {
|
||||||
return strToEl('\
|
return strToEl(
|
||||||
|
'\
|
||||||
<div\
|
<div\
|
||||||
class="'+ String(classNames.item) + ' ' + String(classNames.itemChoice) + ' ' + String(data.disabled ? classNames.itemDisabled : classNames.itemSelectable) + '"\
|
class="' +
|
||||||
data-select-text="'+ String(itemSelectText) + '"\
|
String(classNames.item) +
|
||||||
|
' ' +
|
||||||
|
String(classNames.itemChoice) +
|
||||||
|
' ' +
|
||||||
|
String(
|
||||||
|
data.disabled
|
||||||
|
? classNames.itemDisabled
|
||||||
|
: classNames.itemSelectable,
|
||||||
|
) +
|
||||||
|
'"\
|
||||||
|
data-select-text="' +
|
||||||
|
String(itemSelectText) +
|
||||||
|
'"\
|
||||||
data-choice \
|
data-choice \
|
||||||
'+ String(data.disabled ? 'data-choice-disabled aria-disabled="true"' : 'data-choice-selectable') + '\
|
' +
|
||||||
data-id="'+ String(data.id) + '"\
|
String(
|
||||||
data-value="'+ String(data.value) + '"\
|
data.disabled
|
||||||
'+ String(data.groupId > 0 ? 'role="treeitem"' : 'role="option"') + '\
|
? 'data-choice-disabled aria-disabled="true"'
|
||||||
|
: 'data-choice-selectable',
|
||||||
|
) +
|
||||||
|
'\
|
||||||
|
data-id="' +
|
||||||
|
String(data.id) +
|
||||||
|
'"\
|
||||||
|
data-value="' +
|
||||||
|
String(data.value) +
|
||||||
|
'"\
|
||||||
|
' +
|
||||||
|
String(
|
||||||
|
data.groupId > 0 ? 'role="treeitem"' : 'role="option"',
|
||||||
|
) +
|
||||||
|
'\
|
||||||
>\
|
>\
|
||||||
<span style="margin-right:10px;">👉🏽</span> ' + String(data.label) + '\
|
<span style="margin-right:10px;">👉🏽</span> ' +
|
||||||
|
String(data.label) +
|
||||||
|
'\
|
||||||
</div>\
|
</div>\
|
||||||
');
|
',
|
||||||
|
);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
},
|
||||||
});
|
},
|
||||||
|
);
|
||||||
|
|
||||||
var resetSimple = new Choices(document.getElementById('reset-simple'));
|
var resetSimple = new Choices(document.getElementById('reset-simple'));
|
||||||
|
|
||||||
|
@ -579,12 +920,16 @@
|
||||||
|
|
||||||
<!-- Google Analytics - Ignore me -->
|
<!-- Google Analytics - Ignore me -->
|
||||||
<script>
|
<script>
|
||||||
window.ga = window.ga || function() { (ga.q = ga.q || []).push(arguments) }; ga.l = +new Date;
|
window.ga =
|
||||||
|
window.ga ||
|
||||||
|
function() {
|
||||||
|
(ga.q = ga.q || []).push(arguments);
|
||||||
|
};
|
||||||
|
ga.l = +new Date();
|
||||||
ga('create', 'UA-31575166-1', 'auto');
|
ga('create', 'UA-31575166-1', 'auto');
|
||||||
ga('send', 'pageview');
|
ga('send', 'pageview');
|
||||||
</script>
|
</script>
|
||||||
<script async src='https://www.google-analytics.com/analytics.js'></script>
|
<script async src="https://www.google-analytics.com/analytics.js"></script>
|
||||||
<!-- End Google Analytics -->
|
<!-- End Google Analytics -->
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -1,26 +1,56 @@
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
|
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8" />
|
||||||
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
|
<meta
|
||||||
|
name="viewport"
|
||||||
|
content="width=device-width,initial-scale=1,user-scalable=no"
|
||||||
|
/>
|
||||||
<title>Choices</title>
|
<title>Choices</title>
|
||||||
<meta name=description itemprop=description content="A lightweight, configurable select box/text input plugin. Similar to Select2 and Selectize but without the jQuery dependency.">
|
<meta
|
||||||
<link rel="apple-touch-icon" sizes="180x180" href="../assets/images/apple-touch-icon.png">
|
name="description"
|
||||||
<link rel="icon" type="image/png" href="../assets/images/favicon-32x32.png" sizes="32x32">
|
itemprop="description"
|
||||||
<link rel="icon" type="image/png" href="../assets/images/favicon-16x16.png" sizes="16x16">
|
content="A lightweight, configurable select box/text input plugin. Similar to Select2 and Selectize but without the jQuery dependency."
|
||||||
<link rel="manifest" href="../assets/images/manifest.json">
|
/>
|
||||||
<link rel="mask-icon" href="../assets/images/safari-pinned-tab.svg" color="#00bcd4">
|
<link
|
||||||
<link rel="shortcut icon" href="../assets/images/favicon.ico">
|
rel="apple-touch-icon"
|
||||||
<meta name="msapplication-config" content="../assets/images/browserconfig.xml">
|
sizes="180x180"
|
||||||
<meta name="theme-color" content="#ffffff">
|
href="../assets/images/apple-touch-icon.png"
|
||||||
|
/>
|
||||||
|
<link
|
||||||
|
rel="icon"
|
||||||
|
type="image/png"
|
||||||
|
href="../assets/images/favicon-32x32.png"
|
||||||
|
sizes="32x32"
|
||||||
|
/>
|
||||||
|
<link
|
||||||
|
rel="icon"
|
||||||
|
type="image/png"
|
||||||
|
href="../assets/images/favicon-16x16.png"
|
||||||
|
sizes="16x16"
|
||||||
|
/>
|
||||||
|
<link rel="manifest" href="../assets/images/manifest.json" />
|
||||||
|
<link
|
||||||
|
rel="mask-icon"
|
||||||
|
href="../assets/images/safari-pinned-tab.svg"
|
||||||
|
color="#00bcd4"
|
||||||
|
/>
|
||||||
|
<link rel="shortcut icon" href="../assets/images/favicon.ico" />
|
||||||
|
<meta
|
||||||
|
name="msapplication-config"
|
||||||
|
content="../assets/images/browserconfig.xml"
|
||||||
|
/>
|
||||||
|
<meta name="theme-color" content="#ffffff" />
|
||||||
|
|
||||||
<!-- Ignore these -->
|
<!-- Ignore these -->
|
||||||
<link rel="stylesheet" href="../assets/styles/base.min.css?version=6.0.3">
|
<link rel="stylesheet" href="../assets/styles/base.min.css?version=6.0.3" />
|
||||||
<!-- End ignore these -->
|
<!-- End ignore these -->
|
||||||
|
|
||||||
<!-- Choices includes -->
|
<!-- Choices includes -->
|
||||||
<link rel="stylesheet" href="../assets/styles/choices.min.css?version=6.0.3">
|
<link
|
||||||
|
rel="stylesheet"
|
||||||
|
href="../assets/styles/choices.min.css?version=6.0.3"
|
||||||
|
/>
|
||||||
<script src="../assets/scripts/choices.min.js?version=6.0.3"></script>
|
<script src="../assets/scripts/choices.min.js?version=6.0.3"></script>
|
||||||
<!-- End Choices includes -->
|
<!-- End Choices includes -->
|
||||||
</head>
|
</head>
|
||||||
|
@ -31,63 +61,76 @@
|
||||||
<h2>Text inputs</h2>
|
<h2>Text inputs</h2>
|
||||||
<div data-test-hook="basic">
|
<div data-test-hook="basic">
|
||||||
<label for="choices-basic">Basic</label>
|
<label for="choices-basic">Basic</label>
|
||||||
<input class="form-control" id="choices-basic" type="text">
|
<input class="form-control" id="choices-basic" type="text" />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div data-test-hook="edit-items">
|
<div data-test-hook="edit-items">
|
||||||
<label for="choices-edit-items">Edit items</label>
|
<label for="choices-edit-items">Edit items</label>
|
||||||
<input class="form-control" id="choices-edit-items" type="text">
|
<input class="form-control" id="choices-edit-items" type="text" />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div data-test-hook="remove-button">
|
<div data-test-hook="remove-button">
|
||||||
<label for="choices-remove-button">Remove button</label>
|
<label for="choices-remove-button">Remove button</label>
|
||||||
<input class="form-control" id="choices-remove-button" type="text">
|
<input class="form-control" id="choices-remove-button" type="text" />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div data-test-hook="unique-values">
|
<div data-test-hook="unique-values">
|
||||||
<label for="choices-unique-values">Unique values</label>
|
<label for="choices-unique-values">Unique values</label>
|
||||||
<input class="form-control" id="choices-unique-values" type="text">
|
<input class="form-control" id="choices-unique-values" type="text" />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div data-test-hook="input-limit">
|
<div data-test-hook="input-limit">
|
||||||
<label for="choices-input-limit">Input limit</label>
|
<label for="choices-input-limit">Input limit</label>
|
||||||
<input class="form-control" id="choices-input-limit" type="text">
|
<input class="form-control" id="choices-input-limit" type="text" />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div data-test-hook="add-item-filter">
|
<div data-test-hook="add-item-filter">
|
||||||
<label for="choices-add-item-filter">Add item filter</label>
|
<label for="choices-add-item-filter">Add item filter</label>
|
||||||
<input class="form-control" id="choices-add-item-filter" type="text">
|
<input
|
||||||
|
class="form-control"
|
||||||
|
id="choices-add-item-filter"
|
||||||
|
type="text"
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div data-test-hook="adding-items-disabled">
|
<div data-test-hook="adding-items-disabled">
|
||||||
<label for="choices-adding-items-disabled">Add items disabled</label>
|
<label for="choices-adding-items-disabled">Add items disabled</label>
|
||||||
<input class="form-control" id="choices-adding-items-disabled" type="text">
|
<input
|
||||||
|
class="form-control"
|
||||||
|
id="choices-adding-items-disabled"
|
||||||
|
type="text"
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div data-test-hook="disabled-via-attr">
|
<div data-test-hook="disabled-via-attr">
|
||||||
<label for="choices-disabled-via-attr">Disabled via attribute</label>
|
<label for="choices-disabled-via-attr">Disabled via attribute</label>
|
||||||
<input class="form-control" id="choices-disabled-via-attr" type="text" disabled>
|
<input
|
||||||
|
class="form-control"
|
||||||
|
id="choices-disabled-via-attr"
|
||||||
|
type="text"
|
||||||
|
disabled
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div data-test-hook="prepend-append">
|
<div data-test-hook="prepend-append">
|
||||||
<label for="choices-prepend-append">Prepend/append</label>
|
<label for="choices-prepend-append">Prepend/append</label>
|
||||||
<input class="form-control" id="choices-prepend-append" type="text">
|
<input class="form-control" id="choices-prepend-append" type="text" />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div data-test-hook="prepopulated">
|
<div data-test-hook="prepopulated">
|
||||||
<label for="choices-prepopulated">Pre-populated choices</label>
|
<label for="choices-prepopulated">Pre-populated choices</label>
|
||||||
<input class="form-control" id="choices-prepopulated" type="text">
|
<input class="form-control" id="choices-prepopulated" type="text" />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div data-test-hook="placeholder">
|
<div data-test-hook="placeholder">
|
||||||
<label for="choices-placeholder">Placeholder</label>
|
<label for="choices-placeholder">Placeholder</label>
|
||||||
<input class="form-control" id="choices-placeholder" type="text">
|
<input class="form-control" id="choices-placeholder" type="text" />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div data-test-hook="within-form">
|
<div data-test-hook="within-form">
|
||||||
<form>
|
<form>
|
||||||
<label for="choices-within-form">Within form</label>
|
<label for="choices-within-form">Within form</label>
|
||||||
<input class="form-control" id="choices-within-form" type="text">
|
<input class="form-control" id="choices-within-form" type="text" />
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -114,8 +157,8 @@
|
||||||
|
|
||||||
new Choices('#choices-add-item-filter', {
|
new Choices('#choices-add-item-filter', {
|
||||||
addItems: true,
|
addItems: true,
|
||||||
addItemFilterFn: (value) => {
|
addItemFilter: value => {
|
||||||
const regex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
|
const regex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
|
||||||
const expression = new RegExp(regex.source, 'i');
|
const expression = new RegExp(regex.source, 'i');
|
||||||
return expression.test(value);
|
return expression.test(value);
|
||||||
},
|
},
|
||||||
|
@ -133,13 +176,16 @@
|
||||||
});
|
});
|
||||||
|
|
||||||
new Choices('#choices-prepopulated', {
|
new Choices('#choices-prepopulated', {
|
||||||
items: ['Josh Johnson', {
|
items: [
|
||||||
|
'Josh Johnson',
|
||||||
|
{
|
||||||
value: 'joe@bloggs.co.uk',
|
value: 'joe@bloggs.co.uk',
|
||||||
label: 'Joe Bloggs',
|
label: 'Joe Bloggs',
|
||||||
customProperties: {
|
customProperties: {
|
||||||
description: 'Joe Blogg is such a generic name',
|
description: 'Joe Blogg is such a generic name',
|
||||||
}
|
},
|
||||||
}],
|
},
|
||||||
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
new Choices('#choices-placeholder', {
|
new Choices('#choices-placeholder', {
|
||||||
|
|
|
@ -79,6 +79,18 @@ class Choices {
|
||||||
{ arrayMerge: (destinationArray, sourceArray) => [...sourceArray] },
|
{ arrayMerge: (destinationArray, sourceArray) => [...sourceArray] },
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Convert addItemFilter to function
|
||||||
|
if (
|
||||||
|
userConfig.addItemFilter &&
|
||||||
|
typeof userConfig.addItemFilter !== 'function'
|
||||||
|
) {
|
||||||
|
const re =
|
||||||
|
userConfig.addItemFilter instanceof RegExp
|
||||||
|
? userConfig.addItemFilter
|
||||||
|
: new RegExp(userConfig.addItemFilter);
|
||||||
|
this.config.addItemFilter = re.test.bind(re);
|
||||||
|
}
|
||||||
|
|
||||||
const invalidConfigOptions = diff(this.config, DEFAULT_CONFIG);
|
const invalidConfigOptions = diff(this.config, DEFAULT_CONFIG);
|
||||||
if (invalidConfigOptions.length) {
|
if (invalidConfigOptions.length) {
|
||||||
console.warn(
|
console.warn(
|
||||||
|
@ -1026,11 +1038,12 @@ class Choices {
|
||||||
this._isTextElement &&
|
this._isTextElement &&
|
||||||
this.config.addItems &&
|
this.config.addItems &&
|
||||||
canAddItem &&
|
canAddItem &&
|
||||||
isType('Function', this.config.addItemFilterFn) &&
|
typeof this.config.addItemFilter === 'function' &&
|
||||||
!this.config.addItemFilterFn(value)
|
!this.config.addItemFilter(value)
|
||||||
) {
|
) {
|
||||||
canAddItem = false;
|
canAddItem = false;
|
||||||
notice = isType('Function', this.config.customAddItemText)
|
notice =
|
||||||
|
typeof this.config.customAddItemText === 'function'
|
||||||
? this.config.customAddItemText(value)
|
? this.config.customAddItemText(value)
|
||||||
: this.config.customAddItemText;
|
: this.config.customAddItemText;
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,7 +35,7 @@ export const DEFAULT_CONFIG = {
|
||||||
renderChoiceLimit: -1,
|
renderChoiceLimit: -1,
|
||||||
maxItemCount: -1,
|
maxItemCount: -1,
|
||||||
addItems: true,
|
addItems: true,
|
||||||
addItemFilterFn: null,
|
addItemFilter: null,
|
||||||
removeItems: true,
|
removeItems: true,
|
||||||
removeItemButton: false,
|
removeItemButton: false,
|
||||||
editItems: false,
|
editItems: false,
|
||||||
|
|
|
@ -55,7 +55,7 @@ describe('constants', () => {
|
||||||
expect(DEFAULT_CONFIG.renderChoiceLimit).to.be.a('number');
|
expect(DEFAULT_CONFIG.renderChoiceLimit).to.be.a('number');
|
||||||
expect(DEFAULT_CONFIG.maxItemCount).to.be.a('number');
|
expect(DEFAULT_CONFIG.maxItemCount).to.be.a('number');
|
||||||
expect(DEFAULT_CONFIG.addItems).to.be.a('boolean');
|
expect(DEFAULT_CONFIG.addItems).to.be.a('boolean');
|
||||||
expect(DEFAULT_CONFIG.addItemFilterFn).to.equal(null);
|
expect(DEFAULT_CONFIG.addItemFilter).to.equal(null);
|
||||||
expect(DEFAULT_CONFIG.removeItems).to.be.a('boolean');
|
expect(DEFAULT_CONFIG.removeItems).to.be.a('boolean');
|
||||||
expect(DEFAULT_CONFIG.removeItemButton).to.be.a('boolean');
|
expect(DEFAULT_CONFIG.removeItemButton).to.be.a('boolean');
|
||||||
expect(DEFAULT_CONFIG.editItems).to.be.a('boolean');
|
expect(DEFAULT_CONFIG.editItems).to.be.a('boolean');
|
||||||
|
|
3
types/index.d.ts
vendored
3
types/index.d.ts
vendored
|
@ -18,6 +18,7 @@ declare namespace Choices {
|
||||||
type stringFunction = () => string;
|
type stringFunction = () => string;
|
||||||
type noticeStringFunction = (value: string) => string;
|
type noticeStringFunction = (value: string) => string;
|
||||||
type noticeLimitFunction = (maxItemCount: number) => string;
|
type noticeLimitFunction = (maxItemCount: number) => string;
|
||||||
|
type filterFunction = (value: string) => boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Choice {
|
interface Choice {
|
||||||
|
@ -403,7 +404,7 @@ declare namespace Choices {
|
||||||
*
|
*
|
||||||
* @default null
|
* @default null
|
||||||
*/
|
*/
|
||||||
addItemFilterFn: (value: string) => boolean;
|
addItemFilter: string | RegExp | Choices.Types.filterFunction;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The text that is shown when a user has inputted a new item but has not pressed the enter key. To access the current input value, pass a function with a `value` argument (see the **default config** [https://github.com/jshjohnson/Choices#setup] for an example), otherwise pass a string.
|
* The text that is shown when a user has inputted a new item but has not pressed the enter key. To access the current input value, pass a function with a `value` argument (see the **default config** [https://github.com/jshjohnson/Choices#setup] for an example), otherwise pass a string.
|
||||||
|
|
Loading…
Reference in a new issue