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

@rakered/forms

v1.5.1

Published

Tiny helper to help with form submissions

Downloads

10

Readme

@rakered/forms

Tiny helper to help with form submissions

social image

Usage

The most basic helper is getFormData, it takes either the event directly, or an HTML form element. What it returns, is a dictionary holding the values of the form.

import { getFormData } from '@rakered/forms';

const onSubmit = (event) => {
  const data = getFormData(event);
  // » { name: 'smeijer', password: { digest: 'd03…83e', algorithm: 'sha-256' } }
};

<form onSubmit={onSubmit}>
  <input name="user" />
  <input name="password" type="password" />
</form>;

Because we often want to wrap submit handlers between event.preventDefault() and return false, there is a handleSubmit helper that does exactly that.

import { handleSubmit } from '@rakered/forms';

const onSubmit = handleSubmit((values) => {
  // » { name: 'smeijer' }
});

<form onSubmit={onSubmit}>
  <input name="user" />
</form>;

Path expansions

Where applicable, input names will be expanded to object structures

<form onSubmit={onSubmit}>
  <input name="user.name" value="Stephan Meijer" />
  <input name="user.age" type="number" value="34" />
  <input name="hobbies[]" value="chess" />
  <input name="hobbies[]" value="art" />
</form>

serializes to:

{
  user: {
    name: 'Stephan Meijer',
    age: 34,
  },
  hobbies: ['chess', 'art'],
}

Type Coercion

A number of specific input types, are coerced to the proper data type.

  • password { digest: String, algorithm: 'sha-256' } This one is important, so let's start with that. Passwords are hashed using @rakered/hash, so you won't be reading the password that the user entered. Please don't try to work arround this. Instead, embrace it.

  • datetime-local Date The datetime-local input stores a full date, so the is converted to a proper Date. Other date-like fields, such as date, time, or week only support partial dates, and are left alone.

  • checkbox Boolean

  • number Number

  • range Number

Typescript

Both methods are typed and accept a generic to make the values a typed object. Together with the typecoercion, this can simplify form handling a lot.

interface User {
  name: string;
  age: number;
}

const signup = handleSubmit<User>((values) => {
  // » { name: 'smeijer', age: 34 }
});

<form onSubmit={signup}>
  <input name="user" />
  <input name="age" type="number" />
</form>;