Skip to content

Commit a1a947d

Browse files
authored
feat(module): add module for clearer addition (Tinkoff#22)
* feat(module): add module for clearer addition * chore: tests
1 parent 8f88511 commit a1a947d

File tree

5 files changed

+37
-19
lines changed

5 files changed

+37
-19
lines changed

README.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,31 @@ _preventDefault()_ and _stopPropagation()_.
1313

1414
## How to use
1515

16-
1. Add new providers to your app module:
16+
1. Add `EventPluginsModule` to your app module:
1717

1818
```typescript
1919
import {NgModule} from '@angular/core';
20-
import {NG_EVENT_PLUGINS} from '@tinkoff/ng-event-plugins'; // <-- THIS
20+
import {BrowserModule} from '@angular/platform-browser';
21+
import {EventPluginsModule} from '@tinkoff/ng-event-plugins'; // <-- THIS
2122

2223
@NgModule({
2324
bootstrap: [
2425
/*...*/
2526
],
2627
imports: [
2728
/*...*/
29+
BrowserModule,
30+
EventPluginsModule, // <-- GOES HERE
2831
],
2932
declarations: [
3033
/*...*/
3134
],
32-
providers: NG_EVENT_PLUGINS, // <-- GOES HERE
3335
})
3436
export class AppModule {}
3537
```
3638

39+
> `BrowserModule` or `BrowserAnimationsModule` must go first. You will see a warning if you mess the order.
40+
3741
2. Use new modifiers for events in templates and in `@HostListener`:
3842

3943
- `.stop` to call stopPropagation() on event

projects/demo/src/app/app.browser.module.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {APP_BASE_HREF, LocationStrategy, PathLocationStrategy} from '@angular/common';
22
import {NgModule} from '@angular/core';
33
import {BrowserModule} from '@angular/platform-browser';
4-
import {NG_EVENT_PLUGINS} from '@tinkoff/ng-event-plugins';
4+
import {EventPluginsModule} from '@tinkoff/ng-event-plugins';
55
import {HIGHLIGHT_OPTIONS, HighlightModule} from 'ngx-highlightjs';
66
import {AppComponent} from './app.component';
77
import {AppRoutingModule} from './app.routes';
@@ -11,6 +11,7 @@ import {StaticModule} from './modules/static/static.module';
1111
bootstrap: [AppComponent],
1212
imports: [
1313
BrowserModule.withServerTransition({appId: 'demo'}),
14+
EventPluginsModule,
1415
AppRoutingModule,
1516
StaticModule,
1617
HighlightModule,
@@ -37,7 +38,6 @@ import {StaticModule} from './modules/static/static.module';
3738
},
3839
},
3940
},
40-
NG_EVENT_PLUGINS,
4141
],
4242
})
4343
export class AppBrowserModule {}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import {Inject, NgModule} from '@angular/core';
2+
import {EVENT_MANAGER_PLUGINS} from '@angular/platform-browser';
3+
import {NG_EVENT_PLUGINS} from './constants/plugins';
4+
import {SilentEventPlugin} from './plugins/silent.plugin';
5+
6+
// @dynamic
7+
@NgModule({
8+
providers: NG_EVENT_PLUGINS,
9+
})
10+
export class EventPluginsModule {
11+
constructor(@Inject(EVENT_MANAGER_PLUGINS) plugins: readonly unknown[]) {
12+
console.assert(
13+
!(plugins[0] instanceof SilentEventPlugin),
14+
'EventManagerModule must come after BrowserModule in imports',
15+
);
16+
}
17+
}

projects/ng-event-plugins/src/public-api.ts

+1
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ export * from './plugins/stop.plugin';
1111
export * from './plugins/zone.plugin';
1212
export * from './types/predicate';
1313
export * from './utils/as-callable';
14+
export * from './module';

projects/ng-event-plugins/src/test/test.spec.ts

+10-14
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import {
99
import {ComponentFixture, TestBed} from '@angular/core/testing';
1010
import {By} from '@angular/platform-browser';
1111
import {BehaviorSubject} from 'rxjs';
12-
import {NG_EVENT_PLUGINS} from '../constants/plugins';
1312
import {shouldCall} from '../decorators/should-call';
13+
import {EventPluginsModule} from '../module';
1414
import {asCallable} from '../utils/as-callable';
1515

1616
describe('EventManagers', () => {
@@ -33,11 +33,11 @@ describe('EventManagers', () => {
3333
(click.silent)="onFilteredClicks($event.bubbles)"
3434
></div>
3535
</div>
36-
<div class="wrapper" (click.capture.stop)="noop()">
36+
<div class="wrapper" (click.capture.stop)="(0)">
3737
<div id="captured-clicks" class="element" (click)="onCaptured()"></div>
3838
</div>
3939
<div class="wrapper" (click.self)="onBubbled()">
40-
<div id="bubbled-clicks" class="element" (click)="noop()"></div>
40+
<div id="bubbled-clicks" class="element" (click)="(0)"></div>
4141
</div>
4242
`,
4343
changeDetection: ChangeDetectionStrategy.OnPush,
@@ -54,8 +54,8 @@ describe('EventManagers', () => {
5454
@HostListener('$.data-value.attr')
5555
@HostBinding('$.tabIndex')
5656
@HostListener('$.tabIndex')
57-
@HostBinding('$.style.width.%')
58-
@HostListener('$.style.width.%')
57+
@HostBinding('$.style.marginTop.%')
58+
@HostListener('$.style.marginTop.%')
5959
@HostBinding('$.class.active')
6060
@HostListener('$.class.active')
6161
readonly test = asCallable(new BehaviorSubject<number | null>(1));
@@ -69,25 +69,21 @@ describe('EventManagers', () => {
6969
onFilteredClicks(_bubbles: boolean) {
7070
this.flag = true;
7171
}
72-
73-
noop() {}
7472
}
7573

7674
@Component({
77-
template: `<div (document:click.capture)="noop()"></div>`,
75+
template: `<div (document:click.capture)="(0)"></div>`,
7876
changeDetection: ChangeDetectionStrategy.OnPush,
7977
})
80-
class BrokenComponent {
81-
noop() {}
82-
}
78+
class BrokenComponent {}
8379

8480
let fixture: ComponentFixture<TestComponent>;
8581
let testComponent: TestComponent;
8682

8783
beforeEach(() => {
8884
TestBed.configureTestingModule({
85+
imports: [EventPluginsModule],
8986
declarations: [TestComponent, BrokenComponent],
90-
providers: NG_EVENT_PLUGINS,
9187
});
9288

9389
fixture = TestBed.createComponent(TestComponent);
@@ -195,7 +191,7 @@ describe('EventManagers', () => {
195191
'1',
196192
);
197193
expect(testComponent.elementRef.nativeElement.tabIndex).toBe(1);
198-
expect(testComponent.elementRef.nativeElement.style.width).toBe('1%');
194+
expect(testComponent.elementRef.nativeElement.style.marginTop).toBe('1%');
199195
expect(testComponent.elementRef.nativeElement.classList.contains('active')).toBe(
200196
true,
201197
);
@@ -208,7 +204,7 @@ describe('EventManagers', () => {
208204
null,
209205
);
210206
expect(testComponent.elementRef.nativeElement.tabIndex).toBe(0);
211-
expect(testComponent.elementRef.nativeElement.style.width).toBe('1%');
207+
expect(testComponent.elementRef.nativeElement.style.marginTop).toBe('1%');
212208
expect(testComponent.elementRef.nativeElement.classList.contains('active')).toBe(
213209
false,
214210
);

0 commit comments

Comments
 (0)