@tarojs/components
Version:
Taro 组件库。
160 lines (159 loc) • 4.27 kB
JavaScript
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { Component, h, Event, Element, Listen, State, Watch } from '@stencil/core';
export class Form {
constructor() {
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)));
}
static get is() { return "taro-form-core"; }
static get states() { return {
"slotParent": {}
}; }
static get events() { return [{
"method": "onSubmit",
"name": "submit",
"bubbles": true,
"cancelable": true,
"composed": true,
"docs": {
"tags": [],
"text": ""
},
"complexType": {
"original": "any",
"resolved": "any",
"references": {}
}
}]; }
static get elementRef() { return "el"; }
static get watchers() { return [{
"propName": "slotParent",
"methodName": "watchSlotParent"
}]; }
static get listeners() { return [{
"name": "tarobuttonsubmit",
"method": "onButtonSubmit",
"target": undefined,
"capture": false,
"passive": false
}, {
"name": "tarobuttonreset",
"method": "onButtonReset",
"target": undefined,
"capture": false,
"passive": false
}]; }
}