1.0.1 • Published 5 years ago

coffeekraken-s-ajax-form-component v1.0.1

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

Coffeekraken s-ajax-form-component

Provide a simple and declarative way to send a form through ajax with a success/error display mecanism built in.

View demo

Features

  1. Simple declarative way to send a form through ajax
  2. Keep track of the form status through simple loading, error and success attributes
  3. Display the success/error message easily
  4. Many more...

Table of content

  1. Demo
  2. Install
  3. Get Started
  4. Form status
  5. Success/error message display mecanism
  6. Javascript API
  7. Sugar Web Components Documentation
  8. Browsers support
  9. Contribute
  10. Who are Coffeekraken?
  11. Licence

Install

npm install coffeekraken-s-ajax-form-component --save

Get Started

First, import the component into your javascript file like so:

import SAjaxFormComponent from 'coffeekraken-s-ajax-form-component'

Then simply use it inside your html like so:

<form is="s-ajax-form" action="..." method="POST">
	<div s-ajax-form-success>
		Your message has been sent successfuly
	</div>
	<div s-ajax-form-error>
		Something went wrong...
	</div>
	<!-- your form here... -->
</form>

Form status

When your form is bein/has been sent with success, or maybe not, it will take one a another attribute depending on the state. Here's the two attributes:

  1. loading: When the form is currently sending data
  2. success: When the form has been sent with success
  3. error: When the form has not been sent correctly

Display state timeout

You can specify a timeout in order to automatically remove the state attributes by using the state-timeout property like so:

<form is="s-ajax-form" state-timeout="4000" ...>
</form>

This will make the error or success attribute to be removed 4 seconds after the form submission.

Success/error message display mecanism

The webcomponent integrate a simple but powerful way to handle the success/error display of your form. This mecanism start with two attributes that you can add to any HTMLElement that will hold the success/error message. Here's the two attributes:

  1. s-ajax-form-success: Put this attribute on the element that (will) hold your success message
  2. s-ajax-form-error: Put this attribute on the element that (will) hold your error message

When the form has been sent, depending on the response status, the form will be in success or error state. The according HTMLElement will then have the active attribute on itself.

  1. If the form has been sent with success, the s-ajax-form-success HTMLElement will have the active attribute.
  2. If the form has not been sent properly, the s-ajax-form-error HTMLElement will have the active attribute.

Here's a simple example:

<form is="s-ajax-form" action="..." method="POST">
	<div s-ajax-form-success>
		Your message has been sent successfuly
	</div>
	<div s-ajax-form-error>
		Something went wrong...
	</div>
	<!-- your form here... -->
</form>

Inject success/error message from the response

Let's consider our server response like so:

{
	"status": "OK",
	"code": 200,
	"message": "Your message has been sent"
}

You can then tell your s-ajax-form-success HTMLElement to display the message response property by setting it like so:

<form is="s-ajax-form" ...>
	<div s-ajax-form-success="message" timeout="4000"></div>
	<!-- your form here... -->
</form>

When our form has been sent with success, the component will take the s-ajax-form-success attribute value and use it as a path inside our response to get the text to inject inside the HTMLElement itself before display it.

The same principle goes for the s-ajax-form-error HTMLElement

Display timeout

Each s-ajax-form-success and s-ajax-form-error HTMLElement support a timeout attribute that tells the component how many times to display the reply. Here's a simple example:

<form is="s-ajax-form" ...>
	<div s-ajax-form-success timeout="4000">
		Your message has been sent.
	</div>
	<div s-ajax-form-error timeout="6000">
		Something went wrong...
	</div>
	<!-- your form here... -->
</form>

If you don't specify any timeout, the default value will be the stateTimeout property described above.

Browsers support

IE / EdgeFirefoxChromeSafari
IE11+last 2 versionslast 2 versionslast 2 versions

As browsers are automatically updated, we will keep as reference the last two versions of each but this component can work on older ones as well.

The webcomponent API (custom elements, shadowDOM, etc...) is not supported in some older browsers like IE10, etc... In order to make them work, you will need to integrate the corresponding polyfill.

Contribute

This is an open source project and will ever be! You are more that welcomed to contribute to his development and make it more awesome every day. To do so, you have several possibilities:

  1. Share the love ❤️
  2. Declare issues
  3. Fix issues
  4. Add features
  5. Build web component

Who are Coffeekraken

We try to be some cool guys that build some cool tools to make our (and yours hopefully) every day life better.

More on who we are

License

The code is available under the MIT license. This mean that you can use, modify, or do whatever you want with it. This mean also that it is shipped to you for free, so don't be a hater and if you find some issues, etc... feel free to contribute instead of sharing your frustrations on social networks like an asshole...