1.0.1 • Published 6 months ago

@abgameur/getformdata v1.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
6 months ago

getformdata

A simple utility to extract form data from a FormData object in a type-safe manner, converting empty string values to null.

Installation

npm install @abgameur/getformdata

or

yarn add @abgameur/getformdata

Usage

Import the getFormData function in your React project:

import getFormData from '@abgameur/getformdata';

Use it in your form submit handler to convert FormData to a simple JavaScript object, with empty strings converted to null:

import getFormData from '@abgameur/getformdata';

export const MyForm = () => {
  const handleSubmit = (formData: FormData) => {
    const data = getFormData(formData);
    console.log(data); // Process your form data here
  };

  return (
    <form action={handleSubmit}>
      <input type="text" name="content" placeholder="Enter content" />
      <button type="submit">Submit</button>
    </form>
  );
};

API

getFormData(formData: FormData): Record<string, string | null>

Parameters:

formData (FormData): The FormData object to convert.

Returns:

A Record<string, string | null> object where each key corresponds to a form field name, and each value is the field's value as a string or null if the field was empty.

Contributing

Contributions, issues, and feature requests are welcome!

License

This project is licensed under the MIT License - see the LICENSE file for details

1.0.1

6 months ago

1.0.0

6 months ago