mirror of
https://github.com/storybookjs/storybook.git
synced 2025-03-16 05:03:11 +08:00
Add moduleMetdata decorator for supplying common Angular metadata
This commit is contained in:
parent
9bb049afbe
commit
e29d0e758a
1
app/angular/index.d.ts
vendored
1
app/angular/index.d.ts
vendored
@ -1,4 +1,5 @@
|
||||
import { NgModuleMetadata, ICollection } from './dist/client/preview/angular/types';
|
||||
export { moduleMetadata } from './dist/client/preview/angular/decorators';
|
||||
|
||||
export interface IStorybookStory {
|
||||
name: string;
|
||||
|
2
app/angular/src/client/index.js
vendored
2
app/angular/src/client/index.js
vendored
@ -1 +1,3 @@
|
||||
export { storiesOf, setAddon, addDecorator, configure, getStorybook } from './preview';
|
||||
|
||||
export { moduleMetadata } from './preview/angular/decorators';
|
||||
|
42
app/angular/src/client/preview/angular/decorators.test.ts
Normal file
42
app/angular/src/client/preview/angular/decorators.test.ts
Normal file
@ -0,0 +1,42 @@
|
||||
import { moduleMetadata } from './decorators';
|
||||
|
||||
class MockModule {}
|
||||
class MockService {}
|
||||
class MockComponent {}
|
||||
|
||||
describe('moduleMetadata', () => {
|
||||
it('should add metadata to a story without it', () => {
|
||||
const result = moduleMetadata({
|
||||
imports: [MockModule],
|
||||
providers: [MockService],
|
||||
})(() => ({
|
||||
component: MockComponent,
|
||||
}));
|
||||
|
||||
expect(result).toEqual({
|
||||
component: MockComponent,
|
||||
moduleMetadata: {
|
||||
imports: [MockModule],
|
||||
providers: [MockService],
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('should not override individual metadata on a story', () => {
|
||||
const result = moduleMetadata({
|
||||
imports: [MockModule],
|
||||
})(() => ({
|
||||
component: MockComponent,
|
||||
moduleMetadata: {
|
||||
providers: MockService,
|
||||
},
|
||||
}));
|
||||
|
||||
expect(result).toEqual({
|
||||
component: MockComponent,
|
||||
moduleMetadata: {
|
||||
providers: MockService,
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
6
app/angular/src/client/preview/angular/decorators.ts
Normal file
6
app/angular/src/client/preview/angular/decorators.ts
Normal file
@ -0,0 +1,6 @@
|
||||
import { NgModuleMetadata } from './types';
|
||||
|
||||
export const moduleMetadata = (metadata: Partial<NgModuleMetadata>) => (context: () => any) => ({
|
||||
moduleMetadata: metadata,
|
||||
...context(),
|
||||
});
|
@ -113,3 +113,55 @@ npm run storybook
|
||||
|
||||
Now you can change components and write stories whenever you need to.
|
||||
You'll get those changes into Storybook in a snap with the help of webpack's HMR API.
|
||||
|
||||
## Module Metadata
|
||||
|
||||
If your component has dependencies on other Angular directives and modules, these can be supplied using the `moduleMetadata` property on an individual story:
|
||||
|
||||
```js
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { storiesOf } from '@storybook/angular';
|
||||
import { MyButtonComponent } from '../src/app/my-button/my-button.component';
|
||||
import { MyPanelComponent } from '../src/app/my-panel/my-panel.component';
|
||||
import { MyDataService } from '../src/app/my-data/my-data.service';
|
||||
|
||||
storiesOf('My Panel', module)
|
||||
.add('Default', () => ({
|
||||
component: MyPanelComponent,
|
||||
moduleMetadata: {
|
||||
imports: [CommonModule],
|
||||
schemas: [],
|
||||
declarations: [MyButtonComponent],
|
||||
providers: [MyDataService],
|
||||
}
|
||||
}));
|
||||
```
|
||||
|
||||
If you have metadata that is common between your stories, this can configured once using the `moduleMetadata()` decorator:
|
||||
|
||||
```js
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { storiesOf, moduleMetadata } from '@storybook/angular';
|
||||
import { MyButtonComponent } from '../src/app/my-button/my-button.component';
|
||||
import { MyPanelComponent } from '../src/app/my-panel/my-panel.component';
|
||||
import { MyDataService } from '../src/app/my-data/my-data.service';
|
||||
|
||||
storiesOf('My Panel', module)
|
||||
.addDecorator(
|
||||
moduleMetadata({
|
||||
imports: [CommonModule],
|
||||
schemas: [],
|
||||
declarations: [MyButtonComponent],
|
||||
providers: [MyDataService],
|
||||
})
|
||||
)
|
||||
.add('Default', () => ({
|
||||
component: MyPanelComponent
|
||||
}))
|
||||
.add('with a title', () => ({
|
||||
component: MyPanelComponent,
|
||||
props: {
|
||||
title: 'Foo',
|
||||
}
|
||||
}));
|
||||
```
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { storiesOf } from '@storybook/angular';
|
||||
import { storiesOf, moduleMetadata } from '@storybook/angular';
|
||||
import { withKnobs, text } from '@storybook/addon-knobs/angular';
|
||||
|
||||
import { NameComponent } from './moduleMetadata/name.component';
|
||||
@ -35,16 +35,18 @@ storiesOf('Custom Pipe/With Knobs', module)
|
||||
}));
|
||||
|
||||
storiesOf('Custom ngModule metadata', module)
|
||||
.add('simple', () => ({
|
||||
component: ServiceComponent,
|
||||
props: {
|
||||
name: 'Static name',
|
||||
},
|
||||
moduleMetadata: {
|
||||
.addDecorator(
|
||||
moduleMetadata({
|
||||
imports: [],
|
||||
schemas: [],
|
||||
declarations: [],
|
||||
providers: [DummyService],
|
||||
})
|
||||
)
|
||||
.add('simple', () => ({
|
||||
component: ServiceComponent,
|
||||
props: {
|
||||
name: 'Static name',
|
||||
},
|
||||
}))
|
||||
.addDecorator(withKnobs)
|
||||
@ -56,11 +58,5 @@ storiesOf('Custom ngModule metadata', module)
|
||||
props: {
|
||||
name,
|
||||
},
|
||||
moduleMetadata: {
|
||||
imports: [],
|
||||
schemas: [],
|
||||
declarations: [],
|
||||
providers: [DummyService],
|
||||
},
|
||||
};
|
||||
});
|
||||
|
@ -43,7 +43,7 @@ const createOption = ({ defaultValue, option, name, extraParam }) => ({
|
||||
|
||||
const tasks = {
|
||||
core: createProject({
|
||||
name: `Core & React & Vue & Polymer ${chalk.gray('(core)')}`,
|
||||
name: `Core & React & Vue & Polymer & Angular ${chalk.gray('(core)')}`,
|
||||
defaultValue: true,
|
||||
option: '--core',
|
||||
projectLocation: path.join(__dirname, '..'),
|
||||
|
Loading…
x
Reference in New Issue
Block a user