Giter Club home page Giter Club logo

storybook-state's Introduction

Storybook state

Note

This repository is a direct copy of https://github.com/Sambego/storybook-state but with updated dependencies. No other functionality was changed (unless necessary to retain functionality). All credit goes to the original author.

Getting Started

npm install --save-dev @aeolun/storybook-state

First you will need to create a new store, to save the state and handle updates. You can add all properties which your component expects, and the State component will propagate them to your component. Once you've created the store, you can wrap your components in a State component and pass along the store.

In the example below we create a modal which will expect an active property. When clicking on the button we will update the store, which in turn will update the property active on the modal;

Display and update using a State component

import React from "react";
import { storiesOf } from "@storybook/react";
import { State, Store } from "@aeolun/storybook-state";

const store = new Store({
  active: false
});

const SimpleModal = props => (
  <div>
    <State store={store}>
      <Modal>Modal content</Modal>
    </State>
    <Button onClick={() => store.set({ active: !store.get("active") })} />
  </div>
);

export default { title: "Modal" };
export const Simple = () => SimpleModal;

Display and update using a State decorator

import React from "react";
import { addDecorator, addParameters } from "@storybook/react";
import { Store, withState } from "@aeolun/storybook-state";

const SimpleCounter = props => {
  return [
    <p> Count: {props.count} </p>,
    <button onClick={props.handleCountUpdate}> {props.count} </button>
  ];
};

const store = new Store({
  count: 0,
  handleCountUpdate: () => store.set({ count: store.get("count") + 1 })
});

addDecorator(withState());
addParameters({
  state: {
    store
  }
});

export default { title: "Counter" };
export const Simple = () => SimpleCounter;

Store

The store has a few methods you can use to get and update the state.

When creating a new instance, you can pass along the initial state.

const store = new Store({
  foo: "bar",
  active: true,
  items: ["item 1", "item 2"]
});

You can retrieve a state from the store by using the get() method.

store.get("foo"); // will return 'bar'
store.get("active"); // will return true
store.get("items"); // will return ['item 1', 'item 2']

You can update the store by using the set() method.

store.set({
  active: false,
  bar: "foo"
});

You can subscribe to changes in the store by using the subscribe() method. You can register a callback, which will have the updated state as the first parameter whenever the state updates.

store.subscribe(state => // Do something with the updated state.

State component

The state component accepts one property, an instance of Store. All properties that depend on the state, or should update on state changes, should be added in the Store, and will be propagated to your component by the <State /> component.

<State store={store}>
  <StateDependendComponent />
</State>

The state also allows a function as a child so you can pass the state to any prop of your components.

const store = new Store({
  active: true
});

<State store={store}>
  {state => [
    <ElementOne active={state.active} />,
    <ElementTwo checked={state.active} />
  ]}
</State>;

You can also manipulate the state before passing it to the children via the parseState property.

<State store={store} parseState={state => ({ ...state, id: `#${state.uuid}` })}>
  <StateDependendComponent />
</State>

When using the withState decorator, you can pass along the state parser function as a parameter.

addDecorator(withState());
addParameters({
  state: {
    store,
    parseState: state => ({ ...state, count: `foo-${state.count}` })
  }
});

storybook-state's People

Contributors

sambego avatar aeolun avatar geoffreydhuyvetters avatar tholapz avatar mrmckeb avatar deanbowler avatar driesd avatar guillaumewuip avatar roine avatar jonicious avatar jmca avatar miracle2k avatar duiker101 avatar

Watchers

 avatar

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.