Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix(radio): deselect button if value doesn't match up with the group anymore #14734

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
crisbeto wants to merge 1 commit into angular:main
base: main
Choose a base branch
Loading
from crisbeto:radio-selected-button-value-change
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
21 changes: 21 additions & 0 deletions src/material/radio/radio.spec.ts
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,27 @@ describe('MatRadio', () => {
expect(radioInstances[2].checked).toBeFalsy('should not select the third button');
});

it(`should update checked status if changed value to radio group's value`, () => {
groupInstance.value = 'apple';
radioInstances[0].value = 'apple';
fixture.detectChanges();

expect(groupInstance.selected).toBe(
radioInstances[0], 'expect group selected to be first button');
expect(radioInstances[0].checked).toBeTruthy('expect group select the first button');
expect(radioInstances[1].checked).toBeFalsy('should not select the second button');
expect(radioInstances[2].checked).toBeFalsy('should not select the third button');

radioInstances[0].value = 'watermelon';
fixture.detectChanges();

expect(groupInstance.value).toBeFalsy();
expect(groupInstance.selected).toBeFalsy('expect group selected to be null');
expect(radioInstances[0].checked).toBeFalsy('should not select the first button');
expect(radioInstances[1].checked).toBeFalsy('should not select the second button');
expect(radioInstances[2].checked).toBeFalsy('should not select the third button');
});

it('should apply class based on color attribute', () => {
expect(radioNativeElements.every(radioEl => radioEl.classList.contains('mat-accent')))
.toBe(true, 'Expected every radio element to use the accent color by default.');
Expand Down
15 changes: 9 additions & 6 deletions src/material/radio/radio.ts
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -402,14 +402,17 @@ export class MatRadioButton extends _MatRadioButtonMixinBase
get value(): any { return this._value; }
set value(value: any) {
if (this._value !== value) {
const group = this.radioGroup;
this._value = value;
if (this.radioGroup !== null) {
if (!this.checked) {
// Update checked when the value changed to match the radio group's value
this.checked = this.radioGroup.value === value;
}

if (group) {
// Update `checked`, because the button's value might not match up with the group anymore.
this.checked = group.value === value;

if (this.checked) {
this.radioGroup.selected = this;
group.selected = this;
} else if (group.selected === this) {
group.selected = null;
}
}
}
Expand Down

AltStyle によって変換されたページ (->オリジナル) /