feat(radio-group): add missing implementation for property allowEmptySelection (#16880)

fixes #16841
This commit is contained in:
kplhub
2018-12-31 17:24:44 +01:00
committed by Manu MA
parent 19c8d2252a
commit 09726b0d35
5 changed files with 37 additions and 2 deletions

View File

@ -600,13 +600,14 @@ export class IonRadio {
ionRadioDidUnload!: EventEmitter<CustomEvent>; ionRadioDidUnload!: EventEmitter<CustomEvent>;
ionStyle!: EventEmitter<CustomEvent>; ionStyle!: EventEmitter<CustomEvent>;
ionSelect!: EventEmitter<CustomEvent>; ionSelect!: EventEmitter<CustomEvent>;
ionDeselect!: EventEmitter<CustomEvent>;
ionFocus!: EventEmitter<CustomEvent>; ionFocus!: EventEmitter<CustomEvent>;
ionBlur!: EventEmitter<CustomEvent>; ionBlur!: EventEmitter<CustomEvent>;
el: HTMLElement el: HTMLElement
constructor(c: ChangeDetectorRef, r: ElementRef) { constructor(c: ChangeDetectorRef, r: ElementRef) {
c.detach(); c.detach();
this.el = r.nativeElement; this.el = r.nativeElement;
proxyOutputs(this, this.el, ['ionRadioDidLoad', 'ionRadioDidUnload', 'ionStyle', 'ionSelect', 'ionFocus', 'ionBlur']); proxyOutputs(this, this.el, ['ionRadioDidLoad', 'ionRadioDidUnload', 'ionStyle', 'ionSelect', 'ionDeselect', 'ionFocus', 'ionBlur']);
} }
} }
proxyInputs(IonRadio, ['color', 'mode', 'name', 'disabled', 'checked', 'value']); proxyInputs(IonRadio, ['color', 'mode', 'name', 'disabled', 'checked', 'value']);

View File

@ -3285,6 +3285,10 @@ export namespace Components {
*/ */
'onIonBlur'?: (event: CustomEvent<void>) => void; 'onIonBlur'?: (event: CustomEvent<void>) => void;
/** /**
* Emitted when checked radio button is selected.
*/
'onIonDeselect'?: (event: CustomEvent<RadioChangeEventDetail>) => void;
/**
* Emitted when the radio button has focus. * Emitted when the radio button has focus.
*/ */
'onIonFocus'?: (event: CustomEvent<void>) => void; 'onIonFocus'?: (event: CustomEvent<void>) => void;

View File

@ -73,6 +73,17 @@ export class RadioGroup implements ComponentInterface {
} }
} }
@Listen('ionDeselect')
onRadioDeselect(ev: Event) {
if (this.allowEmptySelection) {
const selectedRadio = ev.target as HTMLIonRadioElement | null;
if (selectedRadio) {
selectedRadio.checked = false;
this.value = undefined;
}
}
}
componentDidLoad() { componentDidLoad() {
// Get the list header if it exists and set the id // Get the list header if it exists and set the id
// this is used to set aria-labelledby // this is used to set aria-labelledby

View File

@ -28,6 +28,15 @@
<ion-radio color="secondary" disabled></ion-radio> <ion-radio color="secondary" disabled></ion-radio>
</ion-radio-group> </ion-radio-group>
<p>
allow-empty-selection="true":&nbsp;
<ion-radio-group allow-empty-selection="true">
<ion-radio color="primary"></ion-radio>
<ion-radio color="secondary"></ion-radio>
<ion-radio color="tertiary"></ion-radio>
</ion-radio-group>
</p>
<style> <style>
/* to be able to see the radio buttons */ /* to be able to see the radio buttons */
.radio-ios { .radio-ios {

View File

@ -75,6 +75,12 @@ export class Radio implements ComponentInterface {
*/ */
@Event() ionSelect!: EventEmitter<RadioChangeEventDetail>; @Event() ionSelect!: EventEmitter<RadioChangeEventDetail>;
/**
* Emitted when checked radio button is selected.
* @internal
*/
@Event() ionDeselect!: EventEmitter<RadioChangeEventDetail>;
/** /**
* Emitted when the radio button has focus. * Emitted when the radio button has focus.
*/ */
@ -129,7 +135,11 @@ export class Radio implements ComponentInterface {
} }
private onClick = () => { private onClick = () => {
this.checked = true; if (this.checked) {
this.ionDeselect.emit();
} else {
this.checked = true;
}
} }
private onKeyUp = () => { private onKeyUp = () => {