Skip to content
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

feat(actions): support e.target.reset() in form server action #166

Merged
merged 6 commits into from
Apr 17, 2024

Conversation

aralroca
Copy link
Collaborator

@aralroca aralroca commented Apr 17, 2024

Fixes #165

Reset form

The browser events inside server actions are serialized. However, e.target.reset() and e.currentTarget.reset() still works in the server. The only difference is that it is not executed at the right time, but is marked to reset it when the server action is finished and returns the response to the client.

export default function UncontrolledFormServer() {
  return (
    <form
      onSubmit={(e) => {
        // This code runs on the server
        e.target.reset(); // Reset the form
        console.log("Username:", e.formData.get("username"));
      }}
    >
      <label>
        Username:
        <input type="text" name="username" />
      </label>
      <br />
      <button type="submit">Submit</button>
    </form>
  );
}

Adds 0 bytes of JS client. Only the Brisa RPC client is needed which is ~800 bytes.

@aralroca aralroca self-assigned this Apr 17, 2024
@aralroca aralroca changed the title feat(actions): support e.target.reset() in server action of a form event feat(actions): support e.target.reset() in server action of a form event Apr 17, 2024
@aralroca aralroca changed the title feat(actions): support e.target.reset() in server action of a form event feat(actions): support e.target.reset() in form server action Apr 17, 2024
@aralroca aralroca merged commit 6313cef into main Apr 17, 2024
0 of 2 checks passed
@aralroca aralroca deleted the form-reset-method branch April 17, 2024 21:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow to reset forms from a server action
1 participant