Giter Club home page Giter Club logo

storybook-addon-state's Introduction

storybook-addon-state

A storybook addon to give you access to store/retrieve arbitrary data. Similar to knobs this doesn't add any additional React wrappers to the story, so any other addons used (prop-types, jsx) aren't effected.

Install

yarn add storybook-addon-state

Usage

import { storiesOf } from '@storybook/react';
import useState from 'storybook-addon-state';

storiesOf('FooBar', module).add('I get state', () => {
  const [count, set] = useState('clicks', 0);

  return (
    <div>
      <button onClick={() => set(count + 1)}>Click Me</button>
      <div>{`I was clicked ${count} time(s)`}</div>
    </div>
  );
});

storybook-addon-state's People

Contributors

adierkens avatar hipstersmoothie avatar

Stargazers

Mohammad Esfandiari avatar  avatar Alex Rudenko avatar Michael Woodward avatar Dmitry Petrov avatar Seth Bro avatar Marquinhus Gonçalves avatar  avatar Brad Adams avatar Peter Magenheimer avatar Andrejs Agejevs avatar Paul Scanlon avatar Alex Wilson avatar Jason Adams avatar Michael Shilman avatar  avatar  avatar

Watchers

James Cloos avatar Paul Scanlon avatar

storybook-addon-state's Issues

Can't use action addon

I can't figure out how to use the action addon alongside this addon. It acts as if there is a forceReRender going on, because if I click fast enough, I can see my action text just for a split second. Here's my code:

import * as React from 'react';
import { storiesOf } from '@storybook/react';
import { action } from '@storybook/addon-actions';
import useState from 'storybook-addon-state';
import { Checkbox} from './Checkbox.tsx';

storiesOf('Forms', module)
  .addDecorator((storyFn) => (<div style={{ backgroundColor: '#fff', width: '450px', padding: '50px' }}>{storyFn()}</div>))
  .add('Checkbox', () => {
    const [checked, set] = useState('checked', true);
    const onChange = () => {
      action('onChange')('something');
      set(!checked)
    };

    return (
      <CustomCheckbox
        checked={checked}
        onChange={onChange} />
    );
  });

should update actualVals

import addons from '@storybook/addons';
import { FORCE_RE_RENDER, STORY_CHANGED } from '@storybook/core-events';

let stores: Record<string, any> = {};

addons.getChannel().addListener(STORY_CHANGED, () => {
stores = {};
});

const capitalizeFirstLetter = (string: string) =>
string.charAt(0).toUpperCase() + string.slice(1);

export default function useState(
name: string,
store: T
): [T, (newState: T) => void] {
let actualVals = stores[name] === undefined ? store : stores[name];
stores[name] = actualVals;

const update = (newState: T) => {
actualVals= newState;
stores[name] = actualVals;
addons.getChannel().emit(FORCE_RE_RENDER);
};
update.toString = () => set${capitalizeFirstLetter(name)}(newState);

return [actualVals, update];
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.