All files / src/lib EasyEdit.jsx

74.55% Statements 41/55
75.56% Branches 34/45
70% Functions 14/20
73.58% Lines 39/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 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284                              16x 16x           16x 16x 16x   16x 16x 16x 16x       1x 1x       1x 1x                                       13x       2x       1x       13x 13x               9x                                   1x               1x                 1x               1x                     13x 13x                     26x           26x               26x 26x                   23x                           2x         2x                                             1x         1x                             39x 13x           26x         1x                                                         1x                  
import React from 'react';
import PropTypes from 'prop-types';
import './EasyEdit.css';
 
import Globals from './globals';
import EasyDropdown from './EasyDropdown';
import EasyInput from "./EasyInput";
import EasyParagraph from "./EasyParagraph";
import EasyRadio from "./EasyRadio";
import EasyCheckbox from "./EasyCheckbox";
import EasyColor from "./EasyColor";
 
export default class EasyEdit 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);
    this.onChange = this.onChange.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 {name, type, options, placeholder} = this.props;
    switch (type) {
      case 'text':
      case 'number':
      case 'date':
      case 'datetime-local':
      case 'time':
      case 'month':
      case 'week':
        return (
            <EasyInput
                value={this.state.value}
                placeholder={placeholder}
                onChange={this.onChange}
                type={type}
                name={name}
            />
        );
      case 'color':
        return (
            <EasyColor
                value={this.state.value}
                onChange={this.onChange}
                name={name}
            />
        );
      case 'textarea':
        return (
            <EasyParagraph
                value={this.state.value}
                placeholder={placeholder}
                onChange={this.onChange}
                name={name}
            />);
      case 'select':
        return (
            <EasyDropdown
                value={this.state.value}
                onChange={this.onChange}
                options={options}
                name={name}
            />
        );
      case 'radio':
        return (
            <EasyRadio
                value={this.state.value}
                onChange={this.onChange}
                options={options}
            />
        );
      case 'checkbox':
        return (
            <EasyCheckbox
                value={this.state.value}
                onChange={this.onCheckboxChange}
                options={options}
            />
        );
    }
  }
 
  renderButtons() {
    const {saveButtonLabel, saveButtonStyle, cancelButtonLabel, cancelButtonStyle} = this.props;
    return (
        <div className="easy-edit-button-wrapper">
          {EasyEdit.generateButton(this.saveButton, this._onSave,
              saveButtonLabel, saveButtonStyle, "save")}
          {EasyEdit.generateButton(this.cancelButton, this._onCancel,
              cancelButtonLabel, cancelButtonStyle, "cancel")}
        </div>
    )
  }
 
  setCssClasses(existingClasses) {
    return this.state.hover ?
        'easy-edit-hover-on ' + existingClasses :
        existingClasses;
  }
 
  static generateButton(ref, onClick, label, cssClass, name) {
    return (
        <button ref={ref} onClick={onClick} className={cssClass} name={name}>
          {label}
        </button>
    )
  }
 
  renderPlaceholder() {
    const {type, placeholder, options} = this.props;
    switch (type) {
      case 'text':
      case 'email':
      case 'textarea':
      case 'number':
      case 'date':
      case 'datetime-local':
      case 'time':
      case 'month':
      case 'week': {
        return (
            <div
                className={this.setCssClasses('easy-edit-wrapper')}
                onClick={this.onClick}
                onMouseEnter={this.hoverOn}
                onMouseLeave={this.hoverOff}
            >
              {this.state.value ? this.state.value : placeholder}
            </div>
        );
      }
      case 'radio':
      case 'select': {
        let selected;
        Iif (this.state.value) {
          selected = options.filter((option) => {
            return this.state.value.includes(option.value);
          });
        }
        return (
            <div
                className={this.setCssClasses('easy-edit-wrapper')}
                onClick={this.onClick}
                onMouseEnter={this.hoverOn}
                onMouseLeave={this.hoverOff}
            >
              {this.state.value ? selected[0].label : placeholder}
            </div>
        );
      }
      case 'color':
        return (
            <input
                type={type}
                value={this.state.value}
                onClick={this.onClick}
                placeholder={placeholder}
                readOnly
            />
        );
      case 'checkbox': {
        let selected;
        Iif (this.state.value) {
          selected = options.filter((option) => {
            return this.state.value.includes(option.value);
          });
        }
        return (
            <div
                className={this.setCssClasses('easy-edit-wrapper')}
                onClick={this.onClick}
                onMouseEnter={this.hoverOn}
                onMouseLeave={this.hoverOff}
            >
              {this.state.value && this.state.value.length !== 0 ? selected.map(
                  checkbox => checkbox.label) : placeholder}
            </div>);
      }
    }
  }
 
  render() {
    if (this.state.editing) {
      return (
          <div className="easy-edit-inline-wrapper">
            {this.renderInput()}
            {this.renderButtons()}
          </div>)
    } else {
      return this.renderPlaceholder()
    }
  }
}
 
EasyEdit.propTypes = {
  type: PropTypes.oneOf([
    'text', 'number', 'color', '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,
  min: PropTypes.oneOfType([
    PropTypes.string,
    PropTypes.number
  ]),
  max: PropTypes.oneOfType([
    PropTypes.string,
    PropTypes.number
  ])
};
 
EasyEdit.defaultProps = {
  value: null,
  saveButtonLabel: Globals.DEFAULT_SAVE_BUTTON_LABEL,
  saveButtonStyle: 'easy-edit-button',
  cancelButtonLabel: Globals.DEFAULT_CANCEL_BUTTON_LABEL,
  cancelButtonStyle: 'easy-edit-button',
  placeholder: Globals.DEFAULT_PLACEHOLDER,
  onCancel: () => {}
};