question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

onSubmit stripped for Forms

See original GitHub issue

Trying to test a button that allows form submissions:

import React from 'react';
import { storiesOf, action } from '@kadira/storybook';
import Button from '../button';

storiesOf('Button', module)
  .add('submit for a form', () => (
    <div className="p4 center">
      <form
        onSubmit={ () => action('form submitted') }>
        <Button type="submit">Submit</Button>
      </form>
    </div>
  ));

It seems that onSubmit gets stripped when it loads in React Storybook. I’ve tried different combinations: wrapping it with a div, making it the root component, using a custom Form component, etc. In all cases the onSubmit is stripped out. Any idea what I’m doing wrong? Or are forms not supported by React Storybook?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:21 (11 by maintainers)

github_iconTop GitHub Comments

6reactions
de-monkeyzcommented, Apr 20, 2016

You need to pass in just the call to action() not a function that calls it:

<form onSubmit={action('form submitted')}>

This doesn’t prevent the default action on the form though, so the iFrame will reload. Not sure how you could handle this outside storing the created action:

const submitAction = action('form submitted');
storiesOf('Button', module)
  .add('submit for a form', () => (
    <div className="p4 center">
      <form
        onSubmit={ e => { e.preventDefault(); submitAction(e); }}>
        <Button type="submit">Submit</Button>
      </form>
    </div>
  ));
5reactions
shilmancommented, Dec 13, 2020

@njcaballero all the 6.x changes should be backwards compatible, i.e. action(x)(e) should still work. if you’re getting a warning about package dependencies you can just install addon-actions as a dev dependency to your project. it won’t affect essentials.

if you want to use the “new way” of doing things where onX is automatically filled in via args, it would look something like this (react example)

const withPreventDefault = (action) => e => {
  e.preventDefault();
  return action(e);
};

const Template = ({ onClick, ...rest }) => (
  <MyComponent onClick={withPreventDefault(onClick)} {...rest} />
)
Read more comments on GitHub >

github_iconTop Results From Across the Web

onSubmit stripped for Forms · Issue #128 · storybookjs/storybook
It seems that onSubmit gets stripped when it loads in React Storybook. I've tried different combinations: wrapping it with a div, making it...
Read more >
Formik onSubmit function is not working on my code
I ran into this problem and found that my validator was returning something that signaled to Formik the form was invalid, but no...
Read more >
Using Formik to Handle Forms in React - CSS-Tricks
This is how the binding works: It handles form submission with onSubmit={formik.handleSubmit} . It handles the state of inputs with value={ ...
Read more >
Code being stripped out of form tag | CKEditor.com Forums
HiSome of my client pages have forms in it. ... onsubmit="return FormCheck(this);" into the form tag. ... gets stripped our of the form...
Read more >
Onsubmit not working! - JavaScript - SitePoint Forums
My onsubmit function is not working. My PHP file has two forms. Both form tags have an onsubmit attribute that links to the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found