Skip to content

Commit f5219d1

Browse files
committed
fix(radio): Fix radio button group value did not change for false, 0, and empty string
1 parent 44637f3 commit f5219d1

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

src/lib/radio/radio.ts

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ export class MatRadioGroup extends _MatRadioGroupMixinBase
171171
@Input()
172172
get value(): any { return this._value; }
173173
set value(newValue: any) {
174-
if (this._value != newValue) {
174+
if (this._value !== newValue) {
175175
// Set this before proceeding to ensure no circular loop occurs with selection.
176176
this._value = newValue;
177177

@@ -247,12 +247,12 @@ export class MatRadioGroup extends _MatRadioGroupMixinBase
247247
/** Updates the `selected` radio button from the internal _value state. */
248248
private _updateSelectedRadioFromValue(): void {
249249
// If the value already matches the selected radio, do nothing.
250-
const isAlreadySelected = this._selected != null && this._selected.value == this._value;
250+
const isAlreadySelected = this._selected !== null && this._selected.value === this._value;
251251

252-
if (this._radios != null && !isAlreadySelected) {
252+
if (this._radios !== null && !isAlreadySelected) {
253253
this._selected = null;
254254
this._radios.forEach(radio => {
255-
radio.checked = this.value == radio.value;
255+
radio.checked = this.value === radio.value;
256256
if (radio.checked) {
257257
this._selected = radio;
258258
}
@@ -374,13 +374,12 @@ export class MatRadioButton extends _MatRadioButtonMixinBase
374374
get checked(): boolean { return this._checked; }
375375
set checked(value: boolean) {
376376
const newCheckedState = coerceBooleanProperty(value);
377-
378-
if (this._checked != newCheckedState) {
377+
if (this._checked !== newCheckedState) {
379378
this._checked = newCheckedState;
380-
381-
if (newCheckedState && this.radioGroup && this.radioGroup.value != this.value) {
379+
if (newCheckedState && this.radioGroup && this.radioGroup.value !== this.value) {
382380
this.radioGroup.selected = this;
383-
} else if (!newCheckedState && this.radioGroup && this.radioGroup.value == this.value) {
381+
} else if (!newCheckedState && this.radioGroup && this.radioGroup.value === this.value) {
382+
384383
// When unchecking the selected radio button, update the selected radio
385384
// property on the group.
386385
this.radioGroup.selected = null;
@@ -398,12 +397,12 @@ export class MatRadioButton extends _MatRadioButtonMixinBase
398397
@Input()
399398
get value(): any { return this._value; }
400399
set value(value: any) {
401-
if (this._value != value) {
400+
if (this._value !== value) {
402401
this._value = value;
403-
if (this.radioGroup != null) {
402+
if (this.radioGroup !== null) {
404403
if (!this.checked) {
405404
// Update checked when the value changed to match the radio group's value
406-
this.checked = this.radioGroup.value == value;
405+
this.checked = this.radioGroup.value === value;
407406
}
408407
if (this.checked) {
409408
this.radioGroup.selected = this;
@@ -421,10 +420,10 @@ export class MatRadioButton extends _MatRadioButtonMixinBase
421420
get align(): 'start' | 'end' {
422421
// align refers to the checkbox relative to the label, while labelPosition refers to the
423422
// label relative to the checkbox. As such, they are inverted.
424-
return this.labelPosition == 'after' ? 'start' : 'end';
423+
return this.labelPosition === 'after' ? 'start' : 'end';
425424
}
426425
set align(v) {
427-
this.labelPosition = (v == 'start') ? 'after' : 'before';
426+
this.labelPosition = (v === 'start') ? 'after' : 'before';
428427
}
429428

430429
private _labelPosition: 'before' | 'after';
@@ -441,7 +440,7 @@ export class MatRadioButton extends _MatRadioButtonMixinBase
441440
/** Whether the radio button is disabled. */
442441
@Input()
443442
get disabled(): boolean {
444-
return this._disabled || (this.radioGroup != null && this.radioGroup.disabled);
443+
return this._disabled || (this.radioGroup !== null && this.radioGroup.disabled);
445444
}
446445
set disabled(value: boolean) {
447446
this._disabled = coerceBooleanProperty(value);
@@ -506,7 +505,7 @@ export class MatRadioButton extends _MatRadioButtonMixinBase
506505

507506
this._removeUniqueSelectionListener =
508507
_radioDispatcher.listen((id: string, name: string) => {
509-
if (id != this.id && name == this.name) {
508+
if (id !== this.id && name === this.name) {
510509
this.checked = false;
511510
}
512511
});
@@ -578,7 +577,7 @@ export class MatRadioButton extends _MatRadioButtonMixinBase
578577
// emit its event object to the `change` output.
579578
event.stopPropagation();
580579

581-
const groupValueChanged = this.radioGroup && this.value != this.radioGroup.value;
580+
const groupValueChanged = this.radioGroup && this.value !== this.radioGroup.value;
582581
this.checked = true;
583582
this._emitChangeEvent();
584583

0 commit comments

Comments
 (0)