Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Angular Add custom pipes support #2518

Merged
merged 3 commits into from
Dec 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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