npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

formstash

v1.0.4

Published

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

Downloads

275

Readme


📝 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.