Skip to content
Please note that GitHub no longer supports Internet Explorer.

We recommend upgrading to the latest Microsoft Edge, Google Chrome, or Firefox.

Learn more
An input element that validates its value with a server endpoint.
Branch: master
Clone or download
muan Merge pull request #11 from github/events
Follow custom element event naming prefix convention
Latest commit 9e589a5 Mar 14, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples Follow custom element event naming prefix convention Mar 13, 2019
src
test Follow custom element event naming prefix convention Mar 13, 2019
.eslintrc.json ignore eslint rules in eslint config instead Apr 12, 2018
.flowconfig flow everything Apr 12, 2018
.gitignore
.travis.yml
LICENSE Standardize license header Apr 12, 2018
README.md
package-lock.json 1.0.0 Feb 21, 2019
package.json
prettier.config.js
rollup.config.js use babel-preset-github Feb 20, 2019

README.md

<auto-check> element

An input element that validates its value against a server endpoint.

Installation

$ npm install --save auto-check-element

Usage

import 'auto-check-element'
<auto-check src="/signup_check/username" csrf="<%= authenticity_token_for("/signup_check/username") %>">
 <input>
</auto-check>

Provide a URL and a CSRF token and the autocheck component will show validation confirmations and validation errors.

The endpoint should respond to POST requests with:

  • a 200 HTTP status code if the provided value if valid.
  • a 422 HTTP status code if the provided value is invalid.
  • a optional error message in the body and a Content-Type header with a value of text/html; fragment.

Events

const check = document.querySelector('auto-check')

// Network request lifecycle events.
check.addEventListener('loadstart', function(event) {
  console.log('Network request started', event)
})
check.addEventListener('loadend', function(event) {
  console.log('Network request complete', event)
})
check.addEventListener('load', function(event) {
  console.log('Network request succeeded', event)
})
check.addEventListener('error', function(event) {
  console.log('Network request failed', event)
})

// Auto-check result events.
const input = check.querySelector('input')

input.addEventListener('auto-check-send', function(event) {
  console.log('Adding to FormData before network request is sent.')
  const {body} = event.detail
  body.append('custom_form_data', 'value')
})
input.addEventListener('auto-check-success', function(event) {
  const {message} = event.detail
  console.log('Validation passed', message)
})
input.addEventListener('auto-check-error', function(event) {
  const {message} = event.detail
  console.log('Validation failed', message)
})
input.addEventListener('auto-check-complete', function(event) {
  console.log('Validation complete', event)
})

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.

You can’t perform that action at this time.