-
Notifications
You must be signed in to change notification settings - Fork 13.4k
feat(angular): add custom injector support for modal and popover controllers #30899
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
Open
ShaneK
wants to merge
4
commits into
feature-8.8
Choose a base branch
from
IONIC-38
base: feature-8.8
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import type { Injector } from '@angular/core'; | ||
| import type { ModalOptions, PopoverOptions } from '@ionic/core/components'; | ||
|
|
||
| export interface AngularModalOptions extends ModalOptions { | ||
| injector?: Injector; | ||
| } | ||
|
|
||
| export interface AngularPopoverOptions extends PopoverOptions { | ||
| injector?: Injector; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
packages/angular/test/base/e2e/src/standalone/modal-custom-injector.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import { test, expect } from '@playwright/test'; | ||
|
|
||
| test.describe('Modal: Custom Injector', () => { | ||
| test.beforeEach(async ({ page }) => { | ||
| await page.goto('/standalone/modal-custom-injector'); | ||
| }); | ||
|
|
||
| test('should inject custom service via custom injector', async ({ page }) => { | ||
| await page.locator('ion-button#open-modal-with-custom-injector').click(); | ||
|
|
||
| await expect(page.locator('ion-modal')).toBeVisible(); | ||
|
|
||
| const serviceValue = page.locator('#service-value'); | ||
| await expect(serviceValue).toHaveText('Service Value: custom-injector-value'); | ||
|
|
||
| await page.locator('#close-modal').click(); | ||
| await expect(page.locator('ion-modal')).not.toBeVisible(); | ||
| }); | ||
|
|
||
| test('should fail without custom injector when service is not globally provided', async ({ page }) => { | ||
| page.on('dialog', async (dialog) => { | ||
| expect(dialog.message()).toContain('TestService not available'); | ||
| await dialog.accept(); | ||
| }); | ||
|
|
||
| await page.locator('ion-button#open-modal-without-custom-injector').click(); | ||
|
|
||
| await page.waitForEvent('dialog'); | ||
| }); | ||
| }); |
16 changes: 16 additions & 0 deletions
16
packages/angular/test/base/e2e/src/standalone/popover-custom-injector.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { test, expect } from '@playwright/test'; | ||
|
|
||
| test.describe('Popover: Custom Injector', () => { | ||
| test.beforeEach(async ({ page }) => { | ||
| await page.goto('/standalone/popover-custom-injector'); | ||
| }); | ||
|
|
||
| test('should inject custom service via custom injector', async ({ page }) => { | ||
| await page.locator('ion-button#open-popover-with-custom-injector').click(); | ||
|
|
||
| await expect(page.locator('ion-popover')).toBeVisible(); | ||
|
|
||
| const serviceValue = page.locator('#service-value'); | ||
| await expect(serviceValue).toHaveText('Service Value: custom-injector-value'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...lar/test/base/src/app/standalone/modal-custom-injector/modal-custom-injector.component.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import { Component, inject, Injector } from '@angular/core'; | ||
| import { IonContent, IonHeader, IonTitle, IonToolbar, IonButton, ModalController } from '@ionic/angular/standalone'; | ||
| import { ModalCustomInjectorModalComponent } from './modal/modal.component'; | ||
| import { TestService } from './test.service'; | ||
|
|
||
| @Component({ | ||
| selector: 'app-modal-custom-injector', | ||
| template: ` | ||
| <ion-header> | ||
| <ion-toolbar> | ||
| <ion-title>Modal Custom Injector Test</ion-title> | ||
| </ion-toolbar> | ||
| </ion-header> | ||
| <ion-content class="ion-padding"> | ||
| <ion-button id="open-modal-with-custom-injector" (click)="openWithCustomInjector()"> | ||
| Open Modal with Custom Injector | ||
| </ion-button> | ||
| <ion-button id="open-modal-without-custom-injector" (click)="openWithoutCustomInjector()"> | ||
| Open Modal without Custom Injector | ||
| </ion-button> | ||
| </ion-content> | ||
| `, | ||
| standalone: true, | ||
| imports: [IonContent, IonHeader, IonTitle, IonToolbar, IonButton] | ||
| }) | ||
| export class ModalCustomInjectorComponent { | ||
| private modalController = inject(ModalController); | ||
| private injector = inject(Injector); | ||
|
|
||
| async openWithCustomInjector() { | ||
| const testService = new TestService(); | ||
| testService.setValue('custom-injector-value'); | ||
|
|
||
| const customInjector = Injector.create({ | ||
| providers: [{ provide: TestService, useValue: testService }], | ||
| parent: this.injector, | ||
| }); | ||
|
|
||
| const modal = await this.modalController.create({ | ||
| component: ModalCustomInjectorModalComponent, | ||
| injector: customInjector, | ||
| }); | ||
|
|
||
| await modal.present(); | ||
| } | ||
|
|
||
| async openWithoutCustomInjector() { | ||
| try { | ||
| const modal = await this.modalController.create({ | ||
| component: ModalCustomInjectorModalComponent, | ||
| }); | ||
| await modal.present(); | ||
| } catch (e) { | ||
| alert('Error: TestService not available without custom injector'); | ||
| } | ||
| } | ||
| } |
35 changes: 35 additions & 0 deletions
35
packages/angular/test/base/src/app/standalone/modal-custom-injector/modal/modal.component.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import { Component, OnInit, inject } from '@angular/core'; | ||
| import { IonContent, IonHeader, IonTitle, IonToolbar, IonButton, IonButtons } from '@ionic/angular/standalone'; | ||
| import { TestService } from '../test.service'; | ||
|
|
||
| @Component({ | ||
| selector: 'app-modal-custom-injector-modal', | ||
| template: ` | ||
| <ion-header> | ||
| <ion-toolbar> | ||
| <ion-title>Modal with Custom Injector</ion-title> | ||
| <ion-buttons slot="end"> | ||
| <ion-button id="close-modal" (click)="dismiss()">Close</ion-button> | ||
| </ion-buttons> | ||
| </ion-toolbar> | ||
| </ion-header> | ||
| <ion-content> | ||
| <p id="service-value">Service Value: {{ serviceValue }}</p> | ||
| </ion-content> | ||
| `, | ||
| standalone: true, | ||
| imports: [IonContent, IonHeader, IonTitle, IonToolbar, IonButton, IonButtons] | ||
| }) | ||
| export class ModalCustomInjectorModalComponent implements OnInit { | ||
| private testService = inject(TestService); | ||
| serviceValue = ''; | ||
| modal: HTMLIonModalElement | undefined; | ||
|
|
||
| ngOnInit() { | ||
| this.serviceValue = this.testService.getValue(); | ||
| } | ||
|
|
||
| dismiss() { | ||
| this.modal?.dismiss(); | ||
| } | ||
| } |
14 changes: 14 additions & 0 deletions
14
packages/angular/test/base/src/app/standalone/modal-custom-injector/test.service.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { Injectable } from '@angular/core'; | ||
|
|
||
| @Injectable() | ||
| export class TestService { | ||
| private value = 'default-value'; | ||
|
|
||
| setValue(value: string) { | ||
| this.value = value; | ||
| } | ||
|
|
||
| getValue(): string { | ||
| return this.value; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I'm not sure if using
Angularas the prefix is consistent with how the other types are set up. Wouldn't it make more sense to call itIonicModalOptionsandIonicPopoverOptions?Uh oh!
There was an error while loading. Please reload this page.
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.
I'm not sure, honestly. We're kind of all over the place with our naming. For me, it makes sense that package-specific options would be named after our packages, but we have names kinda everywhere. Like:
IonicVueRouterOptionsdoes Ionic{Package}{Thing}IonicGlobalandIonicWindowwhich is just Ionic{Thing}NavigationOptions, which doesn't have Ionic at all or AngularAngularDelegateandAngularFrameworkDelegatewhich is in line with this namingIonicReactProps, which is Ionic{Package}{Thing} and it also hasHookOverlayOptionswhich has neither Ionic nor React.Probably the most widely used of these is
IonicVueRouterOptions, and I could see us going for exampleIonicAngularModalOptions. What do you think?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.
I lean towards using
IonicAngularModalOptions... but it also feels a bit too longer. @brandyscarney you have a knack on naming, thoughts?Also would be great if we can create a ticket to try to sync the naming especially since Ionic React router is being worked on.