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

flux-form

v5.0.0

Published

Manage your forms with flux, any flux

Downloads

13

Readme

flux-form

Flux form is a helper for managing forms with flux

import fluxForm from 'flux-form'
import { Dispatcher } from 'flux'

const dispatcher = new Dispatcher()

const form = fluxForm('MyForm', dispatcher, {
  fields: [
    'name',
    'email',
  ],

  state: UserStore.getState(),
})

fluxForm will return a form object which contains all the fields you defined, a save action, a cancel action, and the current form's state.

class SampleFormComponent extends React.Component {
  render() {
    return (
      <div>
        <label>
          Name
          <input {...form.props.name} />
        </label>

        <label>
          Email
          <input {...form.props.email} />
        </label>

        <input type="button" onClick={form.save} />
      </div>
    )
  }
}

...form.props.name will add all of the props for the field name onto the input. The two important props are value which contains the current value, and onChange which is a function handler that sets the value.

Flux Form is meant to be bare bones and generic in that you can build adapters on top of it so it works with your favorite flavor of flux.

For example, we can have an Alt specific form utility which creates a store that you can connect your React component to.

Validation

You can provide basic validation that can be triggered manually and also runs automatically whenever the form is saved.

const form = fluxForm('MyForm', dispatcher, {
  fields: [
    'username',
    'password',
  ],

  validators: {
    // you can asynchronously validate a field by returning a promise
    // for example, you can validate that the username is not taken here
    // by making an API call and rejecting the Promise
    username(data) {
      return new Promise((resolve, reject) {
        xhr.post('/validateUsername', { user: data }, (res) => {
          if (res.statusCode === 200) {
            resolve()
          } else {
            reject(res.responseText)
          }
        })
      })
    },

    // you throw an error if it does not pass validation
    password(data) {
      if (data.length <= 6) {
        throw new Error('Password must be longer than 6 characters')
      }
    },
  },
})

The validation can be triggered and it returns a Promise. You can also pass in a node-style callback if you prefer.

form.validate().then((state) => {
  // the successful state of the form
}, (errors) => {
  // a key value pair of errors
  // for example:
  // {
  //   password: 'Password must be longer than 6 characters',
  // }
})

You can also validate specific fields by passing in an Array to validate.

form.validate(function (err, state) {
  // do something with err or state
}, ['username'])

Normalization

Form fields are normalized either manually or automatically when saving.

const form = fluxForm('MyForm', dispatcher, {
  fields: [
    'phone',
  ],

  output: {
    // some really crappy normalization lol
    phone(data) {
      // take the phone number and format it so it is without dashes and parens
      // and then add a +1 at the beginning
      return '+1' + String(data).replace(/[ |(|)|-]/g, '')
    },
  },
})

To trigger a normalization

form.normalize()

Focus

focus and blur actions are called whenever an element receives focus or is blurred.

Saving

In order to save your form state which will trigger validation and normalization you can call save on your form object.

const form = fluxForm('MyForm', dispatcher, {
  fields: [
    'address',
    'city',
    'country',
  ],
})

form.save().then((state) => {
})

Save returns a promise but you can also pass it a node-style callback.

Action Creators

Flux form works by firing off flux actions with the dispatcher you've provided. The actions are dispatched in FSA format.

The following actions are provided

  • saved
  • canceled
  • changed
  • validationFailed
  • focused
  • blurred

You use getActionCreators in order to get the action creators. Action creators are functions which return an FSA compliant payload ready to be dispatched. Each action creator has an id field which is the string that you can use in your store in order to handle the dispatch.

import { getActionCreators } from 'flux-form'

const { saved } = getActionCreators('MyForm')

// sample usage
alt.dispatch(saved('foobar')) // { type: 'MyForm/saved', payload: 'foobar' }

Flux form will automatically dispatch change events, saved, focus, etc, which you can then listen to yourself in order to keep track of state separately or in order to sync items.