tsp-component
Version:
提供多端和react版本的UI组件
73 lines (72 loc) • 2.69 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
import * as React from 'react';
import Toast from '../toast';
import { unique } from '../util/tools';
import { deviceHeight } from '../util/device';
import { validatePattern, validateRequried } from '../util/validate';
var Form = (function (_super) {
__extends(Form, _super);
function Form(props, state) {
var _this = _super.call(this, props, state) || this;
_this.onSubmit = _this.onSubmit.bind(_this);
_this.locationError = _this.locationError.bind(_this);
return _this;
}
Form.prototype.onSubmit = function (e) {
e.preventDefault();
var fields = unique(this.props.fields);
var prefix = this.props.prefix;
var value = {};
var flag = true;
for (var i = 0; i < fields.length; i++) {
var formControlElem = document.getElementById(prefix + fields[i]);
if (!formControlElem) {
continue;
}
if (!validateRequried(formControlElem, this.locationError)) {
flag = false;
break;
}
if (!validatePattern(formControlElem, this.locationError)) {
flag = false;
break;
}
value[fields[i]] = formControlElem.dataset.value;
}
if (flag) {
this.props.onSubmit(value);
}
};
Form.prototype.locationError = function (msg, formControlElem) {
var y = formControlElem.offsetTop - deviceHeight / 2;
var top = y - this.props.top;
if (this.props.container) {
this.props.container.scrollLeft = top;
}
else {
window.scrollTo(0, top);
}
Toast.warning(msg);
};
Form.prototype.render = function () {
return (React.createElement("form", { className: this.props.className, onSubmit: this.onSubmit, ref: "form" }, this.props.children));
};
Form.defaultProps = {
prefix: '',
top: 0,
className: '',
onSubmit: undefined,
fields: undefined
};
return Form;
}(React.Component));
export default Form;