GithubHelp home page GithubHelp logo

lukesammy / redux-inputs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from zillow/redux-inputs

0.0 1.0 0.0 829 KB

redux-inputs is a Javascript library that works with redux to validate and store values from inputs and forms.

License: MIT License

JavaScript 100.00%

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

gulp

Examples

npm run watch-examples & npm run serve-examples

Tests

gulp eslint
npm test

redux-inputs's People

Contributors

jakepusateri avatar kazooie avatar stonebk avatar

Watchers

 avatar

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.