Advertisement
Guest User

Untitled

a guest
Jul 24th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import * as React from 'react';
  2. import { connect } from 'react-redux';
  3. import * as shortid from 'shortid';
  4. import { validate } from 'validate.js';
  5.  
  6. import {IStore} from '../../store/store';
  7. import {isSuccess, isFail, isLoading} from '../../store/common/actions';
  8. const CallbackValidation = require('./CallbackFormValidation');
  9. const CSS = require('./CallbackForm.styl');
  10.  
  11. interface ICallbackFormBaseProps {
  12.     className?: string;
  13.     showLabel?: boolean;
  14.     isSuccess: boolean;
  15.     isLoading: boolean;
  16. }
  17.  
  18. interface ICallbackFormState {
  19.     inputId: string;
  20.     value: string;
  21.     isValid: boolean;
  22. }
  23.  
  24. interface ICallbackFormDispatchProps {
  25.     send: (email: string) => void;
  26. }
  27.  
  28. interface ICallbackFormProps extends ICallbackFormBaseProps, ICallbackFormDispatchProps {
  29. }
  30.  
  31. export class CallbackForm extends React.PureComponent<ICallbackFormProps, ICallbackFormState> {
  32.     public static defaultProps: Partial<ICallbackFormProps> = {
  33.         className: '',
  34.         showLabel: true,
  35.     };
  36.  
  37.     constructor(props) {
  38.         super(props);
  39.         this.state = {
  40.             inputId: shortid.generate(),
  41.             value: '',
  42.             isValid: false,
  43.         };
  44.     }
  45.  
  46.     keyUpHandler = () => {
  47.         const { value } = this.state;
  48.         this.setState({
  49.             isValid: !validate({email: value}, CallbackValidation),
  50.         });
  51.     }
  52.     changeHandler = (event: React.ChangeEvent<HTMLInputElement>) => {
  53.         const input: HTMLInputElement = (event.target as HTMLInputElement);
  54.         this.setState({
  55.             value: input.value,
  56.         });
  57.     }
  58.     submitHandler = () => {
  59.         const { value } = this.state;
  60.         this.props.send(value);
  61.     }
  62.  
  63.     render() {
  64.         const {className, showLabel} = this.props;
  65.         const {inputId, value, isValid} = this.state;
  66.         const classNames = [CSS.form, className].join(' ');
  67.         return (
  68.             <div className={classNames}>
  69.                 {showLabel && <label htmlFor={inputId} className={CSS.label}>Присоединяйтесь:</label>}
  70.                 <input
  71.                     className={CSS.email}
  72.                     type='email'
  73.                     id={inputId}
  74.                     placeholder='Email'
  75.                     value={value}
  76.                     onKeyUp={this.keyUpHandler}
  77.                     onChange={this.changeHandler}
  78.                 />
  79.                 <button
  80.                     type='submit'
  81.                     className={CSS.button}
  82.                     disabled={!isValid}
  83.                     onClick={this.submitHandler}
  84.                 >
  85.                     Отправить
  86.                 </button>
  87.             </div>
  88.         );
  89.     }
  90. }
  91.  
  92. const mapStateToProps = (state: IStore) => ({
  93.     isSuccess: state.common.isSuccess,
  94.     isLoading: state.common.isLoading,
  95. });
  96.  
  97. const mapDispatchToProps = (dispatch): ICallbackFormDispatchProps => ({
  98.     send: (email: string) => {
  99.         console.log(email);
  100.     },
  101. });
  102.  
  103. export default connect(mapStateToProps, mapDispatchToProps)(CallbackForm);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement