Submit forms via AJAX with ease
Перейти к файлу
Kristján Oddsson fe5a42b283
fix LICENSE link
2019-08-05 09:33:26 +01:00
examples link to latest version on demo page 2019-05-17 15:29:36 +01:00
src move flow declarations out of module scope 2019-05-22 16:44:27 +01:00
test clean up eslint configuration 2019-05-13 15:41:41 +01:00
.babelrc replace rollup with babel 2019-05-10 11:18:00 +01:00
.eslintrc.json clean up eslint configuration 2019-05-13 15:41:41 +01:00
.flowconfig init 2019-04-16 12:37:32 +01:00
.gitignore init 2019-04-16 12:37:32 +01:00
CODE_OF_CONDUCT.md Create CODE_OF_CONDUCT.md 2019-07-31 13:29:21 +01:00
CONTRIBUTING.md fix LICENSE link 2019-08-05 09:33:26 +01:00
LICENSE fix year in LICENSE 2019-04-16 12:44:12 +01:00
README.md Update README HTML 2019-05-17 10:38:31 -04:00
package-lock.json 0.0.6 2019-07-23 15:35:48 +01:00
package.json 0.0.6 2019-07-23 15:35:48 +01:00
prettier.config.js replace rollup with babel 2019-05-10 11:18:00 +01:00

README.md

remote-form

A function that will enable submitting forms over AJAX.

The function will make a request based on the form using window.fetch with the payload encoded as URL parameters if the form method is a GET and FormData for all the other methods.

The request object is available in the callback function, allowing the headers and body to be modified before the request is sent.

Installation

$ npm install --save @github/remote-form

Usage

import {remoteForm} from '@github/remote-form'

// Make all forms that have the `data-remote` attribute a remote form.
remoteForm('form[data-remote]', async function(form, wants, request) {
  // Before we start the request
  form.classList.remove('has-error')
  form.classList.add('is-loading')

  let response
  try {
    response = await wants.html()
  } catch (error) {
    // If the request errored, we'll set the error state and return.
    form.classList.remove('is-loading')
    form.classList.add('has-error')
    return
  }

  // If the request succeeded we can do something with the results.
  form.classList.remove('is-loading')
  form.querySelector('.results').innerHTML = response.html
})
<form action="/signup" method="post" data-remote>
  <label for="username">Username</label>
  <input id="username" type="text">

  <label for="password">Username</label>
  <input id="password" type="password">

  <button type="submit">Log in</button>
  <div class="results"></div>
</form>

Browser support

Browsers without native custom element support require a polyfill.

  • Chrome
  • Firefox
  • Safari
  • Microsoft Edge

Development

npm install
npm test

License

Distributed under the MIT license. See LICENSE for details.