UNPKG

1.31 kBTypeScriptView Raw
1import * as React from 'react';
2import * as PropTypes from 'prop-types';
3import { InlineFlexProps as ReakitInlineFlexProps } from 'reakit/ts';
4// @ts-ignore
5import _get from 'lodash/get';
6
7import _Badge from './styled';
8
9export type LocalBadgeProps = {
10 children?: React.ReactNode;
11 isAbsolute?: boolean;
12 palette?: string;
13};
14export type BadgeProps = ReakitInlineFlexProps & LocalBadgeProps;
15
16export const badgePropTypes = {
17 children: PropTypes.node,
18 isAbsolute: PropTypes.bool,
19 palette: PropTypes.string
20};
21
22export const badgeDefaultProps = {
23 children: undefined,
24 isAbsolute: false,
25 palette: 'text'
26};
27
28export class Badge extends React.Component<LocalBadgeProps> {
29 static propTypes = badgePropTypes;
30 static defaultProps = badgeDefaultProps;
31
32 badge = React.createRef();
33
34 state = {
35 parentElement: undefined
36 };
37
38 componentDidMount = () => {
39 const parentElement = _get(this.badge, 'current.parentElement');
40 parentElement.setAttribute('style', 'position:relative;');
41 this.setState({ parentElement });
42 };
43
44 render = () => {
45 const { children, ...props } = this.props;
46 return (
47 <_Badge elementRef={this.badge} {...props}>
48 {children}
49 </_Badge>
50 );
51 };
52}
53
54// @ts-ignore
55const C: React.FunctionComponent<BadgeProps> = Badge;
56export default C;