Skip to content

Commit 4d2ff10

Browse files
authored
Merge pull request #3 from adidas/fix/form-capability
Fix form capability
2 parents 6aaa0dd + 5386378 commit 4d2ff10

File tree

10 files changed

+266
-269
lines changed

10 files changed

+266
-269
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
# 1.1.0
2+
3+
- Fixed component to be able to work within a `<form/>` element.
4+
- Fixed `getValue` public method.
5+
- Fixed `placeholder` property when it is used as element attribute.
6+
- Updated dev and public examples:
7+
- Wrap select elements in a form to get their values.
8+
- Show the values of the select elements.
9+
- Updated logo.
10+
111
# 1.0.0
212

313
- Initial version of `choicesjs-stencil` web component.

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,15 @@ Include the component in your application via HTML script:
4545
Use it as a new HTML element in your template:
4646

4747
```html
48-
<choicesjs-stencil values="foo, bar"></choicesjs-stencil>
48+
<choicesjs-stencil values="foo,bar"></choicesjs-stencil>
49+
```
50+
51+
To be able to work with the component in a form it needs a name:
52+
53+
```html
54+
<form name="form">
55+
<choicesjs-stencil name="select" values="foo,bar"></choicesjs-stencil>
56+
</form>
4957
```
5058

5159
The component is also available as module to be loaded via module bundler:

index.html

Lines changed: 67 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,17 @@
1616
body {
1717
padding-bottom: 4rem;
1818
}
19-
choicesjs-stencil {
19+
.select-container {
20+
display: inline-flex;
21+
}
22+
.select-container .select {
2023
position: relative;
2124
width: 100%;
2225
height: auto;
2326
}
27+
.select-container .btn {
28+
margin-left: 1rem;
29+
}
2430
.container-fluid {
2531
margin-bottom: 1rem;
2632
}
@@ -46,6 +52,17 @@
4652
.navbar {
4753
min-height: auto;
4854
}
55+
.values-container {
56+
background-color: #f9f9f9;
57+
border-radius: 4px;
58+
font-size: .85rem;
59+
padding: .5rem;
60+
margin-bottom: .5rem;
61+
}
62+
.values-container .values-container__text {
63+
word-break: break-all;
64+
margin: 0;
65+
}
4966
</style>
5067
<style id="customStyles"></style>
5168
</head>
@@ -55,15 +72,19 @@
5572
<h2 class="col-sm-12">ChoicesJS Web Component: test page</h2>
5673
</div>
5774
<div class="row">
58-
<div class="col-sm-12">
59-
<choicesjs-stencil name="choicesjs-stencil"></choicesjs-stencil>
60-
</div>
75+
<form class="col-sm-12 select-container" name="choicesjs-stencil-form">
76+
<choicesjs-stencil name="select" class="select"></choicesjs-stencil>
77+
<button class="btn btn-primary" type="submit">Submit</button>
78+
</form>
6179
</div>
6280
</div>
6381
<div class="container-fluid control-panel">
64-
<form id="selectOptionsForm">
82+
<form name="select-options-form">
6583
<div class="row">
6684
<div class="col-md-12">
85+
<div class="pull-left values-container">
86+
<p class="values-container__text"></p>
87+
</div>
6788
<div class="form-group pull-right">
6889
<button class="btn btn-primary" type="submit">Apply</button>
6990
<button class="btn btn-default" type="reset">Reset</button>
@@ -514,7 +535,7 @@ <h2 class="col-sm-12">ChoicesJS Web Component: test page</h2>
514535
<h4 class="modal-title">Custom Styles</h4>
515536
</div>
516537
<div class="modal-body">
517-
<form id="customStylesForm">
538+
<form name="custom-styles-form">
518539
<div class="form-group">
519540
<div class="row">
520541
<div class="col-md-12">
@@ -583,7 +604,7 @@ <h4 class="modal-title">Search options</h4>
583604
</div>
584605
<div class="modal-body">
585606
<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>
586-
<form id="customSearchForm">
607+
<form name="custom-search-form">
587608
<div class="form-group">
588609
<div class="row">
589610
<div class="col-md-6">
@@ -829,26 +850,58 @@ <h5 class="text-center pad-bottom">See full Fuse documentation in <a href="https
829850
}
830851

831852
function toggleTypeDependentSections(type) {
832-
typeDependencies.forEach(function(element) {
853+
Array.from(typeDependencies).forEach(function(element) {
833854
element.classList.contains('type--' + type) ?
834855
element.classList.remove('hidden') :
835856
element.classList.add('hidden');
836857
});
837858
}
838859

860+
function showValues(event) {
861+
var valuesTextContainer = document.querySelector('.values-container p');
862+
var form = new FormData(this);
863+
var values = [];
864+
var i, lng;
865+
866+
// forEach is not available in all browsers
867+
if (typeof form.forEach === 'function') {
868+
form.forEach(function(value, name) {
869+
values.push(name + ': `' + value + '`') ;
870+
});
871+
} else {
872+
for (i = 0, lng = this.length; i < lng; i++) {
873+
if (this[i].value) {
874+
values.push(this[i].name + ': `' + this[i].value + '`');
875+
}
876+
}
877+
}
878+
879+
valuesTextContainer.innerHTML = values.join(' | ');
880+
console.log('values', values);
881+
882+
event.preventDefault();
883+
event.stopImmediatePropagation();
884+
}
885+
886+
function resetValues() {
887+
document.querySelector('.values-container p').innerHTML = '';
888+
}
889+
839890
function handleChangeOptions(event) {
840891
var form = event.target;
841892

842893
event.preventDefault();
843894
event.stopImmediatePropagation();
844895

845896
changeConfiguration(form);
897+
resetValues();
846898
}
847899

848900
function handleResetOptions() {
849901
var form = event.target;
850902

851903
toggleTypeDependentSections('text');
904+
resetValues();
852905

853906
// delay configuration changes until form sections are ready
854907
setTimeout(function() {
@@ -878,9 +931,11 @@ <h5 class="text-center pad-bottom">See full Fuse documentation in <a href="https
878931
'addItem', 'removeItem', 'highlightItem', 'unhighlightItem',
879932
'choice', 'change', 'search', 'showDropdown', 'hideDropdown'
880933
];
881-
var selectOptionsForm = document.querySelector('#selectOptionsForm');
882-
var customStylesForm = document.querySelector('#customStylesForm');
883-
var customSearchForm = document.querySelector('#customSearchForm');
934+
935+
var choicesjsStencilForm = document.querySelector('form[name=choicesjs-stencil-form]');
936+
var selectOptionsForm = document.querySelector('form[name=select-options-form]');
937+
var customStylesForm = document.querySelector('form[name=custom-styles-form]');
938+
var customSearchForm = document.querySelector('form[name=custom-search-form]');
884939
var customStyles = document.querySelector('#customStyles');
885940
var typeSelect = document.querySelector('select[name="type"]');
886941
var select = document.querySelector('choicesjs-stencil');
@@ -900,6 +955,7 @@ <h5 class="text-center pad-bottom">See full Fuse documentation in <a href="https
900955
});
901956
});
902957

958+
choicesjsStencilForm.onsubmit = showValues;
903959
selectOptionsForm.onsubmit = handleChangeOptions;
904960
selectOptionsForm.onreset = handleResetOptions;
905961
customStylesForm.onsubmit = handleChangeStyles;

logo.png

-673 Bytes
Loading

0 commit comments

Comments
 (0)