Skip to content

Commit 4170773

Browse files
authored
Merge pull request #7 from adidas/feature/update-choicesjs
Update ChoicesJS library
2 parents 4eb380d + 2e7b1c9 commit 4170773

File tree

13 files changed

+462
-313
lines changed

13 files changed

+462
-313
lines changed

.babelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"plugins": [ "@babel/plugin-syntax-dynamic-import" ]
3+
}

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
# 1.3.0
2+
3+
- Updated StencilJS core to v0.18.1.
4+
- Updated ChoicesJS to v7.0.0.
5+
- `regexFilter` config has been replaced with `addItemFilterFn` function.
6+
- `toggleDropdown` method has been removed.
7+
- `clearChoices` method has been added.
8+
- Updated example with new options.
9+
- Updated documentation with example of framework integration.
10+
111
## 1.2.1
212

313
- Fixed Travis configuration to publish and create documentation on release tags.

README.md

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ The library is based on [ChoicesJS][choicesjs], but it is not bundle along with
2929
This dependency can be added to the bundle as external library, or it can just be added to the page via GitHub script:
3030

3131
```html
32-
<script src="https://rawgit.com/jshjohnson/Choices/v4.1.3/public/assets/scripts/choices.js"></script>
32+
<script src="https://rawgit.com/jshjohnson/Choices/v7.0.0/public/assets/scripts/choices.js"></script>
3333
```
3434

