Miss any of our Open RFC calls?Watch the recordings here! »

@sharyn/browser.getformfields

1.0.8 • Public • Published

🌹 getFormFields

getFormFields: Gives you the fields of a form element. getFormFields uses FormData and returns a plain object.

Installation

npm i @sharyn/browser.getformfields
# or 
yarn add @sharyn/browser.getformfields

You can alternatively install the @sharyn/browser package, or the entire sharyn library.

Arguments

formElement (HTMLFormElement): The HTML form element.

[onlyTruthy=false] (boolean): Falsy fields (like '') are omitted if true.

Returns

object: The plain object of the form data.

Example

const Form = () => {
  const form = useRef(null)
  return (
    <>
      <form ref={form}>
        <input name="firstname" />
        <input name="lastname" />
      </form>
      <button onClick={() => console.log(getFormFields(form.current))}>
        Log all form values
      </button>
      <button onClick={() => console.log(getFormFields(form.current, true))}>
        Log truthy form values
      </button>
    </>
  )
}

If firstname is Stan and lastname is left empty:

getFormFields(form.current)       // { firstname: 'Stan', lastname: '' }
getFormFields(form.current, true) // { firstname: 'Stan' }

Imports

Depending on the package you are using, you can import or require getFormFields in the following ways:

import getFormFields from '@sharyn/browser.getformfields'
import getFormFields from '@sharyn/browser/getFormFields'
import getFormFields from 'sharyn/browser/getFormFields'
 
import { getFormFields } from '@sharyn/browser'
import { getFormFields } from 'sharyn/browser'
import { getFormFields } from 'sharyn'

This package is part of Sharyn, a collection of utilities and helpers.

Install

npm i @sharyn/browser.getformfields

DownloadsWeekly Downloads

1

Version

1.0.8

License

MIT

Unpacked Size

5.07 kB

Total Files

5

Last publish

Collaborators

  • avatar