简体   繁体   中英

React Storybook addon Knobs for editing Angular props

I could not edit values of props using Knobs in Storybook using Angular 2+. Knobs Github's readme.md has following line

Storybook Addon Knobs allow you to edit React props dynamically using the Storybook UI. You can also use Knobs as a dynamic variable inside stories in Storybook.

Does that mean it's not possible with Angular, at least for now? My code is inside index.stories.ts:

import { storiesOf } from '@storybook/angular';
import { boolean, number, text,  button, array, select, selectV2, color, 
object, withKnobs, withKnobsOptions} from '@storybook/addon-knobs/angular';

const stories = storiesOf('Storybook Knobs',module);
stories.addDecorator(withKnobs);  
stories.add('with knobs', () => ({ 
   props:{
        Name:text('Name', 'John'),
        age:number('Age',47)
        },
        template:`My name is ${Name}, I'm ${age} years old`          
       }) );

Previously, I also tried using a component rather than template but I could not change the value of props like shown here and here . Any links to sample or article will be highly appreciated.

there's a full example in the Storybook repo: https://github.com/storybooks/storybook/blob/master/examples/angular-cli/src/stories/addon-knobs.stories.ts

Also, the code you posted alone doesn't provide much context. If you could create a git repo that'd be great.

This is the example we have in our repository, please test it and if it doesn't work do not hesitate and create an issue

import { storiesOf } from '@storybook/angular';
import { action } from '@storybook/addon-actions';

import {
  withKnobs,
  text,
  number,
  boolean,
  array,
  select,
  color,
  date,
  button,
} from '@storybook/addon-knobs/angular';

import { SimpleKnobsComponent } from './knobs.component';
import { AllKnobsComponent } from './all-knobs.component';

storiesOf('Addon|Knobs', module)
  .addDecorator(withKnobs)
  .add('Simple', () => {
    const name = text('name', 'John Doe');
    const age = number('age', 0);
    const phoneNumber = text('phoneNumber', '555-55-55');

    return {
      moduleMetadata: {
        entryComponents: [SimpleKnobsComponent],
        declarations: [SimpleKnobsComponent],
      },
      template: `
        <h1> This is a template </h1>
        <storybook-simple-knobs-component
          [age]="age"
          [phoneNumber]="phoneNumber"
          [name]="name"
        >
        </storybook-simple-knobs-component>
      `,
      props: {
        name,
        age,
        phoneNumber,
      },
    };
  })
  .add('All knobs', () => {
    const name = text('name', 'Jane');
    const stock = number('stock', 20, {
      range: true,
      min: 0,
      max: 30,
      step: 5,
    });
    const fruits = {
      apples: 'Apple',
      bananas: 'Banana',
      cherries: 'Cherry',
    };
    const fruit = select('fruit', fruits, 'apple');
    const price = number('price', 2.25);

    const border = color('border', 'deeppink');
    const today = date('today', new Date('Jan 20 2017'));
    const items = array('items', ['Laptop', 'Book', 'Whiskey']);
    const nice = boolean('nice', true);
    button('Arbitrary action', action('You clicked it!'));

    return {
      component: AllKnobsComponent,
      props: {
        name,
        stock,
        fruit,
        price,
        border,
        today,
        items,
        nice,
      },
    };
  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM