• Stars
    star
    208
  • Rank 189,015 (Top 4 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 5 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A fresh forms library for React. Highly opinionated, dead simple API.

@leveluptuts/fresh

YO! That's fresh (too fresh)

Bboy Headspinhttps

NPM

Demo

https://fresh.leveluptutorials.com/

Install

yarn add @leveluptuts/fresh

Usage

A basic form

import { Form, Field } from '@leveluptuts/fresh'
const CoolApp = () => {
  return (
    <Form
      formId="simple"
      onSubmit={data => {
        console.log(data)
      }}
    >
      <Field>Name</Field>
      <Field type="number">Number</Field>
      <Field type="select" options={options} />
    </Form>
  )
}

A slightly less basic form

import { Form, Field } from '@leveluptuts/fresh'

const CoolApp = () => {
  return (
    <Form formId="less-simple" onSubmit={onSubmit}>
      <Field>Name</Field>
      <Field type="email">Email</Field>
      <Field type="password">Password</Field>
      <Field type="tags">Tags</Field>
      <Field type="number">Number</Field>
      <Field required type="select" options={options}>
        Type
      </Field>
      <Field type="textarea">Text Area</Field>
      <Field type="markdown">Markdown</Field>
      <Field type="toggle">Markdown</Field>
    </Form>
  )
}

How about one with default values?

import { Form, Field } from '@leveluptuts/fresh'

const defaultValues = {
  name: 'Brooklyn Boo',
  email: '[email protected]',
}

const CoolApp = () => {
  return (
    <Form formId="defaults" onSubmit={onSubmit} defaultValues={defaultValues}>
      <Field>Name</Field>
      <Field type="email">Email</Field>
      <Field>Two Words</Field>
    </Form>
  )
}

Nested Forms?

import { Form, Field } from '@leveluptuts/fresh'

const defaultValues = {
  name: 'Brooklyn Boo',
  email: '[email protected]',
}

const CoolApp = () => {
  return (
    <Form formId="defaults" onSubmit={onSubmit} defaultValues={defaultValues}>
      <Field>Name</Field>
      <Form
        formId="nestedForm"
        onSubmit={onSubmit}
        defaultValues={defaultValues}
      >
        <Field>Name</Field>
      </Form>
    </Form>
  )
}

Access any form data, anytime.

import { Form, Field, useForm } from '@leveluptuts/fresh'

const defaultValues = {
  name: 'Brooklyn Boo',
  email: '[email protected]',
}

const CoolApp = () => {
  const { data } = useForm()
  console.log(data) // data: {  defaults: { name: "" }, secondForm: { name: "" } }
  return (
    <>
      <Form formId="defaults" onSubmit={onSubmit} defaultValues={defaultValues}>
        <Field>Name</Field>
      </Form>
      <Form
        formId="secondForm"
        onSubmit={onSubmit}
        defaultValues={defaultValues}
      >
        <Field>Name</Field>
      </Form>
    </>
  )
}

Demos

Basic Form - https://codesandbox.io/s/basic-form-s2kl0 Less Basic Form - https://codesandbox.io/s/less-basic-form-jn1rn Conditional Items with useForm hook - https://codesandbox.io/s/with-hook-ch1bg?file=/src/App.js

API

Form

The wrapper around your fields.

Prop Type Default Description
formId string *required used to keep track of individual form instances.
onSubmit func (data) => data Can be any of the following types. text (default), email, number, select, password, textarea, tags. (See types below)
cancelButton boolean true if cancel is shown
disabled boolean false if the form is disabled
cancelAction func () => null A function that will run on cancel button click
submitText string 'Submit' Custom text for submit button
cancelText string 'Cancel' Custom text for cancel button
defaultValues object {} An object with correlating default values

Field

Common API - The props that are common among all fields

The common API is shared among all elements. Type specific fields are found below.

Prop Type Default Description
type string 'text' Can be any of the following types: text (default), email, number, select, password, textarea, tags, markdown, toggle. (See types below)
name string '' The name of the field data to be returned. If no name prop is given, the Field element's text will be converted to camelCase and be used.
required boolean false If a field is required
label boolean true If a field has a label
defaultValue string/number/array null The initial value for each field
tooltip string '' Shows an info icon next to the label with a tooltip message on hover
className string '' Custom className can be added to a field.

type - text & textarea

Prop Type Default Description
placeholder string '' placeholder text

type - password

Prop Type Default Description
strength boolean true Shows or hides the password strength meter below the field

type - select

Prop Type Default Description
options array of strings [] The text and values of a select list. *Object support coming soon

type - reference

Prop Type Default Description
displayProperty String "' Object property of what should be displayed in list
options Array of Objects [] Object property of what should be displayed in list

Hooks

useForm

const { data, isReady, defaultValues, setForm, setField, setDefaults, resetForm } = useForm()

data

Can access any loaded form on the page via the data[formId]. Same with isReady and defaultValues. This allows you to extend the form in all kinds of external fields without having to bundle those elements into the library

Errors

Not complete / in use yet, just standard html 5 validation

Styles

Add css from global.css in this repo to get the library styles if you would like them.

https://github.com/leveluptuts/fresh/blob/main/src/fields/global.css

FAQ

Can I customize this component in my own way?

This library makes some calls to keep the API easy to use and maintain. Using it with another library that tries to bring it's own inputs in isn't really needed at this time.

Contributing

yarn yarn start

(in another tab) to run example

cd example yarn yarn start

Prior Art and Inspirations

I am huge fan of simple, easy APIs that take care of 90% of jobs easily. One form library I really enjoyed was https://kozea.github.io/formol/ . The API was simple in all of the ways that I love, but there were some aspects of the library that just didn't fit for us and our workflow. I wanted to make something that was more simple, but just as easy but with more configuration options. I'm also inspired by AutoForm for Meteor https://github.com/aldeed/meteor-autoform for future generation features.

License

MIT © leveluptuts

More Repositories

1

bookit

A UI component explorere desiged specifically for Svelte Kit, not spooky 👻
Svelte
261
star
2

gQuery

Not like jQuery. A GraphQL Fetcher & Cache for Svelte Kit
TypeScript
172
star
3

fullstack-graphql-apollo-react-meteor

A fullstack app tutorial for GraphQL using Apollo, React & Meteor
JavaScript
140
star
4

svelte-toy

A toy for svelte data stores
Svelte
92
star
5

Level-Up-JavaScript-Testing-101

Learn JavaScript testing with Jest.
HTML
64
star
6

svelte-fit

Fit that text of yours in its container automatically
TypeScript
59
star
7

svelte-notion

Tools for using Notion public pages as data in your app
HTML
55
star
8

Svelte-Element-Query

TypeScript
54
star
9

level-up-vscode-theme

54
star
10

svelte-auto-form

A Svelte forms library
Svelte
47
star
11

scotts-dope-hooks

TypeScript
38
star
12

svelte-side-menu

Svelte
33
star
13

drop-in

TypeScript
32
star
14

React-Testing-For-Beginners

Starting code for React Testing For Beginners
JavaScript
27
star
15

Animating-React-Starter

JavaScript
26
star
16

avalanche

A fullstack, modern, crazy fast development platform for React, GraphQL, MongoDB
25
star
17

svelte-accounts-ui

A drop in UI package for Auth in Svelte & Meteor
HTML
25
star
18

advanced-gatsby-shopify

JavaScript
22
star
19

animating-react-framer-motion

JavaScript
22
star
20

Gatsby-Tutorials

Check the branches for videos code.
CSS
20
star
21

hooks-starter

The starting repo for Level Up Tutorials Hooks For Everyone
JavaScript
18
star
22

Blaze-Base

The Meteor Blaze Base starter for the Topics In Meteor tutorial series
CSS
18
star
23

motion-elements

Elements and Components built with Framer Motion
TypeScript
17
star
24

Meteor-1.4-React-For-Everyone

CSS
16
star
25

Level-2-Meteor-React

CSS
13
star
26

React-Base

CSS
13
star
27

web-components-for-beginners

The code examples for the Level Up Tutorials course Web Components For Beginners.
HTML
11
star
28

meteor-1-4-for-everyone

The Meteor 1.4 For Everyone Tutorial Series Code
CSS
11
star
29

Svelte-Kit-Tutorial-Series

24 Video tutorials to teach you the ins and outs of Svelte Kit
Svelte
10
star
30

node-fundamentals-authentication

The code for Level Up Tutorials series, Node Fundamentals Authentication. https://www.leveluptutorials.com/tutorials/node-fundamentals-authentication
JavaScript
10
star
31

comboquery

TypeScript
9
star
32

Intermediate-Meteor-1.4-React-For-Everyone-Start

The Starting Codebase for Intermediate Meteor 1.4 + React
CSS
9
star
33

JavaScript-Testing-101-Solutions

JavaScript
9
star
34

meteor-svelte-code

The code for the yt series on Meteor and Svelte. https://www.youtube.com/playlist?list=PLLnpHn493BHHrJWLpGqCvLFwsWQ9jCBQR
Astro
8
star
35

testing-with-cypress-leveluptutorials

The starting code for Testing With Cypress Level Up Tutorials Code
JavaScript
8
star
36

node-fundamentals-authentication-part2

JavaScript
7
star
37

svelte-kit-tutorial-code

JavaScript
6
star
38

gatsbySite

An Example of a static React contact form using Netlify.
CSS
6
star
39

remix-for-everyone

The code for Leveluptutorials.com course on Remix
JavaScript
6
star
40

exploring-browser-apis-start

The starting code of Exploring Browser APIs
CSS
5
star
41

React-Apollo-Snippets-for-VSCode

Some snippets I wanted to have and thought others might as well.
5
star
42

Meteor-Topics-Blaze-Accounts

CSS
5
star
43

building-svelte-components

Course files for Building Svelte Components
Svelte
5
star
44

react-native-layout-examples

Objective-C
3
star
45

React-Native-For-Everyone

JavaScript
3
star
46

levelup-gridsome

The starter for Gridsome For Everyone
Vue
3
star
47

astro-for-everyone-code

Astro
3
star
48

real-world-debugging

JavaScript
2
star
49

node-auth-part2-starter

JavaScript
2
star
50

svelte-3-for-beginners

An introduction to Svelte 3 course.
HTML
2
star
51

svelte-typescript-course-start

The starter code for the Svelte and TypeScript Course on Level Up Tutorials levelup.video
Svelte
2
star
52

sk-deploy-demo

Svelte
1
star
53

backspin

Something small coming soon
JavaScript
1
star
54

remix-syntax

TypeScript
1
star
55

vue-static

Vue
1
star
56

astro-syntax

An exploration into Astro
Astro
1
star