"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AdaptableFormControlTextClear = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const StringExtensions_1 = require("../../../Utilities/Extensions/StringExtensions");
const FieldWrap_1 = tslib_1.__importDefault(require("../../../components/FieldWrap"));
const SimpleButton_1 = tslib_1.__importDefault(require("../../../components/SimpleButton"));
const Input_1 = tslib_1.__importDefault(require("../../../components/Input"));
exports.AdaptableFormControlTextClear = React.forwardRef((props, ref) => {
    let closeButtonTooltip = props.value ? 'Clear' : null;
    const inputRef = React.useRef(null);
    const { focusOnClear = true } = props;
    return (React.createElement(FieldWrap_1.default, { onClick: props.onClick, className: props.className, style: {
            background: 'var(--ab-color-defaultbackground)',
            overflow: 'visible',
            width: '100%',
            ...props.style,
        } },
        React.createElement(Input_1.default, { autoFocus: props.autoFocus, style: props.inputStyle, ref: (r) => {
                inputRef.current = r;
                if (!ref) {
                    return;
                }
                if (typeof ref === 'function') {
                    ref(r);
                }
                else {
                    ref.current = r;
                }
            }, type: "text", placeholder: props.placeholder, value: props.value, onChange: (x) => props.OnTextChange(x.target.value) }),
        React.createElement(SimpleButton_1.default, { variant: "text", icon: "close", tooltip: closeButtonTooltip, px: 0, py: 0, marginRight: 1, onClick: () => {
                props.OnTextChange('');
                const input = inputRef.current;
                if (!focusOnClear) {
                    return;
                }
                requestAnimationFrame(() => {
                    if (input && input.focus) {
                        input.focus();
                    }
                });
            }, disabled: StringExtensions_1.StringExtensions.IsNullOrEmpty(props.value.toString()) })));
});
