All files / src/Input with-input.js

14.29% Statements 2/14
0% Branches 0/15
20% Functions 1/5
15.38% Lines 2/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39          12x 12x                                                                
/* eslint react/prop-types: 0 */
import React from 'react';
import Done from '@material-ui/icons/Done';
import * as R from 'ramda';
 
export const withInput = Component =>
  class extends React.Component {
    state = {
      value: R.isNil(this.props.defaultValue) ? this.props.value : this.props.defaultValue,
    };
 
    onChange = value => {
      this.setState({ value: value });
    };
 
    onSubmit = () => {
      this.bubbleUp(this.state.value);
    };
 
    bubbleUp = item => {
      if (R.is(Function, this.props.onSubmit)) this.props.onSubmit(item);
    };
 
    render = () => {
      const { onChange, isControlled } = this.props;
      const { value } = this.state;
 
      return (
        <Component
          {...this.props}
          onChange={isControlled ? onChange : this.onChange}
          onSubmit={isControlled ? null : this.onSubmit}
          rightIcon={isControlled || R.isNil(value) || R.isEmpty(value) ? null : Done}
          value={isControlled ? this.props.value : value}
        />
      );
    };
  };