-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
6,520 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.