0.2.17 • Published 3 years ago

@leveluptuts/fresh v0.2.17

Weekly downloads
192
License
MIT
Repository
github
Last release
3 years ago

@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: 'scott@test.com',
}

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: 'scott@test.com',
}

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: 'scott@test.com',
}

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.

PropTypeDefaultDescription
formIdstring*requiredused to keep track of individual form instances.
onSubmitfunc(data) => dataCan be any of the following types. text (default), email, number, select, password, textarea, tags. (See types below)
cancelButtonbooleantrueif cancel is shown
disabledbooleanfalseif the form is disabled
cancelActionfunc() => nullA function that will run on cancel button click
submitTextstring'Submit'Custom text for submit button
cancelTextstring'Cancel'Custom text for cancel button
defaultValuesobject{}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.

PropTypeDefaultDescription
typestring'text'Can be any of the following types: text (default), email, number, select, password, textarea, tags, markdown, toggle. (See types below)
namestring''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.
requiredbooleanfalseIf a field is required
labelbooleantrueIf a field has a label
defaultValuestring/number/arraynullThe initial value for each field
tooltipstring''Shows an info icon next to the label with a tooltip message on hover
classNamestring''Custom className can be added to a field.

type - text & textarea

PropTypeDefaultDescription
placeholderstring''placeholder text

type - password

PropTypeDefaultDescription
strengthbooleantrueShows or hides the password strength meter below the field

type - select

PropTypeDefaultDescription
optionsarray of strings[]The text and values of a select list. *Object support coming soon

type - reference

PropTypeDefaultDescription
displayPropertyString"'Object property of what should be displayed in list
optionsArray 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

0.2.17

3 years ago

0.2.16

3 years ago

0.2.15

3 years ago

0.2.14

3 years ago

0.2.13

3 years ago

0.2.12

3 years ago

0.2.11

4 years ago

0.2.10

4 years ago

0.2.9

4 years ago

0.2.7

4 years ago

0.2.8

4 years ago

0.2.6

4 years ago

0.2.5

4 years ago

0.2.3

4 years ago

0.2.4

4 years ago

0.2.0

4 years ago

0.2.2

4 years ago

0.1.12

4 years ago

0.1.13

4 years ago

0.1.14

4 years ago

0.1.11

4 years ago

0.1.9

4 years ago

0.1.4

4 years ago

0.1.5

4 years ago

0.1.3

4 years ago

0.1.1

4 years ago

0.0.28

4 years ago

0.1.0

4 years ago

0.0.27

4 years ago

0.0.26

5 years ago

0.0.25

5 years ago

0.0.24

5 years ago

0.0.22

5 years ago

0.0.21

5 years ago

0.0.20

5 years ago

0.0.19

5 years ago

0.0.18

5 years ago

0.0.17

5 years ago

0.0.16

5 years ago

0.0.15

5 years ago

0.0.14

5 years ago

0.0.13

5 years ago

0.0.12

5 years ago

0.0.11

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago