Giter Site home page Giter Site logo

from-form-submit's Introduction

from-form-submit

Simply convert a form submit event to a Javascript object

npm bundle size npm Coverage Status

Features

  • Convert input values by looking at their types
    • date
    • number
    • text
    • checkbox
  • Support arrays (eg: name="values.0.name" will be converted as { values: [{ name }] })
  • Vanilla JS, meaning it should work with all frameworks!
  • Prevent the default form submit behaviour

API

  • fromEvent(event: SubmitEvent, starter: Object): Object: convert the submit event ̀event to a Javascript object

    • types are casted when we can
    • inputs must have a name attribute for this to work
    • you can give a starter object, in which case this object will be copied and then populated with the form informations
  • wrapSubmit(callback: Function, starter: Object): Function: wrap your implementation before giving it to your "submit" callback form

    • this function catch the form event, then calls ̀formEvent to retrieve data, then calls your callback with it
    • callback(data: object, event: window.Event): void

Examples (ReactJS)

fromEvent

import React from 'react'
import { fromEvent } from 'from-form-submit'

const Component = () => (
  <form
    onSubmit={(event) => {
      console.log(fromEvent(event))
    }}
  >
    <input type="number" name="age" />
    <input type="string" name="name" />
  </form>
)

wrapSubmit

import React from 'react'
import { wrapSubmit } from 'from-form-submit'

const Component = () => (
  <form onSubmit={wrapSubmit(console.log)}>
    <input type="number" name="age" />
    <input type="string" name="name" />
  </form>
)

from-form-submit's People

Contributors

fabienjuif avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

from-form-submit's Issues

tests

we need some jest tests here!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.