Skip to content

[material-ui][Badge] Fix stale values being used when badge is invisible #43083

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions packages/mui-base/src/useBadge/useBadge.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use client';
import * as React from 'react';
import { usePreviousProps } from '@mui/utils';
import { UseBadgeParameters, UseBadgeReturnValue } from './useBadge.types';

/**
Expand All @@ -21,18 +20,13 @@ export function useBadge(parameters: UseBadgeParameters): UseBadgeReturnValue {
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 { badgeContent, max = maxProp } = parameters;

const displayValue: React.ReactNode =
badgeContent && Number(badgeContent) > max ? `${max}+` : badgeContent;
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-material/src/Badge/Badge.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ describe('<Badge />', () => {
it('should render with invisible class when invisible and showZero are set to false and content is 0', () => {
const { container } = render(<Badge badgeContent={0} showZero={false} invisible={false} />);
expect(findBadge(container)).to.have.class(classes.invisible);
expect(findBadge(container)).to.have.text('');
expect(findBadge(container)).to.have.text('0');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reverts the fix introduced in #21557. This fix has added the usage of the usePreviousProps hook. You will noticed on this demo: https://next.mui.com/material-ui/react-badge/#badge-visibility, that when clicking on the "-" button, when the value is 1, it immediately disappears without adding the "0" as a content - this is a non valid state. The issue is again here on this PR deploy preview.

});

it('should not render with invisible class when invisible and showZero are set to false and content is not 0', () => {
Expand Down
8 changes: 1 addition & 7 deletions packages/mui-material/src/Badge/useBadge.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use client';
import * as React from 'react';
import { usePreviousProps } from '@mui/utils';
import { UseBadgeParameters, UseBadgeReturnValue } from './useBadge.types';

/**
Expand All @@ -21,18 +20,13 @@ function useBadge(parameters: UseBadgeParameters): UseBadgeReturnValue {
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 { badgeContent, max = maxProp } = parameters;

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