-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
feat(dialog): add beforeClose method #6377
Conversation
src/lib/dialog/dialog.spec.ts
Outdated
|
||
viewContainerFixture.whenStable().then(() => { | ||
expect(beforeCloseCallback).toHaveBeenCalledWith('Bulbasaurus'); | ||
expect(overlayContainerElement.querySelector('md-dialog-container')).toBeNull(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test checks that beforeClose
occurred, but not when it occurred. I'd restructure to be like
let beforeCloseInvoked = false;
const beforeCloseHandler = () => {
expect(overlayContainerElement.querySelector('md-dialog-container')).toBeDefined();
beforeCloseInvoked = true;
});
...
viewContainerFixture.whenStable().then(() => {
expect(beforeCloseCallback).toHaveBeenCalledWith('Bulbasaurus');
expect(overlayContainerElement.querySelector('md-dialog-container')).toBeNull();
});
fdaaaad
to
c262622
Compare
@jelbourn update to the test has been made. Let me know if I misinterpreted your comment! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just one style nit
src/lib/dialog/dialog.spec.ts
Outdated
@@ -140,6 +140,25 @@ describe('MdDialog', () => { | |||
}); | |||
})); | |||
|
|||
it('should close a dialog and get back a result before it is closed', async(() => { | |||
const dialogRef = dialog.open(PizzaMsg, { viewContainerRef: testViewContainerRef }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Omit spaces inside braces
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
I must be missing something, but how do we cancel the close in beforeClose()? |
@kdubious check out #4647 (comment) |
@willshowell, thanks. I read it, but still have a point of confusion. I'm trying to pass a DTO to a dialog. The dialog has a form (ReactiveForm) that displays the DTO, the user makes their edits, then clicks "Save." At that point, I want the parent component (it maintains a list of the objects being edited in the dialog) to be notified about the current state of the object, so that the parent can manage saving to a remote service. If unsuccessful, the parent would prevent the dialog from closing. Is there another approach I should consider? |
@kdubious I probably should've linked to this thread #3460, particularly #3460 (comment). Basically It sounds like in your case, you need to run the async validation before the dialog actually closes. If communication with the parent component is the issue, try using a service. |
Thanks @willshowell. I ended up going with the service. |
@willshowell I really don't see any real use case for beforeClose as it is currently implemented? As requested it would make much more sense if you could cancel the close event (e.g. asking from user if he wants to discard the changes). In that use case it doesn't matter where you get the close event (is it esc, backdrop click or even dialogRef.close()). You should be able to intercept all those and cancel the close if you wish? |
@wiikka one of the use cases for beforeClose that was mentioned in the original issue was when you don't care how the dialog was closed, and you don't care to prevent it, but you want to do something with the resulting value without having to wait for the animation to complete. The conversation in #3460, and more specifically #3460 (comment), pretty much covers why an all-encompassing "prevent/cancel close" api wasn't chosen. |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Fixes #4647