Skip to content

Commit a161635

Browse files
authored
Merge pull request #160 from panpan-lin/angular-directive-carbon-links
Create an Angular Directive for Carbon link
2 parents 9c71a96 + 3a441b2 commit a161635

File tree

5 files changed

+139
-2
lines changed

5 files changed

+139
-2
lines changed

src/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export * from "./forms/forms.module";
1212
export * from "./i18n/i18n.module";
1313
export * from "./icon/icon.module";
1414
export * from "./input/input.module";
15+
export * from "./link/link.module";
1516
export * from "./list-group/list-group.module";
1617
export * from "./loading/loading.module";
1718
export * from "./modal/modal.module";
@@ -26,5 +27,3 @@ export * from "./switch/switch.module";
2627
export * from "./table/table.module";
2728
export * from "./tabs/tabs.module";
2829
export * from "./tiles/tiles.module";
29-
export * from "./toggle/toggle.module";
30-
export * from "./utils/position";

src/link/link.directive.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import {
2+
Directive,
3+
HostBinding,
4+
Input
5+
} from "@angular/core";
6+
7+
/**
8+
* A convinence directive for applying styling to a link.
9+
*
10+
* Example:
11+
*
12+
* ```hmtl
13+
* <a href="#" ibmLink>A link</a>
14+
* ```
15+
*
16+
* See the [vanilla carbon docs](http://www.carbondesignsystem.com/components/link/code) for more detail.
17+
*/
18+
@Directive({
19+
selector: "[ibmLink]"
20+
})
21+
22+
export class Link {
23+
@HostBinding("class.bx--link") baseClass = true;
24+
25+
/**
26+
* Automatically set to `-1` when link is disabled.
27+
* @memberof Link
28+
*/
29+
@HostBinding("attr.tabindex") tabindex;
30+
31+
/**
32+
* Set to true to disable link.
33+
* @memberof Link
34+
*/
35+
@Input()
36+
@HostBinding("attr.aria-disabled")
37+
set disabled(disabled: boolean) {
38+
this._disabled = disabled;
39+
this.tabindex = this.disabled ? -1 : null;
40+
}
41+
42+
get disabled(): boolean {
43+
return this._disabled;
44+
}
45+
46+
private _disabled;
47+
}

src/link/link.module.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { NgModule } from "@angular/core";
2+
import { CommonModule } from "@angular/common";
3+
4+
import { Link } from "./link.directive";
5+
6+
export { Link } from "./link.directive";
7+
8+
@NgModule({
9+
declarations: [
10+
Link
11+
],
12+
exports: [
13+
Link
14+
],
15+
imports: [
16+
CommonModule
17+
]
18+
})
19+
export class LinkModule {}

src/link/link.spec.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { TestBed, ComponentFixture } from "@angular/core/testing";
2+
import { Component, Input, DebugElement } from "@angular/core";
3+
import { By } from "@angular/platform-browser";
4+
5+
import { Link } from "./link.directive";
6+
7+
describe("Link", () => {
8+
it("should create a Link", () => {
9+
TestBed.configureTestingModule({
10+
declarations: [TestLinkComponent, Link]
11+
});
12+
13+
let fixture: ComponentFixture<TestLinkComponent> = TestBed.createComponent(TestLinkComponent);
14+
let component: TestLinkComponent = fixture.componentInstance;
15+
fixture.detectChanges();
16+
17+
expect(component).toBeTruthy();
18+
const directiveEl = fixture.debugElement.query(By.directive(Link));
19+
expect(directiveEl).not.toBeNull();
20+
expect(directiveEl.attributes["aria-disabled"]).toBe(null);
21+
expect(directiveEl.attributes["tabindex"]).toBe(null);
22+
expect(directiveEl.attributes["href"]).toBe("https://angular.carbondesignsystem.com/");
23+
});
24+
25+
it("should create a disabled link", () => {
26+
TestBed.configureTestingModule({
27+
declarations: [TestDisabledLinkComponent, Link]
28+
});
29+
30+
let fixture: ComponentFixture<TestDisabledLinkComponent> = TestBed.createComponent(TestDisabledLinkComponent);
31+
let component: TestDisabledLinkComponent = fixture.componentInstance;
32+
fixture.detectChanges();
33+
34+
const directiveEl = fixture.debugElement.query(By.directive(Link));
35+
expect(directiveEl.attributes["aria-disabled"]).toBe("true");
36+
expect(directiveEl.attributes["tabindex"]).toBe("-1");
37+
});
38+
});
39+
40+
@Component({
41+
template: `<a href="https://angular.carbondesignsystem.com/" ibmLink>link</a>`
42+
})
43+
class TestLinkComponent {
44+
}
45+
46+
@Component({
47+
template: `<a href="https://angular.carbondesignsystem.com/" [disabled]="1+1===2" ibmLink>link</a>`
48+
})
49+
class TestDisabledLinkComponent {
50+
}

src/link/link.stories.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { storiesOf, moduleMetadata } from "@storybook/angular";
2+
import { withKnobs, boolean } from "@storybook/addon-knobs/angular";
3+
4+
import { LinkModule } from "../";
5+
6+
storiesOf("Link", module)
7+
.addDecorator(
8+
moduleMetadata({
9+
imports: [
10+
LinkModule
11+
]
12+
})
13+
)
14+
.addDecorator(withKnobs)
15+
.add("Basic", () => ({
16+
template: `
17+
<a href="#" ibmLink [disabled]="disabled">link</a>
18+
`,
19+
props: {
20+
disabled: boolean("disabled", false)
21+
}
22+
}));

0 commit comments

Comments
 (0)