/
BadgeUnstyled.tsx
153 lines (142 loc) · 3.87 KB
/
BadgeUnstyled.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import * as React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import { OverridableComponent } from '@mui/types';
import composeClasses from '../composeClasses';
import appendOwnerState from '../utils/appendOwnerState';
import useBadge from './useBadge';
import { getBadgeUnstyledUtilityClass } from './badgeUnstyledClasses';
import {
BadgeUnstyledProps,
BadgeUnstyledOwnerState,
BadgeUnstyledTypeMap,
BadgeUnstyledRootSlotProps,
BadgeUnstyledBadgeSlotProps,
} from './BadgeUnstyled.types';
import { WithOptionalOwnerState } from '../utils';
const useUtilityClasses = (ownerState: BadgeUnstyledOwnerState) => {
const { invisible } = ownerState;
const slots = {
root: ['root'],
badge: ['badge', invisible && 'invisible'],
};
return composeClasses(slots, getBadgeUnstyledUtilityClass, undefined);
};
/**
*
* Demos:
*
* - [Badge](https://mui.com/base/react-badge/)
*
* API:
*
* - [BadgeUnstyled API](https://mui.com/base/api/badge-unstyled/)
*/
const BadgeUnstyled = React.forwardRef(function BadgeUnstyled(
props: BadgeUnstyledProps & { component?: React.ElementType },
ref,
) {
const {
badgeContent: badgeContentProp,
component,
children,
className,
components = {},
componentsProps = {},
invisible: invisibleProp,
max: maxProp = 99,
showZero = false,
...other
} = props;
const { badgeContent, max, displayValue, invisible } = useBadge({
...props,
max: maxProp,
});
const ownerState: BadgeUnstyledOwnerState = {
...props,
badgeContent,
invisible,
max,
showZero,
};
const classes = useUtilityClasses(ownerState);
const Root = component || components.Root || 'span';
const rootProps: WithOptionalOwnerState<BadgeUnstyledRootSlotProps> = appendOwnerState(
Root,
{
...other,
...componentsProps.root,
ref,
className: clsx(classes.root, componentsProps.root?.className, className),
},
ownerState,
);
const Badge = components.Badge || 'span';
const badgeProps: WithOptionalOwnerState<BadgeUnstyledBadgeSlotProps> = appendOwnerState(
Badge,
{ ...componentsProps.badge, className: clsx(classes.badge, componentsProps.badge?.className) },
ownerState,
);
return (
<Root {...rootProps}>
{children}
<Badge {...badgeProps}>{displayValue}</Badge>
</Root>
);
}) as OverridableComponent<BadgeUnstyledTypeMap>;
BadgeUnstyled.propTypes /* remove-proptypes */ = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit TypeScript types and run "yarn proptypes" |
// ----------------------------------------------------------------------
/**
* The content rendered within the badge.
*/
badgeContent: PropTypes.node,
/**
* The badge will be added relative to this node.
*/
children: PropTypes.node,
/**
* @ignore
*/
className: PropTypes.string,
/**
* The component used for the root node.
* Either a string to use a HTML element or a component.
*/
component: PropTypes.elementType,
/**
* The components used for each slot inside the Badge.
* Either a string to use a HTML element or a component.
* @default {}
*/
components: PropTypes.shape({
Badge: PropTypes.elementType,
Root: PropTypes.elementType,
}),
/**
* The props used for each slot inside the Badge.
* @default {}
*/
componentsProps: PropTypes.shape({
badge: PropTypes.object,
root: PropTypes.object,
}),
/**
* If `true`, the badge is invisible.
* @default false
*/
invisible: PropTypes.bool,
/**
* Max count to show.
* @default 99
*/
max: PropTypes.number,
/**
* Controls whether the badge is hidden when `badgeContent` is zero.
* @default false
*/
showZero: PropTypes.bool,
} as any;
export default BadgeUnstyled;