UNPKG

3.73 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 const {
35 children,
36 max: maxProp = 99,
37 slotProps = {},
38 slots = {},
39 showZero = false
40 } = props,
41 other = _objectWithoutPropertiesLoose(props, _excluded);
42 const {
43 badgeContent,
44 max,
45 displayValue,
46 invisible
47 } = useBadge(_extends({}, props, {
48 max: maxProp
49 }));
50 const ownerState = _extends({}, props, {
51 badgeContent,
52 invisible,
53 max,
54 showZero
55 });
56 const classes = useUtilityClasses(ownerState);
57 const Root = slots.root ?? 'span';
58 const rootProps = useSlotProps({
59 elementType: Root,
60 externalSlotProps: slotProps.root,
61 externalForwardedProps: other,
62 additionalProps: {
63 ref: forwardedRef
64 },
65 ownerState,
66 className: classes.root
67 });
68 const BadgeComponent = slots.badge ?? 'span';
69 const badgeProps = useSlotProps({
70 elementType: BadgeComponent,
71 externalSlotProps: slotProps.badge,
72 ownerState,
73 className: classes.badge
74 });
75 return /*#__PURE__*/_jsxs(Root, _extends({}, rootProps, {
76 children: [children, /*#__PURE__*/_jsx(BadgeComponent, _extends({}, badgeProps, {
77 children: displayValue
78 }))]
79 }));
80});
81process.env.NODE_ENV !== "production" ? Badge.propTypes /* remove-proptypes */ = {
82 // ----------------------------- Warning --------------------------------
83 // | These PropTypes are generated from the TypeScript type definitions |
84 // | To update them edit TypeScript types and run "yarn proptypes" |
85 // ----------------------------------------------------------------------
86 /**
87 * The content rendered within the badge.
88 */
89 badgeContent: PropTypes.node,
90 /**
91 * The badge will be added relative to this node.
92 */
93 children: PropTypes.node,
94 /**
95 * If `true`, the badge is invisible.
96 * @default false
97 */
98 invisible: PropTypes.bool,
99 /**
100 * Max count to show.
101 * @default 99
102 */
103 max: PropTypes.number,
104 /**
105 * Controls whether the badge is hidden when `badgeContent` is zero.
106 * @default false
107 */
108 showZero: PropTypes.bool,
109 /**
110 * The props used for each slot inside the Badge.
111 * @default {}
112 */
113 slotProps: PropTypes.shape({
114 badge: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
115 root: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
116 }),
117 /**
118 * The components used for each slot inside the Badge.
119 * Either a string to use a HTML element or a component.
120 * @default {}
121 */
122 slots: PropTypes.shape({
123 badge: PropTypes.elementType,
124 root: PropTypes.elementType
125 })
126} : void 0;
127export default Badge;
\No newline at end of file