weex-nuke
Version:
基于 Rax 、Weex 的高性能组件体系 ~~
64 lines (56 loc) • 1.97 kB
JavaScript
/** @jsx createElement */
;
import { createElement, Component, PropTypes } from 'rax';
import { calcTextSize } from 'nuke-helper';
import { isWeb } from 'nuke-env';
const defaultLineHeightOptimizer = fontSize =>
Math.ceil(9.24 * Math.pow(10, -5) * Math.pow(fontSize, 2) + 1.492 * fontSize + 2.174);
class StyledA extends Component {
constructor(props, context) {
super();
this.fixedFont = context.commonConfigs && context.commonConfigs.fixedFont;
this.optimizeLineHeight = context.commonConfigs && context.commonConfigs.optimizeLineHeight;
if ('fixedFont' in props) {
this.fixedFont = props.fixedFont;
}
if ('optimizeLineHeight' in props) {
this.optimizeLineHeight = props.optimizeLineHeight;
}
}
/**
* This function is used to calculate line height when language is Thai or Vietnanese.
* @param {obj} style
*/
calcLineHeight(style) {
const { lineHeightOptimizer } = this.props;
const fontSize = parseInt(style.fontSize, 10);
const lineHeight = lineHeightOptimizer(fontSize);
if (lineHeight && !style.lineHeight) {
if (this.fixedFont && typeof lineHeight === 'number') {
style.lineHeight = lineHeight + (isWeb ? 'px' : 'wx');
} else {
style.lineHeight = lineHeight;
}
}
return style;
}
render() {
let textStyle = Object.assign({ fontSize: 32, wordWrap: 'break-word' }, this.props.style);
textStyle = this.fixedFont ? calcTextSize(textStyle) : textStyle;
textStyle = this.optimizeLineHeight ? this.calcLineHeight(textStyle) : textStyle;
return <a {...this.props} style={textStyle} fixedFont optimizeLineHeight />;
}
}
StyledA.contextTypes = {
androidConfigs: PropTypes.any,
commonConfigs: PropTypes.any,
};
StyledA.propTypes = {
optimizeLineHeight: PropTypes.boolean,
fixedFont: PropTypes.boolean,
lineHeightOptimizer: PropTypes.func,
};
StyledA.defaultProps = {
style: {},
};
export default StyledA;