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

react-forms-lite

v1.1.1

Published

Lite, minimal React forms with specs

Downloads

5

Readme

react-forms-lite

What is it?

react-forms-lite is a minimal, opinionated React Component for building forms. It consists of the 3 following Components:

  • Forms
  • Form
  • Input

Forms is a wrapper around Form. The basic idea is that you can have multiple Form(s) that are submitted via 1 submit button as part of the Forms component. This helps in specifying different specs for each Form yet submit it as one. For example a reservation page will have the reservation detail Form but may also have the payment Form. You will want to submit them both as one but it helps in tracking them separately.

Forms properties:

  • specs: Object, required
  • onSubmit: Function(values), required

onSubmit is a function attribute of the Component that owns Forms. This function is called when the submit button is clicked. The values argument is structured like the specs such that each spec has it's values based on the id(s) passed along with an additional isValid property.

spec object must have:

  • name: String,
  • fields: Array[Object]

Each field corresponds to an Input. The following code from the Input Component explains which properties are required and which are optional.

// All properties and their types
Input.propTypes = {
    id: React.PropTypes.string.isRequired,
    label: React.PropTypes.string.isRequired,
    className: React.PropTypes.string,
    type: React.PropTypes.oneOf(['text','textarea','select','datepicker']),
    required: React.PropTypes.bool,
    placeholder: React.PropTypes.string,
    validate: React.PropTypes.func,
    options: React.PropTypes.array
}

// Default values for some properties
Input.defaultProps = {
    className: '',
    type: 'text',
    required: false,
    placeholder: '',
    validate: function(prop) {
        // Default: not empty string
        return prop !== '';
    }
}

Styling Utilities

The HTML structure is similar to the following:

<!-- Forms Component -->
<div class="rfl-forms-container">
    <!-- Form Component -->
    <div class="rfl-form-container">
        <!-- Input Component -->
        <div class="rfl-field-container">
            <div class="rfl-label-container"><label>{this.props.label}</label></div>
            <div class="rfl-input-container">
                <!-- Input according to spec -->
                <input type="text" />
            </div>
        </div>
    </div>
    <div class="rfl-submit-btn-container">
        <button>Submit</button>
    </div>
</div>

Hence the above classes can be used for styling. However the following classes are actually default properties on the Forms Component and can be changed by passing appropriate properties:

Forms.defaultProps = {
    containerClass: 'rfl-forms-container',
    submitBtnContainerClass: 'rfl-submit-btn-container',
    formContainerClass: 'rfl-form-container'
}

Validation Errors

Currently there is no support for displaying validation errors as part of react-forms-lite. The onSubmit callback will have the isValid property that you can use to render validation errors.

A minimal example

import React from 'react';
import ReactDOM from 'react-dom';

import Forms from 'react-forms-lite';

let specs = {
    mySpec: {
        name: 'mySpec',
        fields: {
            myInput: {id: 'my-input', className: 'my-input', label: 'Text Field'},
            myDatePicker: {id: 'datepicker', type: 'datepicker', label: 'DatePicker'}
        }
    }
};

class App extends React.Component {
    constructor(props) {
        super(props);
    }
    
    onFormsSubmit(formData) {
        console.log(formData);
    }
    
    render() {
        return <div>
                <Forms specs={specs} onSubmit={this.onFormsSubmit} />
            </div>
    }
}

ReactDOM.render(<App />, document.getElementById('app'));

Note: This Component is the result of learning React and using it in a project. It will hopefully significantly improve with the use of Redux/React-redux/Immutable.js as I improve my knowledge of the React ecosystem.