mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 11:42:04 +08:00
fix(modal): innerView.closeModal(...) not passing back context (#5833)
This commit is contained in:
@ -24,6 +24,7 @@ import { Label } from "tns-core-modules/ui/label";
|
||||
import { Color } from "tns-core-modules/color";
|
||||
import { TabView, TabViewItem } from "tns-core-modules/ui/tab-view/tab-view";
|
||||
import { _resetRootView, getRootView } from "tns-core-modules/application";
|
||||
import { Button } from "tns-core-modules/ui/button/button";
|
||||
|
||||
export function addLabelToPage(page: Page, text?: string) {
|
||||
const label = new Label();
|
||||
@ -421,6 +422,84 @@ export function test_WhenPageIsNavigatedToFrameCurrentPageIsNowTheSameAsThePage(
|
||||
page.off(Label.loadedEvent, navigatedEventHandler);
|
||||
}
|
||||
|
||||
export function test_WhenInnerViewCallsCloseModal_WithArguments_ShouldPassResult() {
|
||||
_test_WhenInnerViewCallsCloseModal((args: ShownModallyData) =>
|
||||
{
|
||||
const page = <Page>args.object;
|
||||
const button = <Button>page.content;
|
||||
return button.closeModal.bind(button);
|
||||
}, "return value");
|
||||
}
|
||||
|
||||
export function test_WhenInnerViewCallsCloseModal_WithoutArguments_ShouldWork() {
|
||||
_test_WhenInnerViewCallsCloseModal((args: ShownModallyData) =>
|
||||
{
|
||||
const page = <Page>args.object;
|
||||
const button = <Button>page.content;
|
||||
return button.closeModal.bind(button);
|
||||
});
|
||||
}
|
||||
|
||||
export function test_WhenInnerViewCallsCloseCallback_WithArguments_ShouldPassResult() {
|
||||
_test_WhenInnerViewCallsCloseModal((args: ShownModallyData) =>
|
||||
{
|
||||
return args.closeCallback;
|
||||
}, "return value");
|
||||
}
|
||||
|
||||
export function test_WhenInnerViewCallsCloseCallback_WithoutArguments_ShouldWork() {
|
||||
_test_WhenInnerViewCallsCloseModal((args: ShownModallyData) =>
|
||||
{
|
||||
return args.closeCallback;
|
||||
});
|
||||
}
|
||||
|
||||
function _test_WhenInnerViewCallsCloseModal(closeModalGetter: (ShownModallyData) => Function, result?: any) {
|
||||
let modalClosedWithResult = false;
|
||||
|
||||
const modalCloseCallback = function (returnValue: any) {
|
||||
modalClosedWithResult = returnValue === result;
|
||||
}
|
||||
|
||||
const modalPageShownModallyEventHandler = function(args: ShownModallyData) {
|
||||
const page = <Page>args.object;
|
||||
page.off(View.shownModallyEvent, modalPageShownModallyEventHandler);
|
||||
|
||||
closeModalGetter(args)(result);
|
||||
}
|
||||
|
||||
const hostNavigatedToEventHandler = function(args: NavigatedData) {
|
||||
const page = <Page>args.object;
|
||||
page.off(Page.navigatedToEvent, hostNavigatedToEventHandler);
|
||||
|
||||
const modalPage = new Page();
|
||||
modalPage.id = "modalPage_test_WhenInnerViewCallsCloseModal_WithArguments_ShouldPassResult";
|
||||
modalPage.on(View.shownModallyEvent, modalPageShownModallyEventHandler);
|
||||
|
||||
const button = new Button();
|
||||
button.text = "CLOSE MODAL";
|
||||
modalPage.content = button;
|
||||
|
||||
(<Button>page.content).showModal(modalPage, {}, modalCloseCallback);
|
||||
}
|
||||
|
||||
const masterPageFactory = function(): Page {
|
||||
const masterPage = new Page();
|
||||
masterPage.id = "masterPage_test_WhenInnerViewCallsCloseModal_WithArguments_ShouldPassResult";
|
||||
masterPage.on(Page.navigatedToEvent, hostNavigatedToEventHandler)
|
||||
|
||||
const button = new Button();
|
||||
button.text = "TAP";
|
||||
masterPage.content = button;
|
||||
|
||||
return masterPage;
|
||||
};
|
||||
|
||||
helper.navigate(masterPageFactory);
|
||||
|
||||
TKUnit.waitUntilReady(() => modalClosedWithResult);
|
||||
}
|
||||
|
||||
export function test_WhenViewBaseCallsShowModal_WithArguments_ShouldOpenModal() {
|
||||
let modalClosed = false;
|
||||
|
||||
|
@ -141,8 +141,9 @@ export abstract class ViewBase extends Observable {
|
||||
|
||||
/**
|
||||
* Closes the current modal view that this page is showing.
|
||||
* @param context - Any context you want to pass back to the host when closing the modal view.
|
||||
*/
|
||||
closeModal(): void;
|
||||
closeModal(context?: any): void;
|
||||
|
||||
public effectiveMinWidth: number;
|
||||
public effectiveMinHeight: number;
|
||||
|
@ -949,10 +949,10 @@ export abstract class ViewBase extends Observable implements ViewBaseDefinition
|
||||
return parent && parent.showModal(...args);
|
||||
}
|
||||
|
||||
public closeModal(): void {
|
||||
public closeModal(...args): void {
|
||||
const parent = this.parent;
|
||||
if (parent) {
|
||||
parent.closeModal();
|
||||
parent.closeModal(...args);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -232,14 +232,14 @@ export abstract class ViewCommon extends ViewBase implements ViewDefinition {
|
||||
}
|
||||
}
|
||||
|
||||
public closeModal() {
|
||||
public closeModal(...args) {
|
||||
let closeCallback = this._closeModalCallback;
|
||||
if (closeCallback) {
|
||||
closeCallback.apply(undefined, arguments);
|
||||
} else {
|
||||
let parent = this.parent;
|
||||
if (parent) {
|
||||
parent.closeModal();
|
||||
parent.closeModal(...args);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user