'use strict';

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

exports.__esModule = true;
exports.RadioControl = void 0;

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

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

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

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

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

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

var _FormControl2 = require("./FormControl");

var _Radio = _interopRequireDefault(require("react-bootstrap/lib/Radio"));

var RadioControl =
/*#__PURE__*/
function (_FormControl) {
  (0, _inheritsLoose2.default)(RadioControl, _FormControl);

  function RadioControl() {
    return _FormControl.apply(this, arguments) || this;
  }

  var _proto = RadioControl.prototype;

  _proto.inputConverter = function inputConverter(value) {
    return {
      checked: this.props.value == value,
      // eslint-disable-line
      value: value
    };
  };

  _proto.componentDidMount = function componentDidMount() {
    this.testAutomationIdHandler();
  };

  _proto.componentDidUpdate = function componentDidUpdate() {
    this.testAutomationIdHandler();
  };

  _proto.testAutomationIdHandler = function testAutomationIdHandler() {
    if (this.input && !this.props.hasFormGroupContainer) {
      this.input.parentNode.setAttribute('data-test-automation-id', this.props.testAutomationId);
    }
  };

  _proto.getInputDOMNode = function getInputDOMNode() {
    return this.input;
  };

  _proto.render = function render() {
    var _this = this;

    var _this$getPropsFromCon = this.getPropsFromContext(),
        children = _this$getPropsFromCon.children,
        hiddenLabel = _this$getPropsFromCon.hiddenLabel,
        removeName = _this$getPropsFromCon.removeName,
        props = (0, _objectWithoutPropertiesLoose2.default)(_this$getPropsFromCon, ["children", "hiddenLabel", "removeName"]);

    return _react.default.createElement(_Radio.default, (0, _extends2.default)({}, this.getSafeProps(props), {
      ref: "input",
      type: "radio",
      "data-test-automation-id": "input",
      name: removeName ? null : props.name,
      inputRef: function inputRef(ref) {
        return _this.input = ref;
      }
    }), _react.default.createElement("span", {
      "data-test-automation-id": "label",
      className: !!hiddenLabel ? 'sr-only' : null
    }, children));
  };

  return RadioControl;
}(_FormControl2.FormControl);

exports.RadioControl = RadioControl;
(0, _defineProperty2.default)(RadioControl, "propTypes", Object.assign({}, _FormControl2.FormControl.propTypes, {
  removeName: _propTypes.default.bool
}));
(0, _defineProperty2.default)(RadioControl, "defaultProps", Object.assign({}, _FormControl2.FormControl.defaultProps, {
  testAutomationId: 'radio',
  removeName: false
}));