UNPKG

3.84 kBJavaScriptView Raw
1import _extends from "@babel/runtime/helpers/esm/extends";
2import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
3const _excluded = ["badgeContent", "children", "invisible", "max", "slotProps", "slots", "showZero"];
4import * as React from 'react';
5import PropTypes from 'prop-types';
6import composeClasses from '../composeClasses';
7import useBadge from '../useBadge';
8import { getBadgeUtilityClass } from './badgeClasses';
9import { useSlotProps } from '../utils';
10import { useClassNamesOverride } from '../utils/ClassNameConfigurator';
11import { jsx as _jsx } from "react/jsx-runtime";
12import { jsxs as _jsxs } from "react/jsx-runtime";
13const useUtilityClasses = ownerState => {
14 const {
15 invisible
16 } = ownerState;
17 const slots = {
18 root: ['root'],
19 badge: ['badge', invisible && 'invisible']
20 };
21 return composeClasses(slots, useClassNamesOverride(getBadgeUtilityClass));
22};
23/**
24 *
25 * Demos:
26 *
27 * - [Badge](https://mui.com/base/react-badge/)
28 *
29 * API:
30 *
31 * - [Badge API](https://mui.com/base/react-badge/components-api/#badge)
32 */
33const Badge = /*#__PURE__*/React.forwardRef(function Badge(props, forwardedRef) {
34 var _slots$root, _slots$badge;
35 const {
36 children,
37 max: maxProp = 99,
38 slotProps = {},
39 slots = {},
40 showZero = false
41 } = props,
42 other = _objectWithoutPropertiesLoose(props, _excluded);
43 const {
44 badgeContent,
45 max,
46 displayValue,
47 invisible
48 } = useBadge(_extends({}, props, {
49 max: maxProp
50 }));
51 const ownerState = _extends({}, props, {
52 badgeContent,
53 invisible,
54 max,
55 showZero
56 });
57 const classes = useUtilityClasses(ownerState);
58 const Root = (_slots$root = slots.root) != null ? _slots$root : 'span';
59 const rootProps = useSlotProps({
60 elementType: Root,
61 externalSlotProps: slotProps.root,
62 externalForwardedProps: other,
63 additionalProps: {
64 ref: forwardedRef
65 },
66 ownerState,
67 className: classes.root
68 });
69 const BadgeComponent = (_slots$badge = slots.badge) != null ? _slots$badge : 'span';
70 const badgeProps = useSlotProps({
71 elementType: BadgeComponent,
72 externalSlotProps: slotProps.badge,
73 ownerState,
74 className: classes.badge
75 });
76 return /*#__PURE__*/_jsxs(Root, _extends({}, rootProps, {
77 children: [children, /*#__PURE__*/_jsx(BadgeComponent, _extends({}, badgeProps, {
78 children: displayValue
79 }))]
80 }));
81});
82process.env.NODE_ENV !== "production" ? Badge.propTypes /* remove-proptypes */ = {
83 // ----------------------------- Warning --------------------------------
84 // | These PropTypes are generated from the TypeScript type definitions |
85 // | To update them edit TypeScript types and run "yarn proptypes" |
86 // ----------------------------------------------------------------------
87 /**
88 * The content rendered within the badge.
89 */
90 badgeContent: PropTypes.node,
91 /**
92 * The badge will be added relative to this node.
93 */
94 children: PropTypes.node,
95 /**
96 * If `true`, the badge is invisible.
97 * @default false
98 */
99 invisible: PropTypes.bool,
100 /**
101 * Max count to show.
102 * @default 99
103 */
104 max: PropTypes.number,
105 /**
106 * Controls whether the badge is hidden when `badgeContent` is zero.
107 * @default false
108 */
109 showZero: PropTypes.bool,
110 /**
111 * The props used for each slot inside the Badge.
112 * @default {}
113 */
114 slotProps: PropTypes.shape({
115 badge: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
116 root: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
117 }),
118 /**
119 * The components used for each slot inside the Badge.
120 * Either a string to use a HTML element or a component.
121 * @default {}
122 */
123 slots: PropTypes.shape({
124 badge: PropTypes.elementType,
125 root: PropTypes.elementType
126 })
127} : void 0;
128export default Badge;
\No newline at end of file