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

redux-duck-form

v0.3.0

Published

Provides a simple API to manage your form state in the store

Downloads

3

Readme

redux-simple-form

A redux duck to manage your form state in the store.

Motvation

Sometimes you need to save form state after the component unmount or another component must watch changes in the form state.

Also you have to be able to run validations in order to submit the form.

This duck is an abtraction wich provides useful methods to change a value, clear the field, run validations and handle the form submition.

Simple usage

$ yarn add redux-simple-form
import { combineReducers } from 'redux';
import createFormReducer from 'redux-simple-form';

const fields = [
  'email',
  { name: 'age', optional: true },
];

const simpleForm = createFormReducer('simpleForm', fields);

export default combineReducers({
  simpleForm,
});

This creates a reducer which initial state looks like

{
  simpleForm: {
    email: {
      value: '',
      error: null,
      touched: false,
      valid: false,
      optional: false
    },
    age: { 
      value: '',
      error: null,
      touched: false,
      valid: false,
      optional: true
    }
  }
}

Now you can do stuff

import { setFieldValue, clearField, runValidator, submitForm } from 'readux-simple-form';

store.dispatch(setFieldValue('simpleForm')('email', '[email protected]');

/* new shape
  email: {
    value: '[email protected]',
    error: null,
    touched: true,
    valid: false,
    optional: false
  }
*/

API

createFormReducer

(formName:string, fields: []) => Reducer

Main function, create a new redux reducer. The formName must be equal to the key wich stores this part of the state. Creates an slice for each item in the array passed as second argument.

setFieldValue

(formName) => (fieldName, value) => Action

Dispatch this action in order to change the value of fieldName in the formName form. Returns an object action.

{
  type: SET_FIELD_VALUE,
  payload: {
    value,
  },
  meta: {
    formName,
    fieldName,
  },
}

clearField

(formName) => (fieldName) => Action

Dispatch this action in order to reset fieldName in the formName form. Returns an object action.

{
  type: CLEAR_FIELD,
  meta: {
    formName,
    fieldName,
  },
}

runValidator

(formName, selector?) => (fieldName, validator) => Action

This thunk validates the field found in the global state using the selector if one is provided selector(getState())[formName] or getState()[formName].

To determine if the field is valid a validator function must be provided. This function can be a promise based (to do async validation) or a simple function wich returns an error string when the field has errors or empty string when the field is valid.

validator

(value: String) => Error:String || Empty:String' Example

const validator = value => (
  !value ? "Field can't be empty" : ''
);

selector

(state: Object) => subState:Object Example

const selector = state => state.some.nesting

submitForm

(formNAme) => (onSubmit, onError) => null

Calls the function onSubmit when all no optionals fields are valid. The fields values and dispatch function are passed as arguments to onSubmit callback. If there existe at least one error, onError is called with an error object and the dispatch function.

Contribute

This project is under development. Pull request and ideas are welcome.

License

MIT