Skip to content

Commit 09726b0

Browse files
kplhubmanucorporat
authored andcommitted
feat(radio-group): add missing implementation for property allowEmptySelection (#16880)
fixes #16841
1 parent 19c8d22 commit 09726b0

File tree

5 files changed

+37
-2
lines changed

5 files changed

+37
-2
lines changed

angular/src/directives/proxies.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,13 +600,14 @@ export class IonRadio {
600600
ionRadioDidUnload!: EventEmitter<CustomEvent>;
601601
ionStyle!: EventEmitter<CustomEvent>;
602602
ionSelect!: EventEmitter<CustomEvent>;
603+
ionDeselect!: EventEmitter<CustomEvent>;
603604
ionFocus!: EventEmitter<CustomEvent>;
604605
ionBlur!: EventEmitter<CustomEvent>;
605606
el: HTMLElement
606607
constructor(c: ChangeDetectorRef, r: ElementRef) {
607608
c.detach();
608609
this.el = r.nativeElement;
609-
proxyOutputs(this, this.el, ['ionRadioDidLoad', 'ionRadioDidUnload', 'ionStyle', 'ionSelect', 'ionFocus', 'ionBlur']);
610+
proxyOutputs(this, this.el, ['ionRadioDidLoad', 'ionRadioDidUnload', 'ionStyle', 'ionSelect', 'ionDeselect', 'ionFocus', 'ionBlur']);
610611
}
611612
}
612613
proxyInputs(IonRadio, ['color', 'mode', 'name', 'disabled', 'checked', 'value']);

core/src/components.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3285,6 +3285,10 @@ export namespace Components {
32853285
*/
32863286
'onIonBlur'?: (event: CustomEvent<void>) => void;
32873287
/**
3288+
* Emitted when checked radio button is selected.
3289+
*/
3290+
'onIonDeselect'?: (event: CustomEvent<RadioChangeEventDetail>) => void;
3291+
/**
32883292
* Emitted when the radio button has focus.
32893293
*/
32903294
'onIonFocus'?: (event: CustomEvent<void>) => void;

core/src/components/radio-group/radio-group.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,17 @@ export class RadioGroup implements ComponentInterface {
7373
}
7474
}
7575

76+
@Listen('ionDeselect')
77+
onRadioDeselect(ev: Event) {
78+
if (this.allowEmptySelection) {
79+
const selectedRadio = ev.target as HTMLIonRadioElement | null;
80+
if (selectedRadio) {
81+
selectedRadio.checked = false;
82+
this.value = undefined;
83+
}
84+
}
85+
}
86+
7687
componentDidLoad() {
7788
// Get the list header if it exists and set the id
7889
// this is used to set aria-labelledby

core/src/components/radio-group/test/standalone/index.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@
2828
<ion-radio color="secondary" disabled></ion-radio>
2929
</ion-radio-group>
3030

31+
<p>
32+
allow-empty-selection="true":&nbsp;
33+
<ion-radio-group allow-empty-selection="true">
34+
<ion-radio color="primary"></ion-radio>
35+
<ion-radio color="secondary"></ion-radio>
36+
<ion-radio color="tertiary"></ion-radio>
37+
</ion-radio-group>
38+
</p>
39+
3140
<style>
3241
/* to be able to see the radio buttons */
3342
.radio-ios {

core/src/components/radio/radio.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ export class Radio implements ComponentInterface {
7575
*/
7676
@Event() ionSelect!: EventEmitter<RadioChangeEventDetail>;
7777

78+
/**
79+
* Emitted when checked radio button is selected.
80+
* @internal
81+
*/
82+
@Event() ionDeselect!: EventEmitter<RadioChangeEventDetail>;
83+
7884
/**
7985
* Emitted when the radio button has focus.
8086
*/
@@ -129,7 +135,11 @@ export class Radio implements ComponentInterface {
129135
}
130136

131137
private onClick = () => {
132-
this.checked = true;
138+
if (this.checked) {
139+
this.ionDeselect.emit();
140+
} else {
141+
this.checked = true;
142+
}
133143
}
134144

135145
private onKeyUp = () => {

0 commit comments

Comments
 (0)