From 7ff0159071443188016b9ff9d91b56289a01d4aa Mon Sep 17 00:00:00 2001 From: Mark Wallace Date: Tue, 4 Sep 2018 16:38:46 +0100 Subject: [PATCH] Add new tiles module with basic and clickable tile components --- package.json | 2 +- src/index.ts | 1 + src/tiles/clickable-tile.component.spec.ts | 48 +++++++++++++++++ src/tiles/clickable-tile.component.ts | 63 ++++++++++++++++++++++ src/tiles/tile.component.spec.ts | 30 +++++++++++ src/tiles/tile.component.ts | 27 ++++++++++ src/tiles/tiles.module.ts | 25 +++++++++ src/tiles/tiles.stories.ts | 51 ++++++++++++++++++ 8 files changed, 246 insertions(+), 1 deletion(-) create mode 100644 src/tiles/clickable-tile.component.spec.ts create mode 100644 src/tiles/clickable-tile.component.ts create mode 100644 src/tiles/tile.component.spec.ts create mode 100644 src/tiles/tile.component.ts create mode 100644 src/tiles/tiles.module.ts create mode 100644 src/tiles/tiles.stories.ts diff --git a/package.json b/package.json index 6e5607ed19..a79249a8a6 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "storybook": "start-storybook -p 6006", "docs:build": "compodoc src -p tsconfig.json", "docs:server": "compodoc src -p tsconfig.json -s -w", - "lint": "tslint -e spec.ts src/**/*.ts && stylelint src/**/*.scss --config .stylelintrc.yml", + "lint": "tslint src/**/*.ts && stylelint src/**/*.scss --config .stylelintrc.yml", "test": "karma start", "test:watch": "karma start --auto-watch --no-single-run", "utils:add": "git subtree add --prefix=src/utils git@github.ibm.com:peretz/utils.git master --squash", diff --git a/src/index.ts b/src/index.ts index 5c16e45191..a93a0566c1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -20,3 +20,4 @@ export * from "./switch/switch.module"; export * from "./radio/radio.module"; export * from "./input/input.module"; export * from "./select/select.module"; +export * from "./tiles/tiles.module"; diff --git a/src/tiles/clickable-tile.component.spec.ts b/src/tiles/clickable-tile.component.spec.ts new file mode 100644 index 0000000000..865ce380f2 --- /dev/null +++ b/src/tiles/clickable-tile.component.spec.ts @@ -0,0 +1,48 @@ +/// + +import { DebugElement, Component } from "@angular/core"; +import { + async, + ComponentFixture, + TestBed +} from "@angular/core/testing"; +import { By } from "@angular/platform-browser"; + +import { ClickableTile } from "./clickable-tile.component"; + +describe("ClickableTile", () => { + beforeEach(async(() => { + TestBed.configureTestingModule({ + declarations: [ClickableTile, ClickableTileTestComponent], + }).compileComponents(); + })); + + it("should create a ClickableTile", () => { + let fixture: ComponentFixture = TestBed.createComponent(ClickableTile); + let component: ClickableTile = fixture.componentInstance; + fixture.detectChanges(); + + expect(component).toBeTruthy(); + }); + + it("should create a disabled ClickableTile", () => { + let fixture: ComponentFixture = TestBed.createComponent(ClickableTileTestComponent); + let component: ClickableTileTestComponent = fixture.componentInstance; + fixture.detectChanges(); + + const directiveEl = fixture.debugElement.query(By.directive(ClickableTile)); + expect(directiveEl).not.toBeNull(); + expect(directiveEl.attributes["disabled"]).toBeTruthy(); + expect(directiveEl.attributes["href"]).toBe("https://angular.carbondesignsystem.com/"); + }); +}); + +@Component({ + selector: "test-cmp", + template: ` + + Test Clickable Tile + `, + entryComponents: [ClickableTile] +}) +class ClickableTileTestComponent {} diff --git a/src/tiles/clickable-tile.component.ts b/src/tiles/clickable-tile.component.ts new file mode 100644 index 0000000000..426adfa6d2 --- /dev/null +++ b/src/tiles/clickable-tile.component.ts @@ -0,0 +1,63 @@ +import { + Component, + Input +} from "@angular/core"; + +/** + * Build application's clickable tiles using this component. + * + * ## Basic usage + * + * ```html + * + * tile content + * + * ``` + * + * @export + * @class ClickableTile + * @implements {OnInit} + */ +@Component({ + selector: "ibm-clickable-tile", + template: ` + + + ` +}) +export class ClickableTile { + /** + * Sets the `href` attribute on the `ibm-clickable-tile` element. + * @type {string} + * @memberof ClickableTile + */ + @Input() href: string; + + /** + * Set to `true` to disable the clickable tile. + * @type {boolean} + * @memberof ClickableTile + */ + @Input() disabled = false; + + clicked = false; + + onClick(event) { + this.clicked = !this.clicked; + } + + onKeyDown(event) { + if (event.key === "Enter" || event.key === " ") { + this.clicked = !this.clicked; + } + } +} diff --git a/src/tiles/tile.component.spec.ts b/src/tiles/tile.component.spec.ts new file mode 100644 index 0000000000..b4e3e13802 --- /dev/null +++ b/src/tiles/tile.component.spec.ts @@ -0,0 +1,30 @@ +/// + +import { + async, + ComponentFixture, + TestBed +} from "@angular/core/testing"; + +import { Tile } from "./tile.component"; + +describe("Tile", () => { + let component: Tile; + let fixture: ComponentFixture; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + declarations: [Tile], + }).compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(Tile); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it("should create a Tile", () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/src/tiles/tile.component.ts b/src/tiles/tile.component.ts new file mode 100644 index 0000000000..45b895c8e3 --- /dev/null +++ b/src/tiles/tile.component.ts @@ -0,0 +1,27 @@ +import { + Component, + HostBinding +} from "@angular/core"; + +/** + * Build application's tiles using this component. + * + * ## Basic usage + * + * ```html + * + * tile content + * + * ``` + * + * @export + * @class Tile + * @implements {OnInit} + */ +@Component({ + selector: "ibm-tile", + template: `` +}) +export class Tile { + @HostBinding("class") tileClass = "bx--tile"; +} diff --git a/src/tiles/tiles.module.ts b/src/tiles/tiles.module.ts new file mode 100644 index 0000000000..710eec72f6 --- /dev/null +++ b/src/tiles/tiles.module.ts @@ -0,0 +1,25 @@ +import { NgModule } from "@angular/core"; +import { CommonModule } from "@angular/common"; +import { TranslateModule } from "@ngx-translate/core"; + +import { Tile } from "./tile.component"; +import { ClickableTile } from "./clickable-tile.component"; + +export { Tile } from "./tile.component"; +export { ClickableTile } from "./clickable-tile.component"; + +@NgModule({ + declarations: [ + Tile, + ClickableTile + ], + exports: [ + Tile, + ClickableTile + ], + imports: [ + CommonModule, + TranslateModule.forChild() + ] +}) +export class TilesModule {} diff --git a/src/tiles/tiles.stories.ts b/src/tiles/tiles.stories.ts new file mode 100644 index 0000000000..8232dbbc4f --- /dev/null +++ b/src/tiles/tiles.stories.ts @@ -0,0 +1,51 @@ +import { storiesOf, moduleMetadata } from "@storybook/angular"; +import { withKnobs } from "@storybook/addon-knobs/angular"; + +import { TranslateModule } from "@ngx-translate/core"; + +import { TilesModule } from "../"; + +storiesOf("Tiles", module) + .addDecorator( + moduleMetadata({ + imports: [ + TilesModule, + TranslateModule.forRoot() + ] + }) + ) + .addDecorator(withKnobs) + .add("Basic", () => ({ + template: ` + + tile content goes here... + + ` + })) + .add("Multiple", () => ({ + template: ` +
+ + Tile 1 + + + Tile 2 + + + Tile 3 + +
+ ` + })) + .add("Clickable", () => ({ + template: ` +

Normal

+ + clickable tile content goes here... + +

Disabled

+ + clickable tile content goes here... + + ` + }));