Predefine values + limiting + updating hidden input values

This commit is contained in:
Josh Johnson 2016-03-16 23:15:03 +00:00
parent 19c429bca8
commit 8ea38d8d6b
3 changed files with 58 additions and 37 deletions

File diff suppressed because one or more lines are too long

View file

@ -1,4 +1,4 @@
import { wrap, getSiblings } from './lib/utils.js'; import { wrap, getSiblings, isType } from './lib/utils.js';
(function (root, factory) { (function (root, factory) {
if (typeof define === 'function' && define.amd) { if (typeof define === 'function' && define.amd) {
@ -20,8 +20,8 @@ import { wrap, getSiblings } from './lib/utils.js';
const DEFAULT_OPTIONS = { const DEFAULT_OPTIONS = {
element: '[data-choice]', element: '[data-choice]',
disabled: false, disabled: false,
maxItems: 0, maxItems: 5,
debug: false, debug: true,
placeholder: false, placeholder: false,
callbackOnInit: function(){}, callbackOnInit: function(){},
callbackOnRender: function(){}, callbackOnRender: function(){},
@ -61,28 +61,45 @@ import { wrap, getSiblings } from './lib/utils.js';
} }
clearInput(el) {
if(el.value) el.value = '';
}
/* Event handling */ /* Event handling */
onKeyDown(e) { onKeyDown(e) {
// let input = // Handle enter key
console.log('Key down')
if(e.keyCode === 13 && e.target.value) { if(e.keyCode === 13 && e.target.value) {
this.addItem(e.target, e.target.value); let el = e.target;
e.target.value = ''; let value = el.value;
let list = e.target.parentNode.querySelector('.choice__list');
let handleEnterKey = () => {
this.addItem(el, value, list);
this.updateInputValue(el, value);
this.clearInput(el);
};
if(this.options.maxItems) {
if(this.options.maxItems > list.children.length) {
handleEnterKey();
}
} else {
handleEnterKey();
}
} }
} }
onFocus(e) { onFocus(e) {
console.log('Focus')
} }
onClick(e) { onClick(e) {
console.log('Click')
} }
onChange(e) { onChange(e) {
console.log('Change')
} }
/* Event listeners */ /* Event listeners */
@ -125,30 +142,28 @@ import { wrap, getSiblings } from './lib/utils.js';
} }
addItem(el, value) { updateInputValue(el, value) {
console.log('Add item'); if(this.options.debug) console.debug('Update input value');
let wrapper = el.parentNode; // Find hidden element
let valueInput = wrapper.querySelector('.choice__input--original'); let hiddenInput = el.parentNode.querySelector('.choice__input--hidden');
// If input already has values, parse the array, otherwise create a blank array
let list = wrapper.querySelector('.choice__list'); let valueArray = hiddenInput.value !== '' && isType('Array', JSON.parse(hiddenInput.value)) ? JSON.parse(hiddenInput.value) : [];
// Push new value to array
valueArray.push(value);
// Caste array to string and set it as the hidden inputs value
hiddenInput.value = JSON.stringify(valueArray);
}
addItem(el, value, list) {
if(this.options.debug) console.debug('Add item');
// Create new list element
let item = document.createElement('li'); let item = document.createElement('li');
item.classList.add('choice__item'); item.classList.add('choice__item');
item.textContent = value; item.textContent = value;
if(valueInput.value === '') { // Append it to list
valueInput.value = JSON.stringify([]); list.appendChild(item);
}
let valueInputArray = JSON.parse(valueInput.value);
valueInputArray.push(value);
valueInput.value = JSON.stringify(valueInputArray);
console.log(valueInput.value);
wrapper.appendChild(item);
} }
removeItem() { removeItem() {
@ -175,7 +190,7 @@ import { wrap, getSiblings } from './lib/utils.js';
} }
render(el) { render(el) {
console.log('Render'); if(this.options.debug) console.debug('Render');
let wrapper = document.createElement('div'); let wrapper = document.createElement('div');
let input = document.createElement('input'); let input = document.createElement('input');
@ -183,7 +198,7 @@ import { wrap, getSiblings } from './lib/utils.js';
wrapper.classList.add('choice', 'choice--active'); wrapper.classList.add('choice', 'choice--active');
el.classList.add('choice__input', 'choice__input--original'); el.classList.add('choice__input', 'choice__input--hidden');
el.tabIndex = '-1'; el.tabIndex = '-1';
el.setAttribute('style', 'display:none;'); el.setAttribute('style', 'display:none;');
el.setAttribute('aria-hidden', 'true'); el.setAttribute('aria-hidden', 'true');
@ -192,6 +207,13 @@ import { wrap, getSiblings } from './lib/utils.js';
list.classList.add('choice__list'); list.classList.add('choice__list');
if(el.value !== '') {
let valueArray = JSON.parse(el.value);
valueArray.map((v) => {
this.addItem(el, v, list);
});
}
input.type = 'text'; input.type = 'text';
input.classList.add('choice__input', 'choice__input--cloned'); input.classList.add('choice__input', 'choice__input--cloned');
@ -217,5 +239,4 @@ import { wrap, getSiblings } from './lib/utils.js';
var choices = new Choices(); var choices = new Choices();
choices.init(); choices.init();
console.log(choices);
}); });

View file

@ -5,8 +5,8 @@
<title>Choices</title> <title>Choices</title>
</head> </head>
<body> <body>
<input type="text" data-choice> <input id="1" type="text" data-choice value='["preset-1", "preset-2"]'>
<input type="text" data-choice> <input id="2" type="text" data-choice>
<script src="assets/scripts/dist/bundle.js"></script> <script src="assets/scripts/dist/bundle.js"></script>
</body> </body>
</html> </html>