"use strict";

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

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

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

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

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

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

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

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

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

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

var _TextInput = require("./form/TextInput");

var _Block = require("./Block");

var _Icon = require("./Icon");

var Filter =
/*#__PURE__*/
function (_React$Component) {
  (0, _inheritsLoose2.default)(Filter, _React$Component);

  function Filter(props, context) {
    var _this;

    _this = _React$Component.call(this, props, context) || this;
    _this.handleChange = _this.handleChange.bind((0, _assertThisInitialized2.default)(_this));
    _this.handleFocusField = _this.handleFocusField.bind((0, _assertThisInitialized2.default)(_this));
    return _this;
  }

  var _proto = Filter.prototype;

  _proto.handleChange = function handleChange(event) {
    this.props.onChange(this.refs.searchField.getValue());
  };

  _proto.focus = function focus() {
    this.refs.searchField.focus();
  };

  _proto.handleFocusField = function handleFocusField(event) {
    if (this.props.selectOnFocus) {
      event.target.select();
    }

    if (this.props.onFocus) {
      this.props.onFocus(event);
    }
  };

  _proto.render = function render() {
    var _this$props = this.props,
        wrapperClassName = _this$props.wrapperClassName,
        ariaLabel = _this$props.ariaLabel,
        id = _this$props.id,
        props = (0, _objectWithoutPropertiesLoose2.default)(_this$props, ["wrapperClassName", "ariaLabel", "id"]);
    Object.assign(props, {
      'aria-label': props['aria-label'] || ariaLabel,
      id: props['controlId'] || id
    });
    return _react.default.createElement(_Block.Block, {
      className: "rc-filter",
      noOffset: true
    }, _react.default.createElement(_TextInput.TextInput, (0, _extends2.default)({}, props, {
      wrapperClassName: (0, _classnames.default)('form-group', wrapperClassName),
      ref: "searchField",
      onChange: this.handleChange,
      onFocus: this.handleFocusField
    })), _react.default.createElement(_Icon.Icon, {
      name: "search"
    }));
  };

  return Filter;
}(_react.default.Component);

exports.Filter = Filter;
(0, _defineProperty2.default)(Filter, "propTypes", {
  onChange: _propTypes.default.func.isRequired,
  id: _propTypes.default.string,
  testAutomationId: _propTypes.default.string,
  wrapperClassName: _propTypes.default.string,
  selectOnFocus: _propTypes.default.bool,
  onFocus: _propTypes.default.func,
  ariaLabel: _propTypes.default.string
});
(0, _defineProperty2.default)(Filter, "defaultProps", {
  id: null,
  testAutomationId: 'fieldFilter',
  wrapperClassName: '',
  selectOnFocus: false,
  onFocus: function onFocus() {},
  ariaLabel: 'TRANSLATE:SEARCH'
});