npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@stegopop/fetch-data-forms

v1.0.0

Published

Instantly turn an HTML form element into an asynchronous Fetch request by adding an attribute.

Downloads

4

Readme

FetchDataForms

npm version License: MIT

Instantly turn an HTML form element into an AJAX request just by adding an attribute.

Dependencies

Requires jQuery's AJAX.

Usage

Make your web apps feel snappier by converting server-side requests into AJAX requests.

Instantiate FetchDataForms

new FetchDataForms();

Just add the data-fetch attribute to any form you'd like to convert.

<form action="/url" method="POST" data-fetch>
   ...
</form>

Install

With NPM

npm install @stegopop/data-fetch-forms

With a CDN

<script src="https://cdn.jsdelivr.net/npm/@stegopop/data-fetch-forms"></script>

Options

Below are optional features that can be configured via an options object passed the the FetchDataForms constructor, or via data attributes.

Selector

By default, any form with the data-fetch attribute will submit with FetchDataForms.

You may modify this with a css style selector in the options object.

new FetchDataForms({
    select: "form.fetch"
});

reCAPTCHA

Note: FetchDataForms only supports reCAPTCHA v3.

Added Google reCAPTCHA to FetchDataForms is super simple. Just add a recaptcha object with a version and siteKey as in the options object.

<script src="https://www.google.com/recaptcha/api.js?render=YourKeyHere11111111111111111111111111111"></script>
<script>
   new FetchDataForms({
      recaptcha: {
         version: "v3",
         siteKey: "YourKeyHere11111111111111111111111111111"
      },
   });
</script>

Then add the data-fetch-recaptcha attribute to any form you'd like to protect.

<form action="/url" method="POST" data-fetch data-fetch-recaptcha>
    ...
</form>

Note: This version of the Google reCAPTCHA is score-based. If you wanted to perform certain actions based on that score, then pass the score from Google's Site Verify Response. You can access that in the form submit event, or the done, always, or fail callbacks.

Showing Errors

Displaying errors in form inputs requires you to specify where you'd like the error to show.

Do that with the data-fetch-errors attribute.

<form action="/url" method="POST" data-fetch>
   
    <label for="favorite_sandwich">
        <input type="text" name="favorite_sandwich" id="favorite_sandwich" required>
        <div data-fetch-errors="favorite_sandwich"></div>
    </label>
    
</form>

Then when the server finds an error, return a JSON message with this format for errors.

{
    "errors": {
        "favorite_sandwich": {
            "messages": [
                "This is the 'favorite_sandwich' error message.",
                "There could multiple errors."
            ]
        }
    }
}

Submission Events

If you assign your form an id, then an event will be dispatched whenever your form is submitted.

Whatever casing you use for your form id will be converted to kebab-casing for the event name.

Example: <form id="testForm"> requires listening like this addEventListener("test-form-submit", function() { ... })

<form id="fetch-example" action="/url" method="POST" data-fetch>
    ...
</form>

<script>
    document.querySelector("#fetch-example").addEventListener("fetch-example-submit", function(data) {
        console.log("Recieved an event");
    });
</script>

Callbacks

Another way to call code after a submission is with the AJAX callback attributes.

  • done
  • fail
  • always
  • beforeSend

Pass the name of a JavaScript function to the attribute.

<form action="/url" method="POST" 
      data-fetch 
      data-fetch-done="doneCallbackExample"
      data-fetch-fail="failCallbackExample"
      data-fetch-always="alwaysCallbackExample"
      data-fetch-before-send="beforeSendCallbackExample">
    ...
</form>

Pass the return data to your done callback function by adding a data argument to your function.

function doneCallbackExample(data) {
    ...
}

function failCallbackExample(data) {
    ...
}

function alwaysCallbackExample(data) {
    ...
}

function beforeSendCallbackExample() {
    ...
}

Disable Submit Button Duration

By default, the submission button for your form will be disabled for 1.5 seconds after submission to prevent multiple submissions.

You modify this disabled duration by setting submitDisabledDuration in the options object passed to the FetchDataForms class.

new FetchDataForms({
    submitDisabledDuration: 3000
});

Or you may disable it by setting the value to 0.

Multiple Submit Buttons

You may add multiple submit buttons to your form.

Adding a name and value to your buttons will allow you see which submitter was pressed from the backend.

Only the submitter element name and value will be submitted.

Just like the browser default, if you press enter in your form, the first submit element will be the one that submits the form.

Development

Compiling

I've run into issues compiling this with babel, and moved to swc. Which I also had issues compiling locally.

It does work in the playground though, so it's probably an issue with my node environment.

I'm not going to fix that right now. So if npx swc doesn't work, then manually paste the .swcrc json config here.

https://swc.rs/playground

And then copy and paste over the dist file.