@tarojs/components
Version:
Taro 组件库。
129 lines (126 loc) • 3.52 kB
JavaScript
import { r as registerInstance, c as createEvent, h, g as getElement } from './index-5bd7cbab.js';
let Form = class {
constructor(hostRef) {
registerInstance(this, hostRef);
this.onSubmit = createEvent(this, "submit", 7);
this.value = {};
}
watchSlotParent(newParent) {
if (!this.orginalAppendChild) {
this.orginalAppendChild = this.el.appendChild;
this.orginalInsertBefore = this.el.insertBefore;
this.orginalReplaceChild = this.el.replaceChild;
this.orginalRemoveChild = this.el.removeChild;
}
if (!newParent) {
this.el.appendChild = this.orginalAppendChild;
this.el.insertBefore = this.orginalInsertBefore;
this.el.replaceChild = this.orginalReplaceChild;
this.el.removeChild = this.orginalRemoveChild;
return;
}
this.el.appendChild = (newChild) => {
return newParent.appendChild(newChild);
};
this.el.insertBefore = (newChild, refChild) => {
return newParent.insertBefore(newChild, refChild);
};
this.el.replaceChild = (newChild, oldChild) => {
return newParent.replaceChild(newChild, oldChild);
};
this.el.removeChild = (oldChild) => {
return newParent.removeChild(oldChild);
};
}
onButtonSubmit(e) {
e.stopPropagation();
this.value = this.getFormValue();
this.onSubmit.emit({
value: this.value
});
}
onButtonReset(e) {
e.stopPropagation();
this.form.reset();
}
componentDidLoad() {
this.value = this.getFormValue();
Object.defineProperty(this.el, 'value', {
get: () => this.value,
configurable: true
});
}
componentDidRender() {
this.setSlotParent(this.form);
}
getFormValue() {
const el = this.el;
const elements = [];
const tagElements = el.getElementsByTagName('input');
for (let j = 0; j < tagElements.length; j++) {
elements.push(tagElements[j]);
}
const formItem = {};
const hash = {};
elements.forEach(item => {
if (item.className.indexOf('weui-switch') !== -1) {
formItem[item.name] = item.checked;
return;
}
if (item.type === 'radio') {
if (item.checked) {
hash[item.name] = true;
formItem[item.name] = item.value;
}
else {
if (!hash[item.name]) {
formItem[item.name] = '';
}
}
return;
}
if (item.type === 'checkbox') {
if (item.checked) {
if (hash[item.name]) {
formItem[item.name].push(item.value);
}
else {
hash[item.name] = true;
formItem[item.name] = [item.value];
}
}
else {
if (!hash[item.name]) {
formItem[item.name] = [];
}
}
return;
}
formItem[item.name] = item.value;
});
const textareaElements = el.getElementsByTagName('textarea');
const textareaEleArr = [];
for (let i = 0; i < textareaElements.length; i++) {
textareaEleArr.push(textareaElements[i]);
}
textareaEleArr.forEach(v => {
formItem[v.name] = v.value;
});
return formItem;
}
setSlotParent(el) {
this.slotParent = el;
}
render() {
return (h("form", { ref: dom => {
if (dom) {
this.form = dom;
}
} }, h("slot", null)));
}
get el() { return getElement(this); }
static get watchers() { return {
"slotParent": ["watchSlotParent"]
}; }
};
export { Form as taro_form_core };