3535
## Installation and running
@@ -97,6 +97,64 @@ Some of this component properties must be set via JavaScript (non primitive type
9797
</script>
9898
```
9999

100+
### Framework integration
101+
102+
It is a good decision to create a wrapper of the Web Component to be ready to use by the application framework.
103+
104+
The wrapper has to cover at least:
105+
106+
- Properties:
107+
- `type`: type of selector (see values in configuration section).
108+
- `choices`: values to display in the selector.
109+
- `name?`: name of the element (recommended).
110+
- Other Web Component properties can be fixed or set dynamically.
111+
- Listeners:
112+
- Changes on `choices` property to update the values of `choices` in the Web Component:
113+
- `change` event of the Web Component to be able to propagate it.
114+
115+
#### Example for VueJS framework
116+
117+
```javascript
118+
// Loading ChoicesJS library and ChoicesJS Stencil Web Component
119+
import 'expose-loader?Choices!choices.js';
120+
import { defineCustomElements } from 'choicesjs-stencil/dist/loader';
121+
122+
defineCustomElements(window);
123+
124+
// VueJS component
125+
<template>
126+
<choicesjs-stencil class="choicesjs-stencil"
127+
:type="type"
128+
:name="name"
129+
...
130+
/>
131+
</template>
132+
133+
<script>
134+
export default {
135+
props: [ 'type', 'name', 'choices' ],
136+
watch: {
137+
choices: {
138+
immediate: true,
139+
handler(newChoices, oldChoices) {
140+
const select = this.$el;
141+
142+
if (select && oldChoices !== newChoices) {
143+
select.choices = newChoices;
144+
}
145+
}
146+
}
147+
},
148+
mounted() {
149+
const select = this.$el;
150+
151+
select.choices = this.choices;
152+
select.addEventListener('change', () => this.$emit('change'));
153+
}
154+
};
155+
</script>
156+
```
157+
100158
### Configuration
101159

102160
- `type`: the type of selector to render, defaults to `text`. Options: `text`, `single`, `multiple`.

index.html

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
77
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
88
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
9-
<script src="https://rawgit.com/jshjohnson/Choices/v4.1.3/public/assets/scripts/choices.js"></script>
9+
<script src="https://rawgit.com/jshjohnson/Choices/v7.0.0/public/assets/scripts/choices.js"></script>
1010
<script src="dist/choicesjsstencil.js"></script>
1111
<style>
1212
html {
@@ -335,18 +335,17 @@ <h2 class="col-sm-12">ChoicesJS Web Component: test page</h2>
335335
</span>
336336
<input type="text" class="form-control" name="searchFields" value="[&quot;label&quot;, &quot;value&quot;]">
337337
</div>
338-
<div class="form-group type type--text">
339-
<label for="regexFilter">
340-
Regex Filter
338+
<div class="form-group">
339+
<label for="addItemFilterFn">
340+
addItemFilterFn(value) {
341341
</label>
342-
<span class="description" data-toggle="tooltip" title="A filter that will need to pass for a user to successfully add an item.">
342+
<span class="description" data-toggle="tooltip" title="The function that will need to pass for a user to successfully add an item.">
343343
<span class="glyphicon glyphicon glyphicon-question-sign"></span>
344344
</span>
345-
<div class="input-group">
346-
<span class="input-group-addon">/</span>
347-
<input type="text" class="form-control" name="regexFilter">
348-
<span class="input-group-addon">/</span>
349-
</div>
345+
<textarea class="form-control textarea--vertical" name="addItemFilterFn" rows="2">
346+
return /^[^\s]+$/.test(value);
347+
</textarea>
348+
<label for="addItemFilterFn">}</label>
350349
</div>
351350
<div class="form-group type type--single type--multiple">
352351
<label for="position">
@@ -483,7 +482,9 @@ <h2 class="col-sm-12">ChoicesJS Web Component: test page</h2>
483482
<span class="description" data-toggle="tooltip" title="Function to run once Choices initialises.">
484483
<span class="glyphicon glyphicon glyphicon-question-sign"></span>
485484
</span>
486-
<textarea class="form-control textarea--vertical" name="callbackOnInit" rows="2"></textarea>
485+
<textarea class="form-control textarea--vertical" name="callbackOnInit" rows="2">
486+
console.info('ChoicesJS Stencil Web Component ready!');
487+
</textarea>
487488
<label for="callbackOnInit">}</label>
488489
</div>
489490
<div class="form-group">
@@ -493,10 +494,12 @@ <h2 class="col-sm-12">ChoicesJS Web Component: test page</h2>
493494
<span class="description" data-toggle="tooltip" title="Function to run on template creation. Through this callback it is possible to provide custom templates for the various components of Choices (see terminology). For Choices to work with custom templates, it is important you maintain the various data attributes defined in the reference link below.">
494495
<span class="glyphicon glyphicon glyphicon-question-sign"></span>
495496
</span>
496-
<textarea class="form-control textarea--vertical" name="callbackOnCreateTemplates" rows="2"></textarea>
497+
<textarea class="form-control textarea--vertical" name="callbackOnCreateTemplates" rows="2">
498+
return template;
499+
</textarea>
497500
<label for="callbackOnCreateTemplates">}</label>
498501
<br/>
499-
<a href="https://github.com/jshjohnson/Choices/blob/67f29c286aa21d88847adfcd6304dc7d068dc01f/assets/scripts/src/choices.js#L1993-L2067">
502+
<a href="https://github.com/jshjohnson/Choices/blob/v7.0.0/public/assets/scripts/choices.js#L713-L788" target="_blank">
500503
Templates reference
501504
</a>
502505
</div>
@@ -520,7 +523,11 @@ <h2 class="col-sm-12">ChoicesJS Web Component: test page</h2>
520523
<div class="container">
521524
<div class="row">
522525
<div class="col-md-12">
523-
<p class="text-center pad-top">See full documentation in <a href="https://www.npmjs.com/package/choices.js">choices.js</a> npm package.</p>
526+
<p class="text-center pad-top">
527+
See full documentation in
528+
<a href="https://www.npmjs.com/package/choices.js/v/7.0.0" target="_blank">choices.js</a>
529+
npm package.
530+
</p>
524531
</div>
525532
</div>
526533
</div>
@@ -603,7 +610,9 @@ <h4 class="modal-title">Custom Styles</h4>
603610
<h4 class="modal-title">Search options</h4>
604611
</div>
605612
<div class="modal-body">
606-
<h5 class="text-center pad-bottom">See full Fuse documentation in <a href="https://github.com/krisk/Fuse/tree/v2.7.4#options">GitHub</a>.</h5>
613+
<h5 class="text-center pad-bottom">
614+
See full Fuse documentation in <a href="https://fusejs.io/" target="_blank">GitHub</a>.
615+
</h5>
607616
<form name="custom-search-form">
608617
<div class="form-group">
609618
<div class="row">
@@ -789,7 +798,9 @@ <h5 class="text-center pad-bottom">See full Fuse documentation in <a href="https
789798
choices: jsonParser,
790799
items: jsonParser,
791800
searchFields: jsonParser,
792-
regexFilter: regexParser(),
801+
addItemFilterFn: function(value) {
802+
return new Function('value', value.trim());
803+
},
793804
renderChoiceLimit: Number,
794805
maxItemCount: Number,
795806
searchFloor: Number,

0 commit comments

Comments
 (0)