@fruits-chain/react-native-xiaoshu
Version:
🌈 React Native UI library
97 lines (80 loc) • 3.51 kB
JavaScript
function _extends() { _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; }; return _extends.apply(this, arguments); }
import isNil from 'lodash/isNil';
import React, { memo, isValidElement } from 'react';
import { View } from 'react-native';
import { renderTextLikeJSX, getDefaultValue } from '../helpers';
import Theme from '../theme';
import { useDescription } from './context';
import { varCreator, styleCreator } from './style';
const Description = _ref => {
let {
colon,
contentStyle,
contentTextStyle,
labelStyle,
labelTextStyle,
labelWidth,
layout,
size,
numberOfLines,
justify,
align,
label,
text,
hidden = false,
bold = false,
color,
addonBefore,
addonAfter,
renderLabel,
render,
children,
style,
...restProps
} = _ref;
const TOKENS = Theme.useThemeTokens();
const CV = Theme.createVar(TOKENS, varCreator);
const STYLES = Theme.createStyle(CV, styleCreator);
const descriptionContext = useDescription(); // 整理默认值
const _colon = getDefaultValue(colon, descriptionContext.colon);
const _contentStyle = getDefaultValue(contentStyle, descriptionContext.contentStyle);
const _contentTextStyle = getDefaultValue(contentTextStyle, descriptionContext.contentTextStyle);
const _labelStyle = getDefaultValue(labelStyle, descriptionContext.labelStyle);
const _labelTextStyle = getDefaultValue(labelTextStyle, descriptionContext.labelTextStyle);
const _labelWidth = getDefaultValue(labelWidth, descriptionContext.labelWidth);
const _layout = getDefaultValue(layout, descriptionContext.layout);
const _size = getDefaultValue(size, descriptionContext.size);
const _justify = getDefaultValue(justify, descriptionContext.justify);
const _align = getDefaultValue(align, descriptionContext.align);
const _numberOfLines = getDefaultValue(numberOfLines, descriptionContext.numberOfLines);
const colonStr = _colon ? ':' : '';
const textSizeStyle = STYLES[`size_${_size}_text`];
const flexDirection = _layout === 'horizontal' ? 'row' : 'column';
const labelJSX = !isNil(renderLabel) ? renderLabel(colonStr) : !isNil(label) ? renderTextLikeJSX(`${label}${colonStr}`, [STYLES.label_text, textSizeStyle, _labelTextStyle]) : null;
const contentJSX = /*#__PURE__*/isValidElement(children) ? children : renderTextLikeJSX(!isNil(text) ? text : children, [STYLES.content_text, textSizeStyle, bold ? {
fontWeight: 'bold'
} : null, color ? {
color
} : null, _contentTextStyle], {
numberOfLines: _numberOfLines
});
const renderJSX = !isNil(render) ? render(contentJSX, addonBefore, addonAfter) : /*#__PURE__*/React.createElement(React.Fragment, null, addonBefore, contentJSX, addonAfter);
if (hidden) {
return null;
}
return /*#__PURE__*/React.createElement(View, _extends({}, restProps, {
style: [{
flexDirection,
justifyContent: _justify,
alignItems: _align
}, style]
}), /*#__PURE__*/React.createElement(View, {
style: [!isNil(_labelWidth) ? {
width: _labelWidth
} : null, _labelStyle]
}, labelJSX), /*#__PURE__*/React.createElement(View, {
style: [STYLES.content, _contentStyle]
}, renderJSX));
};
export default /*#__PURE__*/memo(Description);
//# sourceMappingURL=description.js.map