rat-text-test
Version:
text component
166 lines (132 loc) • 5.88 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _ratFrame = require('rat-frame');
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var style = {
text: {
position: 'relative',
'box-sizing': 'border-box',
'flex-direction': 'column',
'align-content': 'flex-start',
'flex-shrink': 0,
'margin-top': 0,
'margin-bottom': 0
},
richtext: {
'margin-top': 0,
'margin-bottom': 0
}
};
var tagTypeArr = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'span'];
var Text = function (_Component) {
_inherits(Text, _Component);
function Text() {
var _ref;
var _temp, _this, _ret;
_classCallCheck(this, Text);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Text.__proto__ || Object.getPrototypeOf(Text)).call.apply(_ref, [this].concat(args))), _this), _this.renderText = function () {
var props = _this.props;
var webProps = _extends({}, props, {
style: props.style || {}
});
var tagType = props.tagType;
var textString = '';
if (props.children != null) {
if (!Array.isArray(props.children)) {
textString = props.children.toString();
} else {
textString = props.children.join('');
}
}
if (props.onPress) {
webProps.onClick = props.onPress;
}
var styleProps = _extends({}, style.text, webProps.style);
var numberOfLines = props.numberOfLines;
if (numberOfLines) {
if (parseInt(numberOfLines) === 1) {
styleProps.whiteSpace = 'nowrap';
} else {
styleProps.display = '-webkit-box';
styleProps.webkitBoxOrient = 'vertical';
styleProps.webkitLineClamp = String(numberOfLines);
}
styleProps.overflow = 'hidden';
}
if (tagType && tagTypeArr.indexOf(tagType) > -1) {
var NewTag = tagType;
delete webProps.tagType;
return (0, _ratFrame.createElement)(
NewTag,
_extends({}, webProps, { style: styleProps }),
textString
);
}
return (0, _ratFrame.createElement)(
'span',
_extends({}, webProps, { style: styleProps }),
textString
);
}, _this.renderRichText = function () {
var props = _this.props;
var children = props.children,
tagType = props.tagType;
var webProps = _extends({}, props, {
style: props.style || {}
});
var styleProps = _extends({}, style.richtext, webProps.style);
if (tagType && tagTypeArr.indexOf(tagType) > -1) {
var NewTag = tagType;
delete webProps.tagType;
return (0, _ratFrame.createElement)(
NewTag,
_extends({}, webProps, { style: styleProps }),
children
);
}
return (0, _ratFrame.createElement)(
'p',
_extends({}, webProps, { style: styleProps }),
children
);
}, _temp), _possibleConstructorReturn(_this, _ret);
}
_createClass(Text, [{
key: 'render',
value: function render() {
var props = this.props;
var children = props.children;
if (!Array.isArray(children)) {
children = [children];
}
var nested = false;
for (var i = 0; i < children.length; i++) {
var child = children[i];
if (child && (typeof child === 'undefined' ? 'undefined' : _typeof(child)) === 'object') {
nested = true;
break;
}
}
return nested ? this.renderRichText() : this.renderText();
}
/**
* 无子元素文本渲染
*/
/**
* 有子元素文本渲染
*/
}]);
return Text;
}(_ratFrame.Component);
exports.default = Text;
module.exports = exports['default'];