daily pastebin goal
23%
SHARE
TWEET

Untitled

a guest Jul 18th, 2018 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react';
  2. import T from 'prop-types';
  3.  
  4. class Form extends React.Component {
  5.   static propTypes = {
  6.     onSubmit: T.func.isRequired,
  7.     children: T.func.isRequired,
  8.   };
  9.  
  10.   constructor(props, ...rest) {
  11.     super(props, ...rest);
  12.     this.state = { input: props.values || {} };
  13.   }
  14.  
  15.   handleChange = evt => {
  16.     evt.preventDefault();
  17.     const { id, name, value } = evt.target;
  18.     this.setState(({ input }) => ({
  19.       input: { ...input, [name || id]: value },
  20.     }));
  21.   };
  22.  
  23.   handleSubmit = evt => {
  24.     evt.preventDefault();
  25.     this.props.onSubmit({ values: this.state.input });
  26.   };
  27.  
  28.   render() {
  29.     const { children, onSubmit, ...rest } = this.props;
  30.  
  31.     return (
  32.       <form onSubmit={this.handleSubmit} {...rest}>
  33.         {children({ onChange: this.handleChange, ...this.state, ...rest })}
  34.       </form>
  35.     );
  36.   }
  37. }
  38.  
  39. export default Form;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top