Skip to content

Commit e298810

Browse files
[material-ui][Button] Apply id only if loading indicator is present (@aarongarciah) (#45339)
Co-authored-by: Aarón García Hervás <[email protected]>
1 parent 78ae74a commit e298810

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

packages/mui-material/src/Button/Button.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -531,9 +531,9 @@ const Button = React.forwardRef(function Button(inProps, ref) {
531531
...other
532532
} = props;
533533

534-
const id = useId(idProp);
534+
const loadingId = useId(idProp);
535535
const loadingIndicator = loadingIndicatorProp ?? (
536-
<CircularProgress aria-labelledby={id} color="inherit" size={16} />
536+
<CircularProgress aria-labelledby={loadingId} color="inherit" size={16} />
537537
);
538538

539539
const ownerState = {
@@ -600,7 +600,7 @@ const Button = React.forwardRef(function Button(inProps, ref) {
600600
focusVisibleClassName={clsx(classes.focusVisible, focusVisibleClassName)}
601601
ref={ref}
602602
type={type}
603-
id={id}
603+
id={loading ? loadingId : idProp}
604604
{...other}
605605
classes={classes}
606606
>

packages/mui-material/src/Button/Button.test.js

+15
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,21 @@ describe('<Button />', () => {
792792
const progressbar = within(button).getByRole('progressbar');
793793
expect(progressbar).toHaveAccessibleName('Submit');
794794
});
795+
796+
it('has no id when `loading=false` and no `id` prop is present`', () => {
797+
const id = 'some-id';
798+
render(
799+
<React.Fragment>
800+
<Button />
801+
<Button id={id} />
802+
</React.Fragment>,
803+
);
804+
805+
const buttons = screen.getAllByRole('button');
806+
807+
expect(buttons[0]).not.to.have.attribute('id');
808+
expect(buttons[1]).to.have.attribute('id', id);
809+
});
795810
});
796811

797812
describe('prop: loadingIndicator', () => {

packages/mui-material/src/IconButton/IconButton.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,9 @@ const IconButton = React.forwardRef(function IconButton(inProps, ref) {
187187
...other
188188
} = props;
189189

190-
const id = useId(idProp);
190+
const loadingId = useId(idProp);
191191
const loadingIndicator = loadingIndicatorProp ?? (
192-
<CircularProgress aria-labelledby={id} color="inherit" size={16} />
192+
<CircularProgress aria-labelledby={loadingId} color="inherit" size={16} />
193193
);
194194

195195
const ownerState = {
@@ -207,7 +207,7 @@ const IconButton = React.forwardRef(function IconButton(inProps, ref) {
207207

208208
return (
209209
<IconButtonRoot
210-
id={id}
210+
id={loading ? loadingId : idProp}
211211
className={clsx(classes.root, className)}
212212
centerRipple
213213
focusRipple={!disableFocusRipple}

packages/mui-material/src/IconButton/IconButton.test.js

+15
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,21 @@ describe('<IconButton />', () => {
192192
const progressbar = within(button).getByRole('progressbar');
193193
expect(progressbar).toHaveAccessibleName('Submit');
194194
});
195+
196+
it('has no id when `loading=false` and no `id` prop is present`', () => {
197+
const id = 'some-id';
198+
render(
199+
<React.Fragment>
200+
<IconButton />
201+
<IconButton id={id} />
202+
</React.Fragment>,
203+
);
204+
205+
const buttons = screen.getAllByRole('button');
206+
207+
expect(buttons[0]).not.to.have.attribute('id');
208+
expect(buttons[1]).to.have.attribute('id', id);
209+
});
195210
});
196211

197212
describe('prop: loadingIndicator', () => {

0 commit comments

Comments
 (0)