-
Notifications
You must be signed in to change notification settings - Fork 23
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
Add backButtonClicked event to page component #2316
Add backButtonClicked event to page component #2316
Conversation
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.
It seems like the default behaviour has broken somehow 🤔
Here's how the back button behaves in our showcase without any backButtonClicked
event handler registered on the main
branch:
main-branch.mp4
On this branch nothing happens however.
@@ -161,6 +161,7 @@ export class PageComponent | |||
@Output() enter = new EventEmitter<void>(); | |||
@Output() leave = new EventEmitter<void>(); | |||
@Output() refresh = new EventEmitter<PullToRefreshEvent>(); | |||
@Output() backButtonClicked = new EventEmitter<Event>(); |
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.
Looking at this again i think we should change the name to be in imperative form, considering other events are named like that as well :D
@Output() backButtonClicked = new EventEmitter<Event>(); | |
@Output() backButtonClick = new EventEmitter<Event>(); |
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.
True, changed
@@ -259,6 +262,16 @@ export class PageComponent | |||
this.windowRef.nativeWindow.addEventListener(selectedTabClickEvent, () => { | |||
this.content.scrollToTop(KirbyAnimation.Duration.LONG); | |||
}); | |||
|
|||
// Intercept back-button click events, defaulting to the built-in click-handler. | |||
if (this.backButtonClicked.length == 0) { |
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.
Are there a specific reason for using the equality - instead of the identity operator?
Personally i prefer the more strict identity operator as it is more predictable how it will behave 😄
if (this.backButtonClicked.length == 0) { | |
if (this.backButtonClicked.length === 0) { |
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.
Doh! Done :-)
// Intercept back-button click events, defaulting to the built-in click-handler. | ||
if (this.backButtonClicked.length == 0) { | ||
this.backButtonClicked | ||
.pipe(takeUntil(this.ngOnDestroy$)) | ||
.subscribe(this.backButtonDelegate.onClick.bind(this.backButtonDelegate)); | ||
} | ||
this.backButtonDelegate.onClick = (event: Event) => { | ||
this.backButtonClicked.emit(event); | ||
}; |
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.
We could consider separating all of this into a InterceptBackButtonClicksSetup
method (or similar) to make it more clear that this code belongs together and what its responsibility is
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.
Added
describe('with a back-button-clicked observer', () => { | ||
it('should emit an event when the back button is clicked', () => { | ||
const ionBackButton = spectator.queryHost('ion-toolbar ion-buttons ion-back-button'); | ||
const subscriber = jasmine.createSpy(); | ||
spectator.output('backButtonClicked').subscribe(subscriber); | ||
|
||
spectator.click(ionBackButton); | ||
spectator.detectComponentChanges(); | ||
|
||
expect(subscriber).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
|
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.
We could also add a test which checks that the default behaviour is executed whenever no observer is present
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.
Added
de9e84b
to
e7d9041
Compare
e7d9041
to
fc9fa15
Compare
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.
One minor documentation thing other than that it looks goodie!
@@ -86,6 +86,12 @@ export class PageShowcaseComponent { | |||
description: 'Emitted when leaving the page', | |||
signature: 'func', | |||
}, | |||
{ | |||
name: 'backButtonClicked', |
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.
name: 'backButtonClicked', | |
name: 'backButtonClick', |
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.
Updated and regenerated mock
4d19f42
to
a998ff9
Compare
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.
Which issue does this PR close?
This PR closes #2119
What is the new behavior?
Add a back-button event-emitter outpput option to page component. When set the default back-button behaviour is removed.
Does this PR introduce a breaking change?
Are there any additional context?
This change allows applications to navigate past the routing restrictions present in micro-frontends.
Checklist:
The following tasks should be carried out in sequence in order to follow the process of contributing correctly.
Reminders
Review
When the pull request has been approved it will be merged to
develop
by Team Kirby.