Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
mnajdova committed Jul 26, 2024
2 parents 2e6f73c + e3f1fa0 commit 9b3ee81
Show file tree
Hide file tree
Showing 28 changed files with 6,520 additions and 0 deletions.
46 changes: 46 additions & 0 deletions packages/mui-material/src/Badge/useBadge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use client';
import * as React from 'react';
import { usePreviousProps } from '@mui/utils';
import { UseBadgeParameters, UseBadgeReturnValue } from './useBadge.types';

/**
*
* Demos:
*
* - [Badge](https://next.mui.com/base-ui/react-badge/#hook)
*
* API:
*
* - [useBadge API](https://next.mui.com/base-ui/react-badge/hooks-api/#use-badge)
*/
export function useBadge(parameters: UseBadgeParameters): UseBadgeReturnValue {
const {
badgeContent: badgeContentProp,
invisible: invisibleProp = false,
max: maxProp = 99,
showZero = false,
} = parameters;

const prevProps = usePreviousProps({
badgeContent: badgeContentProp,
max: maxProp,
});

let invisible = invisibleProp;

if (invisibleProp === false && badgeContentProp === 0 && !showZero) {
invisible = true;
}

const { badgeContent, max = maxProp } = invisible ? prevProps : parameters;

const displayValue: React.ReactNode =
badgeContent && Number(badgeContent) > max ? `${max}+` : badgeContent;

return {
badgeContent,
invisible,
max,
displayValue,
};
}
40 changes: 40 additions & 0 deletions packages/mui-material/src/Badge/useBadge.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
export interface UseBadgeParameters {
/**
* The content rendered within the badge.
*/
badgeContent?: React.ReactNode;
/**
* If `true`, the badge is invisible.
* @default false
*/
invisible?: boolean;
/**
* Max count to show.
* @default 99
*/
max?: number;
/**
* Controls whether the badge is hidden when `badgeContent` is zero.
* @default false
*/
showZero?: boolean;
}

export interface UseBadgeReturnValue {
/**
* Defines the content that's displayed inside the badge.
*/
badgeContent: React.ReactNode;
/**
* If `true`, the component will not be visible.
*/
invisible: boolean;
/**
* Maximum number to be displayed in the badge.
*/
max: number;
/**
* Value to be displayed in the badge. If `badgeContent` is greater than `max`, it will return `max+`.
*/
displayValue: React.ReactNode;
}
Loading

0 comments on commit 9b3ee81

Please sign in to comment.