This repository has been archived by the owner on Nov 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
CounterBadge.tsx
66 lines (53 loc) · 1.71 KB
/
CounterBadge.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import * as React from 'react';
import { withStylable } from 'wix-ui-core/dist/src/utils/withStylable';
import {
Badge as CoreBadge,
BadgeProps as CoreBadgeProps
} from 'wix-ui-core/dist/src/components/deprecated/stylable-badge';
import { Skin, SKIN, maxNumberBeforeTruncation } from './constants';
import { isIcon, isWide } from './utils';
import style from './CounterBadge.st.css';
export type Content = string | number | React.ReactElement<any>;
export interface CounterBadgeProps {
/** Skin of the badge */
skin?: Skin;
/** Content of the badge */
children?: Content;
}
const defaultProps: CounterBadgeProps = {
skin: SKIN.general,
children: ''
};
type CounterBadgeExtendedProps = CounterBadgeProps & { wide: boolean };
const getState = ({ skin, wide }) => ({ skin, wide });
const StyledCounterBadge = withStylable<
CoreBadgeProps,
CounterBadgeExtendedProps
>(CoreBadge, style, getState, defaultProps);
export class CounterBadge extends React.PureComponent<CounterBadgeProps> {
static displayName = 'CounterBadge';
static defaultProps = defaultProps;
private readonly getContent = () => {
const { children } = this.props;
const isChildrenIcon = isIcon(children);
let content = children;
if (!isChildrenIcon && Number(children) > maxNumberBeforeTruncation) {
content = '99+';
}
return isChildrenIcon ? (
React.cloneElement(children as React.ReactElement<any>, {
className: style.icon
})
) : (
<span className={style.text}>{content}</span>
);
};
render() {
const { children, ...rest } = this.props;
return (
<StyledCounterBadge {...rest} wide={isWide(children)}>
{this.getContent()}
</StyledCounterBadge>
);
}
}