Code Monkey home page Code Monkey logo

just-submit's Introduction

๐Ÿ›ซ Just Submit

Submit simple form, with safe types, without management!

version license size downloads

โœจ Features

  • ๐Ÿ”’ TYPE SAFE
  • โšก Speedy DX
  • ๐Ÿ‘ถ Beginner-Friendly
  • ๐Ÿงช Well-Tested
  • ๐Ÿƒ Ultra Light
  • ๐Ÿงฉ Framework-Agnostic

๐Ÿ“ฆ Install

pnpm add just-submit

๐ŸŽฏ Quickstart

Important

Don't forget to add a default value for optional fields.

const handleSubmit = createSubmit({
  fullName: 'string',
  age: 'number',
  birthday: 'date',
  wantGift: 'boolean',
});

// Inside form submit event handler
handleSubmit((data) => {
  //          ^ { fullName: string; age: number; birthday: Date; wantGift: boolean }
});

๐Ÿ“š Examples

React

import { createSubmit } from 'just-submit';

const Form = () => {
  const handleSubmit = createSubmit({
    fullName: 'string',
    age: 'number',
    birthday: 'date',
    wantGift: 'boolean',
  });
  return (
    <form
      onSubmit={handleSubmit((data) => {
        // ...
      })}
    >
      <input type="text" name="fullName" required />
      <input type="number" name="age" min={0} required />
      <input type="date" name="birthday" defaultValue="2005-03-12" />
      <input type="checkbox" name="wantGift" />
      <button type="submit">SUBMIT</button>
    </form>
  );
};

Vue

<script setup lang="ts">
import { createSubmit } from 'just-submit';

const handleSubmit = createSubmit({
  fullName: 'string',
  age: 'number',
  birthday: 'date',
  wantGift: 'boolean',
})((data) => {
  // ...
});
</script>

<template>
  <form @submit="handleSubmit">
    <input type="text" name="fullName" required />
    <input type="number" name="age" min="0" required />
    <input type="date" name="birthday" value="2005-03-12" />
    <input type="checkbox" name="wantGift" />
    <button type="submit">SUBMIT</button>
  </form>
</template>

Vanilla

<form>
  <input type="text" name="fullName" required />
  <input type="number" name="age" min="0" required />
  <input type="date" name="birthday" value="2005-03-12" />
  <input type="checkbox" name="wantGift" />
  <button type="submit">SUBMIT</button>
</form>
import { createSubmit } from 'just-submit';

const handleSubmit = createSubmit({
  fullName: 'string',
  age: 'number',
  birthday: 'date',
  wantGift: 'boolean',
});
const form = document.querySelector('form')!;
form.addEventListener(
  'submit',
  handleSubmit((data) => {
    // ...
  }),
);

CDN

<head>
  <script src="https://unpkg.com/just-submit"></script>
</head>
<body>
  <form>
    <input type="text" name="fullName" required />
    <input type="number" name="age" min="0" required />
    <input type="date" name="birthday" value="2005-03-12" />
    <input type="checkbox" name="wantGift" />
    <button type="submit">SUBMIT</button>
  </form>
  <script>
    const { createSubmit } = JustSubmit;
    const handleSubmit = createSubmit({
      fullName: 'string',
      age: 'number',
      birthday: 'date',
      wantGift: 'boolean',
    });
    const form = document.querySelector('form');
    form.addEventListener(
      'submit',
      handleSubmit((data) => {
        // ...
      }),
    );
  </script>
</body>

๐Ÿ” Troubleshooting

Form Field Converting Error

This library can only convert string values from FormData.

For optional fields, add a default value in case they are null in submission.

How to control form? (watch value changes / form state / etc.)

This library is for simple forms that don't need to be controlled.

If you are working on a complex form, try the libraries here instead.

Can I use it with ...(other framework) ?

You probably CAN! The handleSubmit function can be used in any submit event that has preventDefault and currentTarget.

The examples provided include only the frameworks that have passed our tests. If you find this library works with any other framework, please don't hesitate to create a PR for it. We greatly appreciate your contributions and support!

๐Ÿ™ Thanks

Drawing inspiration and motivation from the following projects:

just-submit's People

Contributors

jsun969 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.