Skip to content
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

fix(picker): pass data and role to the dismiss based on button click or backdrop #19787

Merged
merged 4 commits into from Oct 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 31 additions & 17 deletions core/src/components/picker/picker.tsx
Expand Up @@ -2,7 +2,7 @@ import { Component, ComponentInterface, Element, Event, EventEmitter, Host, Meth

import { getIonMode } from '../../global/ionic-global';
import { Animation, AnimationBuilder, CssClassMap, OverlayEventDetail, OverlayInterface, PickerButton, PickerColumn } from '../../interface';
import { dismiss, eventMethod, prepareOverlay, present, safeCall } from '../../utils/overlays';
import { BACKDROP, dismiss, eventMethod, isCancel, prepareOverlay, present, safeCall } from '../../utils/overlays';
import { getClassMap } from '../../utils/theme';

import { iosEnterAnimation } from './animations/ios.enter';
Expand Down Expand Up @@ -163,19 +163,29 @@ export class Picker implements ComponentInterface, OverlayInterface {
return Promise.resolve(this.columns.find(column => column.name === name));
}

private buttonClick(button: PickerButton) {
// if (this.disabled) {
// return;
// }

// keep the time of the most recent button click
// a handler has been provided, execute it
// pass the handler the values from the inputs
const shouldDismiss = safeCall(button.handler, this.getSelected()) !== false;
private async buttonClick(button: PickerButton) {
const role = button.role;
if (isCancel(role)) {
brandyscarney marked this conversation as resolved.
Show resolved Hide resolved
return this.dismiss(undefined, role);
}
const shouldDismiss = await this.callButtonHandler(button);
if (shouldDismiss) {
return this.dismiss();
return this.dismiss(this.getSelected(), button.role);
}
return Promise.resolve(false);
return Promise.resolve();
}

private async callButtonHandler(button: PickerButton | undefined) {
if (button) {
// a handler has been provided, execute it
// pass the handler the values from the inputs
const rtn = await safeCall(button.handler);
if (rtn === false) {
// if the return value of the handler is false then do not dismiss
return false;
}
}
return true;
}

private getSelected() {
Expand All @@ -194,11 +204,14 @@ export class Picker implements ComponentInterface, OverlayInterface {
}

private onBackdropTap = () => {
const cancelBtn = this.buttons.find(b => b.role === 'cancel');
if (cancelBtn) {
this.buttonClick(cancelBtn);
} else {
this.dismiss();
this.dismiss(undefined, BACKDROP);
}

private dispatchCancelHandler = (ev: CustomEvent) => {
const role = ev.detail.role;
if (isCancel(role)) {
const cancelButton = this.buttons.find(b => b.role === 'cancel');
this.callButtonHandler(cancelButton);
}
}

Expand All @@ -219,6 +232,7 @@ export class Picker implements ComponentInterface, OverlayInterface {
zIndex: `${20000 + this.overlayIndex}`
}}
onIonBackdropTap={this.onBackdropTap}
onIonPickerWillDismiss={this.dispatchCancelHandler}
>
<ion-backdrop
visible={this.showBackdrop}
Expand Down
11 changes: 10 additions & 1 deletion core/src/components/picker/test/basic/index.html
Expand Up @@ -48,6 +48,10 @@
const pickerController = document.querySelector('ion-picker-controller');
const pickerElement = await pickerController.create({
buttons: [{
text: 'Cancel',
role: 'cancel',
handler: () => console.log('Clicked Cancel!')
}, {
text: 'Save',
handler: () => console.log('Clicked Save!')
}, {
Expand Down Expand Up @@ -112,7 +116,12 @@
}],
cssClass: customClass
});
return await pickerElement.present();

await pickerElement.present();

const { data, role } = await pickerElement.onDidDismiss();

console.log('Picker dismissed!', data, role);
}
</script>
</body>
Expand Down