Git Product home page Git Product logo

fresh's Introduction

@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

fresh's People

Contributors

ericssartorius avatar jogoodma avatar stolinski avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fresh's Issues

textarea no longer working

Field type="textarea" no longer works after being changed to Field type="textArea". This either needs changed back to textarea or made to work with both camel case or all lowercase.

Validation

Hello Can you please add a validation object like defaults value we need to add a field that let you do validation in it , like formik if you play with it you will see that he add a validation object with yup can you do that please .
thank you for this library i like it soo much and it simple to work with

Field cancel not working

When the cancel button that comes with the field component is clicked the whole input field disappears

clearOnSubmit prop

If the form should clear when you submit, OR just do nothing (default)

Need to prefer camel case to kebab case when making fields

if i was to have the following:

defaultValues = {
videoId: '123'
}
<Form defaultValues={defaultValues} onSubmit={onSubmit} >
  <Field>Video Id</Field>
</Form>

the data on submit would consist of both videoId and video-id.

It would be nice to always have fields default as camelCase instead of kebab to avoid this problem

Accessibility Audit

Would love to have this audited for accessibility before it's ready for v1. I'm not an expert on form accessibility myself, but I'll enable eslint rules in the meantime.

Bundle size is way too big

Not sure where the heft is coming from. Removed Styled components, will plan on slimming this down as much as possible. I'm guessing it's from a dep.

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on Greenkeeper branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because it uses your CI build statuses to figure out when to notify you about breaking changes.

Since we didn’t receive a CI status on the greenkeeper/initial branch, it’s possible that you don’t have CI set up yet.
We recommend using:

If you have already set up a CI for this repository, you might need to check how it’s configured. Make sure it is set to run on all new branches. If you don’t want it to run on absolutely every branch, you can whitelist branches starting with greenkeeper/.

Once you have installed and configured CI on this repository correctly, you’ll need to re-trigger Greenkeeper’s initial pull request. To do this, please click the 'fix repo' button on account.greenkeeper.io.

Number Select Field

A field that looks kinda like a table.

0 | 1 | 2 | 3 as a horizontal radio select. Commonly seen in surveys

Select input not getting className

For some inputs, instead of the className (for example, 'fresh-input'), an empty prop of class is coming through.

Screen Shot 2019-10-15 at 20 15 51

There are also other empty and unnecessary props such as placeholder (as shown in screenshot) coming through for a lot of the complex fields

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.