content: /**
   * Rutilus
   *
   * @homepage https://gmrutilus.github.io
   * @license Apache-2.0
   */
  
  import React from 'react';
  
  
  /**
   * Is the data in the field valid?
   * @param {String} v Value
   * @param {String} type Validation type
   * @returns {Boolean}
   */
  function validateInput(v, type) {
      /** @type {Function} */
      let exec = () => true;
  
      switch (type) {
          case 'Number':
              exec = (v) => !isNaN(+v) && (v + '').length > 0;
              break;
          case 'String':
              exec = (v) => (v + '').length > 0;
              break;
  
          default: break;
      }
  
      return exec(v);
  }
  
  
  /**
   * Handles when the field's input component's value changes
   * @param {String} validationType Validation type
   * @param {Function} handler
   * @param {Event} event
   */
  function handleInputOnChange(validationType, handler, event) {
      const value = event.target.value;
      let isValid = validateInput(value, validationType);
      handler(value, isValid);
  }
  
  
  /**
   * Handles when the field's input component's value changes
   * @param {Function} handler
   * @param {Event} event
   */
  function handleInputOnChangeCheckbox(handler, event) {
      handler(event.target.checked, true);
  }
  
  
  /**
   * @param {String} value,
   * @param {String} type,
   * @param {String} label,
   * //@param {String} queryParamName, not used
   * @param {String} validate,
   * @param {Array<String>} options,
   * @param {Function} inputOnChangeHandler,
   * @returns {XML}
   * @constructor
   */
  const Field = ({
      value,
      type,
      label,
      //queryParamName, not used
      validate,
      options,
  
      inputOnChangeHandler,
  }) => {
      const fieldType = type.toLowerCase();
  
      let isValid = validateInput(value, validate);
  
      switch (fieldType) {
          case 'checkbox':
              return (
                  <div className="form-group">
                      <label>
                          <input className="form-check-input"
                                 type="checkbox"
                                 defaultChecked={value}
                                 onChange={handleInputOnChangeCheckbox.bind(null, inputOnChangeHandler)} />
                          {' ' + label}
                      </label>
                  </div>
              );
              break;
  
          case 'radio':
              return (
                  <div className="form-group">
                      {label + ' '}
                      {options.map((o, i) => (
                          <label key={i + '_' + i} style={{ paddingRight: '10px' }}>
                              <input className="form-radio-input"
                                     key={i}
                                     type="radio"
                                     name={label}
                                     value={o[1]}
                                     defaultChecked={o[1] === value}
                                     onChange={handleInputOnChange.bind(null, validate, inputOnChangeHandler)} />
                              {' ' + o[0]}
                          </label>
                      ))}
                  </div>
              );
              break;
  
          default:
              return (
                  <div className={'form-group ' + (isValid ? '' : 'has-error')}>
                      <label>{label}</label>
                      <input className="Field-input form-control"
                             type={fieldType}
                             defaultValue={value}
                             onChange={handleInputOnChange.bind(null, validate, inputOnChangeHandler)} />
                  </div>
              );
              break;
      }
  
  };
  
  export default Field;
  