Skip to content

Commit

Permalink
Merge pull request #2518 from storybooks/angular-support-custom-pipes
Browse files Browse the repository at this point in the history
Angular Add custom pipes support
  • Loading branch information
igor-dv authored Dec 20, 2017
2 parents 20828c6 + 4d8e769 commit cb34518
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 6 deletions.
6 changes: 4 additions & 2 deletions addons/knobs/src/angular/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { Component, SimpleChange, ChangeDetectorRef } from '@angular/core';

const getComponentMetadata = ({ component, props = {} }) => {
const getComponentMetadata = ({ component, props = {}, pipes = [] }) => {
if (!component || typeof component !== 'function') throw new Error('No valid component provided');

const componentMeta = component.__annotations__[0] || component.annotations[0];
Expand All @@ -11,6 +11,7 @@ const getComponentMetadata = ({ component, props = {} }) => {
return {
component,
props,
pipes,
componentMeta,
propsMeta,
params: paramsMetadata,
Expand Down Expand Up @@ -95,7 +96,7 @@ const resetKnobs = (knobStore, channel) => {

export function prepareComponent({ getStory, context, channel, knobStore }) {
resetKnobs(knobStore, channel);
const { component, componentMeta, props, propsMeta, params } = getComponentMetadata(
const { component, componentMeta, props, pipes, propsMeta, params } = getComponentMetadata(
getStory(context)
);

Expand All @@ -112,6 +113,7 @@ export function prepareComponent({ getStory, context, channel, knobStore }) {
return {
component: AnnotatedComponent,
props,
pipes,
propsMeta,
};
}
3 changes: 2 additions & 1 deletion app/angular/src/client/preview/angular/app.token.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { InjectionToken } from "@angular/core";
import { InjectionToken, PipeTransform } from "@angular/core";

export const STORY = new InjectionToken<Data>("story");

export type Data = {
component: any;
props: object;
propsMeta: object;
pipes: PipeTransform[];
}
6 changes: 4 additions & 2 deletions app/angular/src/client/preview/angular/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const debounce = (func, wait = 100, immediate = false) => {
};
};

const getComponentMetadata = ({ component, props = {}, propsMeta = {} }) => {
const getComponentMetadata = ({ component, props = {}, propsMeta = {}, pipes = [] }) => {
if (!component || typeof component !== "function")
throw new Error("No valid component provided");

Expand All @@ -50,6 +50,7 @@ const getComponentMetadata = ({ component, props = {}, propsMeta = {} }) => {
return {
component,
props,
pipes,
componentMeta: componentMetadata,
propsMeta: propsMetadata,
params: paramsMetadata
Expand Down Expand Up @@ -88,6 +89,7 @@ const initModule = (currentStory, context, reRender) => {
component,
componentMeta,
props,
pipes,
propsMeta,
params
} = getComponentMetadata(currentStory(context));
Expand All @@ -107,7 +109,7 @@ const initModule = (currentStory, context, reRender) => {
propsMeta
};
const Module = getModule(
[AppComponent, AnnotatedComponent],
[AppComponent, AnnotatedComponent, ...pipes],
[AnnotatedComponent],
[AppComponent],
story
Expand Down
10 changes: 10 additions & 0 deletions examples/angular-cli/src/stories/custom.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
name: 'customPipe'
})
export class CustomPipePipe implements PipeTransform {
transform(value: any, args?: any): any {
return `CustomPipe: ${value}`;
}
}
21 changes: 21 additions & 0 deletions examples/angular-cli/src/stories/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,27 @@ import { Welcome, Button } from '@storybook/angular/demo';
import { SimpleKnobsComponent } from './knobs.component';
import { AllKnobsComponent } from './all-knobs.component';
import { AppComponent } from '../app/app.component';
import { NameComponent } from './name.component';
import { CustomPipePipe } from './custom.pipe';

storiesOf('Custom Pipe', module)
.add('Default', () => ({
component: NameComponent,
props: {
field: 'foobar',
},
pipes: [ CustomPipePipe ],
}));

storiesOf('Custom Pipe/With Knobs', module)
.addDecorator(withKnobs)
.add('NameComponent', () => ({
component: NameComponent,
props: {
field: text('field', 'foobar'),
},
pipes: [ CustomPipePipe ],
}));

storiesOf('Welcome', module)
.add('to Storybook', () => ({
Expand Down
9 changes: 9 additions & 0 deletions examples/angular-cli/src/stories/name.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Component, Input } from '@angular/core';

@Component({
selector: 'name',
template: `<h1>{{ field | customPipe }}</h1>`
})
export class NameComponent {
@Input() field;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"docs:dev": "npm --prefix docs run dev",
"github-release": "github-release-from-changelog",
"lint": "yarn lint:js . && yarn lint:md .",
"lint:js": "NODE_ENV=production eslint --cache --cache-location=.cache/eslint --ext .js,.jsx,.json",
"lint:js": "cross-env NODE_ENV=production eslint --cache --cache-location=.cache/eslint --ext .js,.jsx,.json",
"lint:md": "remark",
"publish": "lerna publish",
"test": "./scripts/test.js",
Expand Down

0 comments on commit cb34518

Please sign in to comment.