New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

formstash

Package Overview
Dependencies
Maintainers
0
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

formstash

A React component wrapper for forms that saves data in real-time to IndexedDB

1.0.4
latest
npm
Version published
Weekly downloads
0
-100%
Maintainers
0
Weekly downloads
 
Created
Source

📝 formstash

formstash is a React component wrapper designed to simplify form handling by saving form data in real-time to IndexedDB. This package ensures that user data is preserved even if the user goes offline, providing a seamless offline experience.

🚀 Getting Started

Installation

To install the package, you can use npm or yarn:

npm install formstash

or

yarn add formstash

Basic Usage

Wrap your form with the FormWrapper component to enable offline data saving:

import React from "react";
import { FormWrapper } from "formstash";

const App = () => {
  return (
    <FormWrapper formName="myForm">
      <label>
        Name:
        <input type="text" name="name" />
      </label>
      <br />
      <label>
        Email:
        <input type="email" name="email" />
      </label>
      <br />
      <button type="submit">Submit</button>
    </FormWrapper>
  );
};

export default App;

📦 Features

🛠️ Real-Time Data Saving

The FormWrapper component automatically saves form data to IndexedDB as users input data. This ensures that user information is preserved even if they navigate away or close the browser.

📦 Offline Compatibility

When users go offline, the data entered into the form is saved locally in IndexedDB. Once the connection is restored, the saved data can be synchronized with your backend or used as needed.

💾 Automatic Data Restoration

The component automatically restores previously saved data when the form is loaded. Users will see their last entered data, enhancing their experience and reducing data entry redundancy.

🔄 Support for Multiple Input Types

FormWrapper supports a wide range of input types, including:

  • Text Input: <input type="text" />
  • Email Input: <input type="email" />
  • Password Input: <input type="password" />
  • Date Input: <input type="date" />
  • Select Dropdown: <select>...</select>
  • Checkbox: <input type="checkbox" />
  • Textarea: <textarea />
  • File Input: <input type="file" />
  • Number Input: <input type="number" />
  • Range Input: <input type="range" />

📖 Detailed Functionality

1. Setup Database

The setupDatabase function initializes IndexedDB for storing form data.

import { setupDatabase } from "formstash";

// Usage
const db = await setupDatabase();

2. Save Form Data

The saveFormData function saves the form data to IndexedDB. It is called automatically whenever form data changes.

import { saveFormData } from "formstash";

// Usage within FormWrapper
const handleInputChange = async (event) => {
  const formElement = event.target.closest("form");
  if (formElement && db) {
    const formData = new FormData(formElement);
    const data = {};
    formData.forEach((value, key) => {
      data[key] = value;
    });
    await saveFormData(db, "myForm", data);
  }
};

3. Autofill Form Fields

The autofillFormFields function restores saved data into the form fields when the component is initialized.

import { autofillFormFields } from "formstash";

// Usage within FormWrapper
const initialize = async () => {
  const database = await setupDatabase();
  await autofillFormFields(database, "myForm", formElement);
};

4. Form Submission Handling

On form submission, data is saved and IndexedDB is cleared. A notification is shown based on the success or failure of the operation.

import React from "react";
import { toast } from "react-toastify";
import { saveFormData, clearFormData } from "formstash";

const handleSubmit = async (event) => {
  event.preventDefault();
  try {
    const formElement = event.target.closest("form");
    const formData = new FormData(formElement);
    const data = {};
    formData.forEach((value, key) => {
      data[key] = value;
    });

    // Save form data
    await saveFormData(db, "myForm", data);

    // Clear form data
    await clearFormData(db, "myForm");

    // Show success message
    toast.success("Form submitted successfully!");
  } catch (error) {
    // Show error message
    toast.error("Failed to submit form. You might be offline.");
  }
};

📚 Documentation

For detailed documentation and advanced usage, refer to the documentation page (replace # with your documentation URL).

🛠️ Development

To build the package:

npm run build

To start a local development server:

npm start

💬 Support

If you have any questions or issues, feel free to open an issue on the GitHub repository.

📝 License

This package is licensed under the MIT License.

Keywords

react

FAQs

Package last updated on 12 Sep 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts