@activatortube/react-native-form
Version:
react native form
87 lines (86 loc) • 3.06 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
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 extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
/**
* 表单的基础组件Form
* 2019-09-06 21:44.
*
* @author WheelerLee https://github.com/WheelerLee
* @copyright 2019
*/
import React from 'react';
import { Component } from 'react';
var Form = /** @class */ (function (_super) {
__extends(Form, _super);
function Form(props) {
var _this = _super.call(this, props) || this;
_this.childrendRefs = [];
return _this;
}
Form.prototype.submit = function () {
var params = {};
for (var _i = 0, _a = this.childrendRefs; _i < _a.length; _i++) {
var x = _a[_i];
var name = x.props.name;
var child = x;
var value = child.value;
if (name && value) {
params[name] = value;
}
}
this.props.onSubmit && this.props.onSubmit(params);
return params;
};
/**
* 遍历所有的孩子节点,获取Form的组件
* @param cr 组件的孩子节点
*/
Form.prototype.getChildren = function (cr) {
var _this = this;
this.childrendRefs = [];
var children = React.Children.map(cr, function (child) {
var x = child;
if (child && child.type && child.type.prototype && child.type.prototype.isForm && child.type.prototype.isForm()) { //表示是From组件,需要复制获取ref属性
if (child.type.name === 'Submit') {
return React.cloneElement(x, {
onPress: _this.submit.bind(_this)
});
}
else {
return React.cloneElement(x, {
ref: function (node) {
if (node)
_this.childrendRefs.push(node);
}
});
}
}
else {
if (x && x.props && x.props.children && (typeof x.props.children !== 'string') && (typeof x.props.children) !== 'number') {
return React.cloneElement(x, {
children: _this.getChildren(x.props.children)
});
}
else {
return x;
}
}
});
return children;
};
Form.prototype.render = function () {
return this.getChildren(this.props.children);
};
return Form;
}(Component));
export default Form;