GithubHelp home page GithubHelp logo

isabella232 / addon-queryparams Goto Github PK

View Code? Open in Web Editor NEW

This project forked from storybookjs/addon-queryparams

0.0 0.0 0.0 166 KB

parameter addon for storybook

License: MIT License

JavaScript 56.83% TypeScript 43.17%

addon-queryparams's Introduction

storybook-addon-queryparams

This storybook addon can be helpful if your components need special query parameters to work the way you want them to. It allows you to mock query params per story so that you can easily reproduce different states of your component.

Getting started

First, install the addon.

$ yarn add @storybook/addon-queryparams --dev

Register it by adding it in the addons attribute in your main.js file (create this file inside your storybook config directory if needed).

module.exports = {
  addons: ["@storybook/addon-queryparams"],
};

In your story, add the withQuery decorator and define the query parameters you want to mock:

import React from "react";
import { Button } from "@storybook/react/demo";
import { withQuery } from "@storybook/addon-queryparams";

export default {
  title: "Button",
  component: Button,
  decorators: [withQuery],
  parameters: {
    query: {
      mock: "Hello world!",
    },
  },
};

export const WithMockedSearch = () => {
  const urlParams = new URLSearchParams(document.location.search);
  const mockedParam = urlParams.get("mock");
  return <div>Mocked value: {mockedParam}</div>;
};
Example with storiesOf API
import React from "react";
import { storiesOf } from "@storybook/react";

storiesOf("button", module)
  .addParameters({
    query: {
      mock: "Hello World!",
    },
  })
  .add("Prints the mocked parameter", () => {
    const urlParams = new URLSearchParams(document.location.search);
    const mockedParam = urlParams.get("mock");
    return <div>Mocked value: {mockedParam}</div>;
  });

Credits

While this addon was part of the Storybook monorepo, it received commits from the following authors:

Andrew Lisowski, Brody McKee, Clément DUNGLER, Filipp Riabchun, Gaëtan Maisse, Gert Hengeveld, Jon Palmer, Lynn Chyi, Michael Shilman, Norbert de Langen, Paul Rosania, Renovate Bot, Tom Coleman, Varun Vachhar, Yann Braga

addon-queryparams's People

Contributors

shilman 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.