"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _react = _interopRequireWildcard(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _classnames = _interopRequireDefault(require("classnames"));

var _constants = require("../../constants");

var _FormField = _interopRequireDefault(require("../FormField"));

var _Hint = _interopRequireDefault(require("../Hint"));

var _Radio = _interopRequireDefault(require("../Radio"));

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

// Copyright 1999-2020. Plesk International GmbH. All rights reserved.

/* eslint-disable react/prefer-stateless-function */

/**
 * `FormFieldRadioButtons` is used for making a choice among a set of mutually exclusive, related options. Only one option can be chosen.
 * @since 0.2.0
 */
class FormFieldRadioButtons extends _react.Component {
  render() {
    const {
      baseClassName,
      className,
      options,
      autoFocus,
      multi,
      ...props
    } = this.props;
    return /*#__PURE__*/_react.default.createElement(_FormField.default, (0, _extends2.default)({
      className: (0, _classnames.default)(baseClassName, className)
    }, props), ({
      getId,
      getName,
      getValue,
      setValue,
      isDisabled
    }) => options.map(({
      value,
      label,
      description,
      disabled,
      indentFields = []
    }, index) => /*#__PURE__*/_react.default.createElement("div", {
      className: `${baseClassName}__radio`,
      key: value
    }, /*#__PURE__*/_react.default.createElement(_Radio.default, {
      name: getName(),
      checked: getValue() === value,
      onChange: isChecked => {
        if (isChecked) {
          setValue(value);
        }
      },
      disabled: isDisabled() || disabled,
      inputProps: {
        id: getId() ? `${getId()}-${value}` : null,
        name: getName(),
        value,
        autoFocus: index === 0 && autoFocus ? true : undefined
      }
    }, label || value), description || indentFields ? /*#__PURE__*/_react.default.createElement("div", {
      className: `${baseClassName}__content`
    }, description && /*#__PURE__*/_react.default.createElement(_Hint.default, {
      component: "div",
      className: `${_FormField.default.defaultProps.baseClassName}__description`
    }, description), indentFields.map(field => {
      if (isDisabled() || disabled || getValue() !== value) {
        return /*#__PURE__*/(0, _react.cloneElement)(field, {
          disabled: true
        });
      }

      return field;
    })) : null)));
  }

}

FormFieldRadioButtons.propTypes = {
  /**
   * Options definition.
   * @since 0.2.0
   */
  options: _propTypes.default.arrayOf(_propTypes.default.shape({
    /**
     * Option value.
     */
    value: _propTypes.default.string.isRequired,

    /**
     * Option label.
     */
    label: _propTypes.default.node.isRequired,

    /**
     * Text hint located below option for providing additional information.
     */
    description: _propTypes.default.node,

    /**
     * Set true if option is not available to choice.
     */
    disabled: _propTypes.default.bool,

    /**
     * Optional array of `FormField` components under this option.
     */
    indentFields: _propTypes.default.array
  })).isRequired,

  /**
   * Value of current chosen option.
   * @deprecated Use [Form](#!/Form) values.
   * @since 0.2.0
   */
  value: _propTypes.default.string,

  /**
   * Disabled state of field.
   * @since 0.2.0
   */
  disabled: _propTypes.default.bool,

  /**
   * The browser will automatically focus on the component upon rendering the screen
   * @since 1.9.0
   */
  autoFocus: _propTypes.default.bool,

  /**
   * @ignore Field cannot be multiple
   */
  multi: _propTypes.default.bool,

  /**
   * @ignore
   */
  className: _propTypes.default.string,

  /**
   * @ignore
   */
  baseClassName: _propTypes.default.string
};
FormFieldRadioButtons.defaultProps = {
  value: undefined,
  disabled: undefined,
  autoFocus: undefined,
  multi: undefined,
  className: undefined,
  baseClassName: `${_constants.CLS_PREFIX}form-field-radio-buttons`
};
var _default = FormFieldRadioButtons;
exports.default = _default;