import { __read } from "tslib";
import * as React from 'react';
import classNames from '../../../utils/classnames';
import FormCell from '../formcell';
import { usePlatform } from '../../../utils/platform';
import { Input as TeaInput } from 'tea-component';
export default function Input(_a) {
    var _b;
    var 
    // 系统属性
    id = _a.id, className = _a.className, style = _a.style, events = _a.events, 
    // 组件属性
    label = _a.label, labelVisible = _a.labelVisible, name = _a.name, defaultValue = _a.defaultValue, password = _a.password, placeholder = _a.placeholder, type = _a.type, maxLength = _a.maxLength, disabled = _a.disabled, autoFocus = _a.focus, layout = _a.layout, requiredFlag = _a.requiredFlag, size = _a.size;
    var platform = usePlatform();
    var cls = classNames((_b = {
            'weda-ui': true,
            'weda-input': true
        },
        _b[className] = className,
        _b));
    var subCls = classNames({
        'weui-cell': true,
        'weui-cell_active': true,
        'weui-cell_form': true,
        'weui-cell_disabled': disabled,
    });
    var inputType = getInputType(type, password);
    var _c = __read(React.useState(defaultValue), 2), value = _c[0], setValue = _c[1];
    // 两次默认值不同时, 需要刷新value
    var prevDefaultValueRef = React.useRef();
    React.useEffect(function () {
        if (prevDefaultValueRef.current !== defaultValue) {
            prevDefaultValueRef.current = defaultValue;
            setValue(defaultValue);
        }
    });
    var onChange = function (e) {
        var text = typeof e === 'string' ? e : e.target.value;
        if (maxLength > 0 && text.length > maxLength) {
            return;
        }
        var newValue = text;
        setValue(newValue);
        events.change({ value: newValue }, { originEvent: e });
    };
    var onClear = function (e) {
        events.clear({ originValue: value }, { originEvent: e });
        setValue('');
        e.preventDefault();
    };
    var onKeyDown = function (e) {
        // 确认逻辑
        if (e.key === 'enter' || e.keyCode === 13) {
            events.confirm({ value: e.target.value }, { originEvent: e });
        }
        // 数字类型的键盘, 不能输入e
        if (inputType === 'number' && e.key === 'e') {
            e.preventDefault();
        }
    };
    var onFocus = function (e) {
        events.focus({ value: e.target.value }, { originEvent: e });
    };
    var onBlur = function (e) {
        events.blur({ value: e.target.value }, { originEvent: e });
    };
    var inputElement = (React.createElement(React.Fragment, null,
        React.createElement("input", { className: "weui-input", type: inputType, placeholder: placeholder, value: value, disabled: disabled, name: name, autoFocus: autoFocus, onChange: onChange, onFocus: onFocus, onBlur: onBlur, onKeyDown: onKeyDown }),
        React.createElement("button", { className: "weui-btn_reset weui-btn_icon weui-btn_input-clear", onMouseDown: onClear },
            React.createElement("i", { className: "weui-icon-clear" }))));
    return (React.createElement(FormCell, { id: id, className: cls, style: style, label: labelVisible ? label : null, layout: layout, multiCell: false, requiredFlag: requiredFlag, size: size }, platform === 'h5' ? (React.createElement("div", { className: subCls },
        React.createElement("div", { className: "weui-cell__bd weui-flex" }, inputElement))) : (React.createElement(TeaInput, { size: size, type: inputType, placeholder: placeholder, value: value, disabled: disabled, name: name, autoFocus: autoFocus, onChange: onChange, onFocus: onFocus, onBlur: onBlur, onKeyDown: onKeyDown }))));
}
function getInputType(type, password) {
    if (password) {
        return 'password';
    }
    switch (type) {
        case 'idcard':
            return 'text';
        case 'digit':
            return 'number';
        default:
            return type;
    }
}
//# sourceMappingURL=index.js.map