@fruits-chain/react-native-xiaoshu
Version:
React Native UI library
55 lines (51 loc) • 1.56 kB
JavaScript
import React, { memo } from 'react';
import { View, Text } from 'react-native';
import { isDef } from '../helpers';
import { useTheme, widthStyle } from '../theme';
import { createStyles } from './style';
/**
* Badge 徽标
* @description 在右上角展示徽标数字或小红点。
*/
const Badge = _ref => {
let {
children,
count,
dot,
max,
color,
style,
countStyle,
countTextStyle,
loading = false,
showZero = false,
offset,
status
} = _ref;
const THEME_VAR = useTheme();
const STYLES = widthStyle(THEME_VAR, createStyles);
if (max && typeof count === 'number' && count > max) {
count = `${max}+`;
}
const hasCount = isDef(count) && (count === 0 ? showZero : true);
const countStyles = [STYLES.count, {
backgroundColor: color || THEME_VAR[status] || THEME_VAR.badge_background_color
}, dot ? STYLES.count_dot : null, isDef(children) ? [STYLES.count_fixed, dot ? STYLES.count_dot_fixed : null, offset ? {
transform: [{
translateX: offset[0]
}, {
translateY: offset[1]
}]
} : null] : [], countStyle];
const badgeJSX = !loading && (hasCount || dot) ? /*#__PURE__*/React.createElement(View, {
style: countStyles
}, dot ? null : /*#__PURE__*/React.createElement(Text, {
style: [STYLES.count_text, countTextStyle]
}, count)) : null;
return /*#__PURE__*/React.createElement(View, {
style: [STYLES.badge, style],
collapsable: false
}, badgeJSX, children);
};
export default /*#__PURE__*/memo(Badge);
//# sourceMappingURL=index.js.map