import React, { FunctionComponent, ReactNode } from 'react'; import { View, Text, StyleProp, ViewStyle } from 'react-native'; import Icon from '../icon'; import toObj from '../utils/style-to-obj'; import fConStyle from '../utils/filter-container-style'; import badgeStyle from './styles'; import { useConfig } from '../configprovider'; export interface BadgeProps { value: any; dot: boolean; max: number; top: number; right: number; zIndex: number; color: string; icon: any; style: StyleProp | undefined; children?: ReactNode; } export type BadgeType = | 'default' | 'primary' | 'success' | 'warning' | 'danger'; const defaultProps = { value: '', dot: false, max: 10000, top: 0, right: 0, zIndex: 0, color: '', icon: '', } as BadgeProps; export const Badge: FunctionComponent> = (props) => { const { theme } = useConfig(); const styles = badgeStyle(theme); const { style, children, dot, top, right, zIndex, color, icon } = { ...defaultProps, ...props, }; function content() { if (dot) return undefined; const { value } = props; const { max } = props; if (typeof value === 'number' && typeof max === 'number') { return max < value ? `${max}+` : value; } return value; } const getStyle = () => { const style: StyleProp = {}; if (top) { style.top = top; } if (right) { style.right = right; } style.zIndex = zIndex; if (color) { style.backgroundColor = color; } return style; }; let wrapStyle = [styles.container, fConStyle(toObj(style || {}))]; let dotStyle = [styles.sup, dot ? styles.isDot : {}, getStyle()]; return ( {icon !== '' ? ( ) : null} {children} {content() || ''} ); }; Badge.defaultProps = defaultProps; Badge.displayName = 'NutBadge';