Created
January 6, 2017 21:13
-
-
Save jakepusateri/9ac8c69bbf431edfa9ae3c34146979e1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | |
const inputsConfig = { | |
email: { | |
defaultValue: 'test@example.com', | |
validator: (value) => typeof value === 'string' && value.indexOf('@') >= 0 | |
} | |
}; | |
const reducer = combineReducers({ | |
inputs: createInputsReducer(inputsConfig) | |
}); | |
const store = createStore(reducer, applyMiddleware(thunk)); | |
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(Input); | |
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> | |
); | |
const FormContainer = connectWithInputs(inputsConfig)(s => s)(Form); | |
ReactDOM.render(<Provider store={store}><FormContainer /></Provider>, document.getElementById('container')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment