mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-16 10:01:59 +08:00
feat(radio-group): add missing implementation for property allowEmptySelection (#16880)
fixes #16841
This commit is contained in:
@ -600,13 +600,14 @@ export class IonRadio {
|
||||
ionRadioDidUnload!: EventEmitter<CustomEvent>;
|
||||
ionStyle!: EventEmitter<CustomEvent>;
|
||||
ionSelect!: EventEmitter<CustomEvent>;
|
||||
ionDeselect!: EventEmitter<CustomEvent>;
|
||||
ionFocus!: EventEmitter<CustomEvent>;
|
||||
ionBlur!: EventEmitter<CustomEvent>;
|
||||
el: HTMLElement
|
||||
constructor(c: ChangeDetectorRef, r: ElementRef) {
|
||||
c.detach();
|
||||
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']);
|
||||
|
4
core/src/components.d.ts
vendored
4
core/src/components.d.ts
vendored
@ -3285,6 +3285,10 @@ export namespace Components {
|
||||
*/
|
||||
'onIonBlur'?: (event: CustomEvent<void>) => void;
|
||||
/**
|
||||
* Emitted when checked radio button is selected.
|
||||
*/
|
||||
'onIonDeselect'?: (event: CustomEvent<RadioChangeEventDetail>) => void;
|
||||
/**
|
||||
* Emitted when the radio button has focus.
|
||||
*/
|
||||
'onIonFocus'?: (event: CustomEvent<void>) => void;
|
||||
|
@ -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() {
|
||||
// Get the list header if it exists and set the id
|
||||
// this is used to set aria-labelledby
|
||||
|
@ -28,6 +28,15 @@
|
||||
<ion-radio color="secondary" disabled></ion-radio>
|
||||
</ion-radio-group>
|
||||
|
||||
<p>
|
||||
allow-empty-selection="true":
|
||||
<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>
|
||||
/* to be able to see the radio buttons */
|
||||
.radio-ios {
|
||||
|
@ -75,6 +75,12 @@ export class Radio implements ComponentInterface {
|
||||
*/
|
||||
@Event() ionSelect!: EventEmitter<RadioChangeEventDetail>;
|
||||
|
||||
/**
|
||||
* Emitted when checked radio button is selected.
|
||||
* @internal
|
||||
*/
|
||||
@Event() ionDeselect!: EventEmitter<RadioChangeEventDetail>;
|
||||
|
||||
/**
|
||||
* Emitted when the radio button has focus.
|
||||
*/
|
||||
@ -129,8 +135,12 @@ export class Radio implements ComponentInterface {
|
||||
}
|
||||
|
||||
private onClick = () => {
|
||||
if (this.checked) {
|
||||
this.ionDeselect.emit();
|
||||
} else {
|
||||
this.checked = true;
|
||||
}
|
||||
}
|
||||
|
||||
private onKeyUp = () => {
|
||||
this.keyFocus = true;
|
||||
|
Reference in New Issue
Block a user