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

soya-form

v0.0.19

Published

React form library for soya

Downloads

2,298

Readme

soya-form

Greenkeeper badge Build Status Coverage Status

React form library for soya.

Table of Contents

Prerequisites

The form library uses Redux. So before starting, we recommend reading Redux documentation first.

Documentation

Input and Field

Input component's purpose is to gather input from user and report it to an owner component via callback props. It also accepts value from props and renders it to the user. This Input component is processed with the createField() helper, wrapping it with a higher order component that writes to and reads from redux store state (FormSegment, to be precise). Field is what we call an Input component that's wrapped so that it's connected to redux state.

Here's an example of a simple Input component (no async validations, disabled state, error messages, only taking input and calling the callback):

import React from 'react';
import { createField } from 'soya-form';

class TextInput extends React.Component {
  render() {
    return <input type="text" value={this.props.value} onChange={this.handleChange.bind(this)} />
  }

  handleChange(event) {
    // Callback is provided by higher order component.
    this.props.handleChange(event.target.value, event);
  }
}

export default createField(TextInput);

Soya's form library doesn't care what goes on inside Input component, it's the developer's prerogative. Input component might render into canvas or read user's input from mouse movements, it's up to you. The form library also doesn't care about the nature of the value, it can be string, list, or a complex object - as long as it's acceptable to store it in redux state (this rules out types like functions or class instances).

The only contract is for Input fields to call the callback functions provided by the higher order components. To see the list of callback functions and other props provided by the higher order component, go to Field API.

Rendering Fields

Before rendering the Field component, we need to first create a Form component. This class's purpose is to represent a form, a place where all fields can register to, and orchestrating actions like form-wide validation. To create a Form component, simply give it a unique id:

import { createForm } from 'soya-form';

export default createForm('FORM_ID')(Component);

When we've created a proper Field component, we can just render it by passing the field's name:

<TextField name="firstName" />

This will render a component that reads and writes to form segment in the state tree, creating this structure:

{
  "form": {
    "FORM_ID": {
      "fields": {
        "firstName": {
          "value": null,
          "errorMessages": [],
          "isEnabled": true,
          "isValidating": false
        }
      },
      "isEnabled": true
    }
  }
}

To submit the form, we can call the Form instance's submit method, like this:

const callback = function(result) {
  if (result.isValid) {
    // Access form values with result.values
  }
};

Here's an example of result object, based on examples above:

{
  "isValid": true,
  "values": {
    "firstName": "Rick"
  }
}

For more information about the submit method, please see Form API.

Complex field names

Field names can be complex. For example:

<TextField name={['name', 'first']} />
<TextField name={['name', 'last']} />

The above components will result in the following values map:

{
  "name": {
    "first": "...",
    "last": "..."
  }
}

We can also create list, by inserting numbers instead of string in the name prop:

<TextField name={['wishlist', 0]} />
<TextField name={['wishlist', 1]} />

This results in the following values map:

{
  "wishlist": [
    "...",
    "..."
  ]
}

This complex name forms the basis of Repeatable Fields.

API

You can read the API documentation here.

License

MIT