UNPKG

893 BJavaScriptView Raw
1import { usePreviousProps } from '@mui/utils';
2/**
3 *
4 * Demos:
5 *
6 * - [Badge](https://mui.com/base/react-badge/#hook)
7 *
8 * API:
9 *
10 * - [useBadge API](https://mui.com/base/react-badge/hooks-api/#use-badge)
11 */
12export default function useBadge(parameters) {
13 const {
14 badgeContent: badgeContentProp,
15 invisible: invisibleProp = false,
16 max: maxProp = 99,
17 showZero = false
18 } = parameters;
19 const prevProps = usePreviousProps({
20 badgeContent: badgeContentProp,
21 max: maxProp
22 });
23 let invisible = invisibleProp;
24 if (invisibleProp === false && badgeContentProp === 0 && !showZero) {
25 invisible = true;
26 }
27 const {
28 badgeContent,
29 max = maxProp
30 } = invisible ? prevProps : parameters;
31 const displayValue = badgeContent && Number(badgeContent) > max ? `${max}+` : badgeContent;
32 return {
33 badgeContent,
34 invisible,
35 max,
36 displayValue
37 };
38}
\No newline at end of file