Skip to content

Commit 0a858d0

Browse files
committed
Create an Angular Directive for Carbon link
1 parent dad486a commit 0a858d0

File tree

5 files changed

+120
-2
lines changed

5 files changed

+120
-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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
* Set to `true` to disable link.
26+
* @memberof Link
27+
*/
28+
@HostBinding("attr.aria-disabled") @Input() disabled = false;
29+
}

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: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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("false");
21+
expect(directiveEl.attributes["href"]).toBe("https://angular.carbondesignsystem.com/");
22+
});
23+
24+
it("should create a disabled link", () => {
25+
TestBed.configureTestingModule({
26+
declarations: [TestDisabledLinkComponent, Link]
27+
});
28+
29+
let fixture: ComponentFixture<TestDisabledLinkComponent> = TestBed.createComponent(TestDisabledLinkComponent);
30+
let component: TestDisabledLinkComponent = fixture.componentInstance;
31+
fixture.detectChanges();
32+
33+
const directiveEl = fixture.debugElement.query(By.directive(Link));
34+
expect(directiveEl.attributes["disabled"]).toBe("true");
35+
expect(directiveEl.attributes["aria-disabled"]).toBe("true");
36+
});
37+
});
38+
39+
@Component({
40+
template: `<a href="https://angular.carbondesignsystem.com/" ibmLink>link</a>`
41+
})
42+
class TestLinkComponent {
43+
}
44+
45+
@Component({
46+
template: `<a href="https://angular.carbondesignsystem.com/" disabled="true" ibmLink>link</a>`
47+
})
48+
class TestDisabledLinkComponent {
49+
}

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)