2018-11-14 14:51:39 -05:00
|
|
|
function isCheckableType(type) {
|
|
|
|
return /^(radio|checkbox)$/.test(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setInputValue(element, value) {
|
|
|
|
const { type } = element;
|
|
|
|
let eventType;
|
|
|
|
|
|
|
|
if (isCheckableType(type)) {
|
|
|
|
element.checked = !element.checked;
|
|
|
|
eventType = 'change';
|
|
|
|
} else {
|
|
|
|
element.value = value;
|
|
|
|
eventType = 'input';
|
|
|
|
}
|
2018-10-15 09:36:19 -04:00
|
|
|
|
|
|
|
element.dispatchEvent(
|
2018-11-14 14:51:39 -05:00
|
|
|
new Event(eventType, {
|
2018-10-15 09:36:19 -04:00
|
|
|
bubbles: true,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-11-14 14:51:39 -05:00
|
|
|
export function getInputValue(input) {
|
|
|
|
return isCheckableType(input.type) ? input.checked : input.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function createForm(type = 'text') {
|
2018-10-15 09:36:19 -04:00
|
|
|
const form = document.createElement('form');
|
|
|
|
form.innerHTML = `
|
2018-11-14 14:51:39 -05:00
|
|
|
<input type="${type}" name="${type}" class="js-input"/>
|
2018-10-15 09:36:19 -04:00
|
|
|
<button type="submit" class="js-dirty-submit"></button>
|
|
|
|
`;
|
2018-11-14 14:51:39 -05:00
|
|
|
|
2018-10-15 09:36:19 -04:00
|
|
|
const input = form.querySelector('.js-input');
|
|
|
|
const submit = form.querySelector('.js-dirty-submit');
|
|
|
|
|
|
|
|
return {
|
|
|
|
form,
|
|
|
|
input,
|
|
|
|
submit,
|
|
|
|
};
|
|
|
|
}
|