Skip to content

Instantly share code, notes, and snippets.

@mlapshin
Last active September 27, 2016 21:57
Show Gist options
  • Save mlapshin/538a3d0a071f2d73dc95673b4696f7b9 to your computer and use it in GitHub Desktop.
Save mlapshin/538a3d0a071f2d73dc95673b4696f7b9 to your computer and use it in GitHub Desktop.
const Input = props => {
return <input {...props} />;
}
const FormRow = props => {
return <div className="formRow">{props.children}</div>;
}
const makeBindFn = (formId, formComp) => {
return inputName => {
return {
onChange: (e) => { formComp.handleInputChange(inputName, e.target.value) },
defaultValue: formComp.state[inputName]
};
};
}
class Form extends React.Component {
constructor(props) {
super(props);
this.state = {
email: "[email protected]"
};
}
handleInputChange(inputName, newVal) {
this.setState({ [inputName]: newVal });
}
render() {
const bind = makeBindFn("login-form", this);
return (
<FormRow>
<Input {...bind("email")} someProp="someVal" />
<Input {...bind("password")} someProp="anotherVal" />
<pre>Form model: {JSON.stringify(this.state)}</pre>
</FormRow>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment