Skip to content

Commit b3d93b4

Browse files
authored
fix: platform filter directives (#11)
1 parent 07a4530 commit b3d93b4

File tree

6 files changed

+40
-5
lines changed

6 files changed

+40
-5
lines changed

apps/nativescript-demo-ng/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
},
1212
"devDependencies": {
1313
"@nativescript/android": "8.0.0",
14-
"@nativescript/ios": "8.0.0"
14+
"@nativescript/ios": "8.0.0",
15+
"@nativescript/unit-test-runner": "^2.0.5"
1516
}
1617
}

apps/nativescript-demo-ng/src/tests/platform-filter-components.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export class PlatformSpecificAttributeComponent {
3535
const DECLARATIONS = [PlatformSpecificAttributeComponent, AndroidSpecificComponent, IosSpecificComponent];
3636
@NgModule({
3737
declarations: DECLARATIONS,
38+
imports: [NativeScriptModule],
3839
schemas: [NO_ERRORS_SCHEMA],
3940
})
4041
export class PlatformModule {}
@@ -61,7 +62,8 @@ describe('Platform filter directives', () => {
6162
fixture.detectChanges();
6263
const componentRef = fixture.componentRef;
6364
const componentRoot = componentRef.instance.elementRef.nativeElement;
64-
expect(dumpView(componentRoot, true).indexOf('Label') < 0).toBe(true);
65+
console.log(dumpView(componentRoot, true));
66+
expect(dumpView(componentRoot, true).indexOf('label') < 0).toBe(true);
6567
});
6668
it('applies iOS specific attribute', () => {
6769
const fixture = TestBed.createComponent(PlatformSpecificAttributeComponent);
@@ -94,7 +96,7 @@ describe('Platform filter directives', () => {
9496
fixture.detectChanges();
9597
const componentRef = fixture.componentRef;
9698
const componentRoot = componentRef.instance.elementRef.nativeElement;
97-
expect(dumpView(componentRoot, true).indexOf('Label') < 0).toBe(true);
99+
expect(dumpView(componentRoot, true).indexOf('label') < 0).toBe(true);
98100
});
99101
it('applies Android specific attribute', () => {
100102
const fixture = TestBed.createComponent(PlatformSpecificAttributeComponent);

apps/nativescript-demo-ng/tools/xplat-postinstall.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ try {
1414
}
1515

1616
// Helpful to trigger ngcc after an install to ensure all has processed properly
17-
const child = childProcess.spawn(/^win/.test(process.platform) ? '..\\..\\node_modules\\.bin\\ngcc' : '../../node_modules/.bin/ngcc', ['--tsconfig', 'tsconfig.app.json', '--properties', 'es2015', 'module', 'main', '--first-only'], {
17+
const ngccPath = path.join('..', '..', 'node_modules', '.bin', 'ngcc');
18+
const child = childProcess.spawn(ngccPath, ['--tsconfig', 'tsconfig.app.json', '--properties', 'es2015', 'module', 'main', '--first-only'], {
1819
cwd: process.cwd(),
1920
stdio: 'inherit',
21+
shell: process.platform == 'win32'
2022
});
2123
child.on('close', (code) => {
2224

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* eslint-disable @angular-eslint/component-selector */
2+
import { Component, Inject } from '@angular/core';
3+
import { Device, platformNames } from '@nativescript/core';
4+
import { DEVICE } from '../../tokens';
5+
6+
@Component({
7+
selector: 'android',
8+
template: `<ng-content *ngIf="show"></ng-content>`,
9+
})
10+
export class AndroidFilterComponent {
11+
public show: boolean;
12+
13+
constructor(@Inject(DEVICE) device: typeof Device) {
14+
this.show = device.os === platformNames.android;
15+
}
16+
}
17+
18+
@Component({
19+
selector: 'ios',
20+
template: `<ng-content *ngIf="show"></ng-content>`,
21+
})
22+
export class IosFilterComponent {
23+
public show: boolean;
24+
constructor(@Inject(DEVICE) device: typeof Device) {
25+
console.log(device.os, platformNames.ios);
26+
this.show = device.os === platformNames.ios;
27+
}
28+
}

packages/angular/src/lib/nativescript-common.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import { ListViewComponent, TemplateKeyDirective } from './cdk/list-view/list-vi
66
import { registerNativeScriptViewComponents } from './element-registry';
77
import { ModalDialogService } from './legacy/directives/dialogs';
88
import { TabViewDirective, TabViewItemDirective } from './cdk/tab-view';
9+
import { AndroidFilterComponent, IosFilterComponent } from './cdk/platform-filters';
910

10-
const CDK_COMPONENTS = [ActionBarComponent, ActionBarScope, ActionItemDirective, NavigationButtonDirective, ListViewComponent, TemplateKeyDirective, TabViewDirective, TabViewItemDirective];
11+
const CDK_COMPONENTS = [ActionBarComponent, ActionBarScope, ActionItemDirective, NavigationButtonDirective, ListViewComponent, TemplateKeyDirective, TabViewDirective, TabViewItemDirective, AndroidFilterComponent, IosFilterComponent];
1112

1213
registerNativeScriptViewComponents();
1314

packages/angular/src/lib/public_api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export * from './cdk/list-view';
1414
export * from './cdk/portal';
1515
export * from './cdk/dialog';
1616
export * from './cdk/tab-view';
17+
export * from './cdk/platform-filters';
1718
export * from './file-system';
1819
export * from './nativescript-common.module';
1920
export * from './loading.service';

0 commit comments

Comments
 (0)