@pisell/layout
Version:
基于 Fusion 设计系统的自然布局体系
85 lines (84 loc) • 3.04 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/objectWithoutPropertiesLoose";
var _excluded = ["className", "type", "style", "component", "strong", "underline", "delete", "code", "mark", "color", "align", "backgroundColor", "children"];
import React, { createElement, useMemo, useContext } from 'react';
import classNames from 'classnames';
import Context from "./common/context";
import { TEXT_TYPE_MAP } from "./common/constant";
/**
* 文字 包含:字号、字重、行高
* @param props
*/
var Text = function Text(props) {
var _classNames;
var className = props.className,
type = props.type,
style = props.style,
_props$component = props.component,
component = _props$component === void 0 ? 'span' : _props$component,
strong = props.strong,
underline = props.underline,
deleteProp = props["delete"],
code = props.code,
mark = props.mark,
color = props.color,
align = props.align,
backgroundColor = props.backgroundColor,
children = props.children,
others = _objectWithoutPropertiesLoose(props, _excluded);
var _useContext = useContext(Context),
prefix = _useContext.prefix;
// @ts-ignore
var newType = TEXT_TYPE_MAP[type] || type;
var cls = classNames(className, (_classNames = {}, _classNames[prefix + "text"] = true, _classNames[prefix + "text-" + newType] = newType, _classNames));
var memorizedChildren = useMemo(function () {
var _children = children;
if (typeof _children === 'string' && _children.indexOf('\n') !== -1) {
var childrenList = _children.split('\n');
var newChildren = [];
childrenList.forEach(function (child) {
newChildren.push(child);
newChildren.push( /*#__PURE__*/React.createElement("br", null));
});
newChildren.pop();
_children = newChildren;
}
if (strong) {
_children = /*#__PURE__*/React.createElement("strong", null, _children);
}
if (underline) {
_children = /*#__PURE__*/React.createElement("u", null, _children);
}
if (deleteProp) {
_children = /*#__PURE__*/React.createElement("del", null, _children);
}
if (code) {
_children = /*#__PURE__*/React.createElement("code", null, _children);
}
if (mark) {
_children = /*#__PURE__*/React.createElement("mark", null, _children);
}
return _children;
}, [children, mark, code, deleteProp, underline, strong]);
var newStyle = useMemo(function () {
return _extends({}, color ? {
color: color
} : null, backgroundColor ? {
backgroundColor: backgroundColor
} : null, {
textAlign: align
}, style);
}, [color, backgroundColor, align, style]);
return /*#__PURE__*/createElement(component, _extends({}, others, {
style: newStyle,
className: cls
}), memorizedChildren);
};
Text.displayName = 'Text';
Text.typeMark = 'Text';
Text.defaultProps = {
align: 'left',
type: 'body2',
component: 'span'
};
export default Text;