-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
onSubmit stripped for Forms #128
Comments
onSubmit
stripped for Forms
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>
)); |
I see, thanks for explaining that. |
add projectDir as a packager projectRoot
I found a better way.
So. We need a better action with a e.preventDefault integrated. |
@hmontes You just inlined a variable, why do you think it's better? |
@Hypnosphi Because if i have several buttons in a component (example. A modal with a Close button and a send form button) i can use different actions (action('Modal closed'), action('Submitted Form'). |
But you only need preventing default on form submit button |
Yes. Because "action" don't have a natural way to do that. (I spend hours to find a way to prevent default an event in storybook) |
If you have several forms, you may want to create a function like this:
And use it like that:
|
Yeah. But that function is INSIDE the component. The solution is for the Story. If you have a presentational component and a container you want to disable the event OUTSIDE of the presentational component (And in this case. In the story) because the presentational components aren't classes. |
Sorry, but I don't get what you're talking about. Please provide an examle |
It seems I'm late to the party, but this is still a problem.
We're not relegating the form submission to the |
@plummer-flex Feel free to add this to your codebase
|
@Hypnosphi I get the adding another wrapper function so as to not use an inline function, but what's the case for not modifying |
Because increasing API surface for each possible usecase is impractical |
Now that @storybook/addon-actions is part of the essentials, I'm not able to import action anymore (even though it is installed as a dependency already). I checked the new action page here: https://storybook.js.org/docs/react/essentials/actions and looks like this is now handled differently. I was able to add an event for a button click, but still can't find how to prevent default for an onSubmit event using the new way. Using Storybook v6.1.5 To clarify, I do see a "submitted" event showing in the action panel, but it immediately redirects to a "No Preview" page. |
@njcaballero all the 6.x changes should be backwards compatible, i.e. if you want to use the "new way" of doing things where const withPreventDefault = (action) => e => {
e.preventDefault();
return action(e);
};
const Template = ({ onClick, ...rest }) => (
<MyComponent onClick={withPreventDefault(onClick)} {...rest} />
) |
@shilman That worked for me. Thanks! |
Trying to test a button that allows form submissions:
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 customForm
component, etc. In all cases theonSubmit
is stripped out. Any idea what I'm doing wrong? Or are forms not supported by React Storybook?The text was updated successfully, but these errors were encountered: