"use strict";
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Textarea = void 0;
var react_1 = __importDefault(require("react"));
var react_textarea_autosize_1 = __importDefault(require("react-textarea-autosize"));
var bem_react_classname_1 = require("bem-react-classname");
var cn_1 = require("../cn");
var scroll_to_1 = __importDefault(require("../lib/scroll-to"));
var vars_1 = require("../vars");
/**
 * Компонент многострочного текстового ввода.
 */
var Textarea = /** @class */ (function (_super) {
    __extends(Textarea, _super);
    function Textarea() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.cn = bem_react_classname_1.createCn('textarea');
        _this.state = {
            focused: false,
            value: _this.props.defaultValue || '',
        };
        _this.handleFocus = function () {
            _this.setState({ focused: true });
            if (_this.props.onFocus) {
                _this.props.onFocus();
            }
        };
        _this.handleBlur = function (event) {
            _this.setState({ focused: false });
            if (_this.props.onBlur) {
                _this.props.onBlur(event);
            }
        };
        _this.handleChange = function (event) {
            var value = event.target.value;
            if (_this.props.value === undefined) {
                _this.setState({ value: value });
            }
            if (_this.props.onChange) {
                _this.props.onChange(value);
            }
        };
        _this.handlePaste = function (event) {
            if (_this.props.onPaste) {
                _this.props.onPaste(event);
            }
        };
        _this.handleHeightChange = function (height) {
            if (_this.props.onHeightChange) {
                _this.props.onHeightChange(height);
            }
        };
        _this.handleKeyPress = function (event) {
            if (_this.props.onKeyPress) {
                _this.props.onKeyPress(event);
            }
        };
        _this.handleKeyDown = function (event) {
            if (_this.props.onKeyDown) {
                _this.props.onKeyDown(event);
            }
        };
        return _this;
    }
    Textarea.prototype.render = function () {
        var _a;
        var _this = this;
        var value = this.props.value === undefined ? this.state.value : this.props.value;
        var textareaProps = (_a = {
                className: this.cn('control')
            },
            _a[this.props.autosize ? 'inputRef' : 'ref'] = function (control) {
                _this.control = control;
            },
            _a.autoComplete = this.props.autocomplete === false ? 'off' : 'on',
            _a.disabled = this.props.disabled,
            _a.id = this.props.id,
            _a.name = this.props.name,
            _a.value = value,
            _a.tabIndex = this.props.tabIndex,
            _a.placeholder = this.props.placeholder,
            _a.maxLength = this.props.maxLength,
            _a.onChange = this.handleChange,
            _a.onFocus = this.handleFocus,
            _a.onBlur = this.handleBlur,
            _a.onPaste = this.handlePaste,
            _a.onKeyPress = this.handleKeyPress,
            _a.onKeyDown = this.handleKeyDown,
            _a);
        return (react_1.default.createElement("span", { className: this.cn({
                disabled: this.props.disabled,
                focused: this.state.focused,
                autosize: this.props.autosize,
                size: this.props.size,
                view: this.props.view,
                width: this.props.width,
                resize: this.props.resize,
                invalid: !!this.props.error,
                'has-label': !!this.props.label,
                'has-value': !!value,
                'has-icon': !!this.props.icon
            }), ref: function (root) {
                _this.root = root;
            }, "data-test-id": this.props['data-test-id'] },
            react_1.default.createElement("span", { className: this.cn('inner') },
                !!this.props.label
                    && (react_1.default.createElement("span", { className: this.cn('top') }, this.props.label)),
                this.props.autosize
                    ? (react_1.default.createElement(react_textarea_autosize_1.default, __assign({}, textareaProps, { maxRows: this.props.maxRows, minRows: this.props.minRows, style: { maxHeight: this.props.maxHeight }, onHeightChange: this.handleHeightChange })))
                    : react_1.default.createElement("textarea", __assign({}, textareaProps)),
                this.props.icon
                    && (react_1.default.createElement("span", { className: this.cn('icon') }, this.props.icon)),
                (this.props.error || this.props.hint)
                    && (react_1.default.createElement("span", { className: this.cn('sub') }, this.props.error || this.props.hint)))));
    };
    /**
     * Устанавливает фокус на поле ввода.
     */
    Textarea.prototype.focus = function () {
        this.control.focus();
    };
    /**
     * Снимает фокус с поля ввода.
     */
    // eslint-disable-next-line class-methods-use-this
    Textarea.prototype.blur = function () {
        if (document.activeElement) {
            document.activeElement.blur();
        }
    };
    /**
     * Скроллит страницу до поля ввода.
     */
    Textarea.prototype.scrollTo = function () {
        var elementRect = this.root.getBoundingClientRect();
        scroll_to_1.default({
            targetY: (elementRect.top + window.pageYOffset) - vars_1.SCROLL_TO_CORRECTION,
        });
    };
    Textarea.defaultProps = {
        view: 'default',
        width: 'default',
        autocomplete: true,
        disabled: false,
        autosize: true,
        resize: 'none',
        size: 'm',
    };
    return Textarea;
}(react_1.default.PureComponent));
exports.Textarea = Textarea;
exports.default = cn_1.withTheme(Textarea);

//# sourceMappingURL=textarea.js.map
