All files / src/lib Editable.jsx

70.91% Statements 39/55
75% Branches 30/40
72% Functions 18/25
69.81% Lines 37/53
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266              16x 16x           16x 16x 16x   16x 16x 16x       1x 1x       1x 1x       1x                                 14x       2x       1x       15x 15x                   11x   1x   3x                                                                               3x 6x             3x                     1x                 11x                   15x 15x                     30x               23x 23x                       23x                                                                 38x 15x           23x         1x                                         1x                    
import React from 'react';
import PropTypes from 'prop-types';
import './Editable.css';
 
export default class Editable extends React.Component {
 
  constructor(props) {
    super(props);
    this.state = {
      editing: false,
      hover: false,
      value: props.value || ''
    };
 
    this.saveButton = React.createRef();
    this.cancelButton = React.createRef();
    this.inputBox = React.createRef();
 
    this.onClick = this.onClick.bind(this);
    this.hoverOn = this.hoverOn.bind(this);
    this.hoverOff = this.hoverOff.bind(this);
  }
 
  _onSave = () => {
    const {onSave} = this.props;
    this.setState({editing: false}, () => onSave(this.state.value));
  };
 
  _onCancel = () => {
    const {onCancel} = this.props;
    this.setState({editing: false}, () => onCancel());
  };
 
  onChange = e => {
    this.setState({value: e.target.value});
  };
 
  onCheckboxChange = e => {
    let values = this.state.value;
    if (e.target.checked) {
      if (!values.includes(e.target.value)) {
        values.push(e.target.value);
      }
    } else {
      values.splice(values.indexOf(e.target.value), 1);
    }
 
    this.setState({value: values});
  };
 
  onClick() {
    this.setState({editing: true});
  }
 
  hoverOn() {
    this.setState({hover: true});
  }
 
  hoverOff() {
    this.setState({hover: false});
  }
 
  renderInput() {
    const {type, options, placeholder, name} = this.props;
    switch (type) {
      case 'text':
      case 'email':
      case 'number':
      case 'date':
      case 'datetime-local':
      case 'color':
      case 'time':
      case 'month':
      case 'week':
        return this.getInput(type, placeholder);
      case 'textarea':
        return this.getTextarea(placeholder);
      case 'select':
        return this.getSelect(options);
      case 'radio':
        return this.getRadio(options, type);
      case 'checkbox':
        return this.getCheckbox(options, type, name);
    }
  }
 
  getCheckbox(options, type, name) {
    return options.map(option => (
        <label className="easy-edit-checkbox-label">
          <input
              type={type}
              className="easy-edit-radio-button"
              value={option.value}
              name={name}
              onChange={this.onCheckboxChange}
              checked={this.state.value.includes(option.value)}
          />{option.label}
        </label>
    ));
  }
 
  getRadio(options, type) {
    return options.map(option => (
        <label className="easy-edit-radio-label">
          <input
              key={option.value}
              ref={this.inputBox}
              type={type}
              className="easy-edit-radio-button"
              value={option.value}
              onChange={this.onChange}
              checked={option.value === this.state.value}
          />{option.label}
        </label>
    ));
  }
 
  getSelect(options) {
    let opt = options.map(option => (
        <option
            value={option.value}
            selected={option.value === this.state.value}
        >
          {option.label}
        </option>
    ));
    return (
        <select
            onChange={this.onChange}
            ref={this.inputBox}
        >
          {opt}
        </select>
    );
  }
 
  getTextarea(placeholder) {
    return <textarea
        ref={this.inputBox}
        value={this.state.value}
        onChange={this.onChange}
        placeholder={placeholder}
    />;
  }
 
  getInput(type, placeholder) {
    return <input
        ref={this.inputBox}
        type={type}
        value={this.state.value}
        onChange={this.onChange}
        placeholder={placeholder}
    />;
  }
 
  renderButtons() {
    const {saveButtonLabel, saveButtonStyle, cancelButtonLabel, cancelButtonStyle} = this.props;
    return (
        <>
          {Editable.generateButton(this.saveButton, this._onSave,
              saveButtonLabel, saveButtonStyle, "save")}
          {Editable.generateButton(this.cancelButton, this._onCancel,
              cancelButtonLabel, cancelButtonStyle,"cancel")}
        </>
    )
  }
 
  static generateButton(ref, onClick, label, cssClass, name) {
    return (
        <button ref={ref} onClick={onClick} className={cssClass} name={name}>
          {label}
        </button>
    )
  }
 
  renderPlaceholder() {
    const {type, placeholder} = this.props;
    switch (type) {
      case 'text':
      case 'select':
      case 'radio':
      case 'email':
      case 'textarea':
      case 'number':
      case 'date':
      case 'datetime-local':
      case 'time':
      case 'month':
      case 'week':
        return (
            <div
                className={this.state.hover ? 'easy-edit-hover-on' : ''}
                onClick={this.onClick}
                onMouseEnter={this.hoverOn}
                onMouseLeave={this.hoverOff}
            >
              {this.state.value ? this.state.value : placeholder}
            </div>
        );
      case 'color':
        return <input
            type={type}
            value={this.state.value}
            onClick={this.onClick}
            placeholder={placeholder}
        />;
      case 'checkbox':
        console.log(this.state.value);
        return this.state.value.map(value => (
            <div
                className={this.state.hover ? 'easy-edit-hover-on' : ''}
                onClick={this.onClick}
                onMouseEnter={this.hoverOn}
                onMouseLeave={this.hoverOff}
            >
              {value}
            </div>
        ))
    }
  }
 
  render() {
    if (this.state.editing) {
      return (
          <div className="easy-edit-inline-wrapper">
            {this.renderInput()}
            {this.renderButtons()}
          </div>)
    } else {
      return this.renderPlaceholder()
    }
  }
}
 
Editable.propTypes = {
  type: PropTypes.oneOf([
    'text', 'number', 'color', 'email', 'textarea', 'date', 'datetime-local',
    'time', 'month', 'week', 'radio', 'checkbox', 'select'
  ]).isRequired,
  value: PropTypes.oneOfType([
    PropTypes.string,
    PropTypes.number,
    PropTypes.array
  ]),
  options: PropTypes.array,
  saveButtonLabel: PropTypes.string,
  saveButtonStyle: PropTypes.string,
  cancelButtonLabel: PropTypes.string,
  cancelButtonStyle: PropTypes.string,
  placeholder: PropTypes.string,
  onCancel: PropTypes.func,
  onSave: PropTypes.func.isRequired,
  name: PropTypes.string.isRequired
};
 
Editable.defaultProps = {
  value: null,
  saveButtonLabel: 'Save',
  saveButtonStyle: 'easy-edit-button',
  cancelButtonLabel: 'Cancel',
  cancelButtonStyle: 'easy-edit-button',
  placeholder: 'Click to edit',
  onCancel: () => {
  }
};