"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.bindInputElement = void 0;
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const Input_1 = require("./Input");
class ProxiedInput extends react_1.default.Component {
    constructor() {
        super(...arguments);
        this.placeholderInputRef = react_1.default.createRef();
    }
    render() {
        const props = Object.assign({}, this.props);
        // 移除两个无效的属性
        // @ts-ignore
        delete props.inputElement;
        delete props.innerRef;
        return react_1.default.createElement(Input_1.Input, Object.assign({}, props, { type: 'text', id: this.props.id, ref: this.placeholderInputRef }));
    }
    componentDidMount() {
        const { innerRef, inputElement } = this.props;
        const parent = this.placeholderInputRef.current.parentElement;
        parent.replaceChild(inputElement, this.placeholderInputRef.current);
        this.applyAttributes();
        inputElement.focus();
        if (typeof innerRef === 'function') {
            innerRef(inputElement);
        }
        else if (innerRef !== null && typeof innerRef === 'object' && innerRef.current === null) {
            innerRef.current = inputElement;
        }
    }
    UNSAFE_componentWillUnmount() {
        const { innerRef, inputElement } = this.props;
        const parent = inputElement.parentElement;
        parent.replaceChild(this.placeholderInputRef.current, inputElement);
        if (innerRef !== null && typeof innerRef === 'object' && innerRef.current) {
            innerRef.current = null;
        }
    }
    shouldComponentUpdate(nextProps) {
        this.applyAttributes(nextProps);
        return false;
    }
    applyAttributes(props = this.props) {
        const { className, id, inputElement, style } = props;
        if (typeof className === 'string') {
            inputElement.className = className;
        }
        if (typeof id === 'string') {
            inputElement.id = id;
        }
        if (style !== null && typeof style === 'object') {
            for (const prop in style) {
                if (typeof style[prop] === 'string' && inputElement[prop]) {
                    inputElement[prop] = style[prop];
                }
            }
        }
    }
}
const Input = react_1.default.forwardRef((props, ref) => (react_1.default.createElement(ProxiedInput, Object.assign({}, props, { innerRef: ref }))));
function bindInputElement(el) {
    return (props) => react_1.default.createElement(Input, Object.assign({}, props, { inputElement: el }));
}
exports.bindInputElement = bindInputElement;
//# sourceMappingURL=ProxiedInput.js.map