Git Product home page Git Product logo

redux-inputs's Introduction

redux-inputs

npm version Build Status

redux-inputs works with redux to validate and store values from inputs and forms.

Features

  • Declarative validation
  • Declarative parsing
  • Declarative formatting
  • Async validation
  • Per-input validation
  • Cross-field validation
  • Custom input components
  • Programatic value collection
  • Programatic value initialization
  • Programatic value modification
  • Programatic input reset

Docs

Installation

npm install --save redux-inputs

Single File Example

import React from 'react';
import ReactDOM from 'react-dom';
import { createStore, combineReducers, applyMiddleware } from 'redux';
import { createInputsReducer, connectWithInputs, ReduxInputsWrapper } from 'redux-inputs';
import { Provider } from 'react-redux';
import thunk from 'redux-thunk';

// Define configuration for this form. A single input named 'email' with a default value and a function to determine validity.
const inputsConfig = {
    email: {
        defaultValue: '[email protected]',
        validator: (value) => typeof value === 'string' && value.indexOf('@') >= 0
    }
};

// Create redux store with a reducer created with the createInputsReducer function.
const reducer = combineReducers({
    inputs: createInputsReducer(inputsConfig)
});
const store = createStore(reducer, applyMiddleware(thunk));

// Define our own Field component, and wrap it in a ReduxInputsWrapper to easily create a compatible input component.
// Integration with other ui component libraries, such as material-ui, would be done here.
const Field = ({id, value, error, onChange, errorText}) => (
    <div>
        <input name={id} value={value} onChange={(e) => onChange(e.target.value)}/>
        {error ? <p style={{color: 'red'}}>{errorText}</p> : null}
    </div>
);
const ReduxInputsField = ReduxInputsWrapper(Field);

// Use the newly created ReduxInputsField by spreading in reduxInputs.inputProps.email object.
const Form = ({ inputs, reduxInputs }) => (
    <form>
        <ReduxInputsField errorText="Your email must contain an @" {...reduxInputs.inputProps.email}/>
        <h3>Input state</h3>
        <pre>{JSON.stringify(inputs, null, 2)}</pre>
    </form>
);

// Hook the form up to the store with connectWithInputs, a wrapped version of react-redux's connect.
const FormContainer = connectWithInputs(inputsConfig)(s => s)(Form);
ReactDOM.render(<Provider store={store}><FormContainer /></Provider>, document.getElementById('container'));

Contributing

Build

npm i

npm run build

Examples

npm run watch-examples & npm run serve-examples

Tests

npm test

redux-inputs's People

Contributors

jakepusateri avatar kazooie avatar stonebk avatar grantgi-zillow avatar evocateur avatar

Stargazers

@_Quantum_OS_GTT_CS_GTTQLM❤️ avatar Josh avatar Nikola avatar Sung Jeon avatar  avatar Maroof Shittu avatar  avatar Shang-Hung Shih avatar Frano Hartman avatar Guillaume Dumoulin avatar Sal Rahman avatar Darcy Turk avatar Sebastian Busch avatar josh hembree avatar Kat Reinhart avatar Chris Hart avatar Andy Chen avatar Fergus Ruston avatar Ken Wallace avatar Rob McGuire avatar Dmitrii Bykov avatar Victor Jolissaint avatar Patrick Kranzlmueller avatar Amo Wu avatar Yannick Huard avatar Keith Ort avatar Chauncey McAskill avatar Joe Seifi avatar Ian Moore avatar Evgeny Erohin avatar Aleksandras Nelkinas avatar  avatar zooofeex avatar taichi avatar Philip Thrasher avatar James Edward Lewis II avatar Ben Linton avatar Maruf Siddiqui avatar Hannu-Pekka Heinäjärvi avatar Philipp Andreychev avatar Tobias Deekens avatar Jair Peña avatar I-Lun Chen avatar Leonard Kinday avatar Shaun avatar Ganeshan Venkataraman avatar Shawn Jonnet avatar Alexander Wallin avatar Riccardo Zanutta avatar Ivan Rudoy avatar Denis avatar ZaneDev avatar Lan Qingyong avatar iyn avatar Max Kolodezhnyi avatar Parker avatar Drew Miller avatar harris.dev avatar Peeyush Kumar avatar Andrew Fuller avatar Alexey V. avatar Ian Sinnott avatar Henry Tao avatar Jacky Shikerya avatar Gordan Ratkovic avatar Jason Morganson avatar Luís Rodrigues avatar Andris Sīlis avatar Laughing avatar Leo Simmons avatar Edison.Hsu avatar justin avatar Jack Lam avatar Matt Seccafien avatar Garcia avatar Vladimir Kapustin avatar Edy Ionescu avatar Alexander Kampfmann avatar Trần Trung Tín avatar Diego Barros avatar Jackson avatar Vlad Andersen avatar Nick Wientge avatar Vasco Lameiras avatar Richard Hess avatar David Konsumer avatar 延 avatar Gerald Yeo avatar Gregg B avatar evandrix avatar Kendrick Von Ledet avatar Ryan Finni avatar Robert McGuinness avatar Odi avatar Chad Paulson avatar Christopher Pappas avatar Eric Clemmons avatar  avatar William Horton avatar Grant Gingell avatar

Watchers

Jonathan Ultis avatar evandrix avatar Richard Hess avatar Martin An avatar Scott Smith avatar  avatar James Cloos avatar  avatar Kris Ellery avatar eNddy avatar  avatar  avatar httpstergeek avatar Jasmine Mann avatar  avatar

redux-inputs's Issues

[Question] PropType Warning Messages

Hello!

First off, great module has worked extremely well for all our use cases.
Quick question, I am seeing failed PropType messages in the debug console for the following props:

  • dispatchChange
  • _id

I couldn't find anywhere specific in the docs that these should be passed to ReduxInputsWrapper and was under the impression _id would be handled by the library when passing an id prop.

If you could point me in the right direction that would be great, apologies if i've missed anything obvious.

Thanks!

Browser autocomplete only works with one field

Take an input form with address, zipcode, state. Chrome will try and autocomplete all these fields at once if the user uses that feature. However, this modification doesn't trigger the onChange on all of these inputs. This means the next time any of those fields are changed, the others get cleared, since their state change never makes it into redux. Can we do something about this? Is this a browser bug?

3.0 Wishlist

  • inputsConfig -> formConfig
    • It can get confusing when we have both inputsConfig and inputConfig. "form" is a collection of inputs and a lot easier to distinguish from "input".
  • Pre-wrapped native input elements
    • At zillow, we have a separate library zsg-redux-inputs that has all the wrapped elements. It would be nice to have simple pre-wrapped native input elements rather than forcing everyone to build their own.

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.