import { __read } from "tslib";
import * as React from 'react';
import classNames from '../../../utils/classnames';
import FormCell from '../formcell';
import './index.css';
import { ConfigProvider, Input } from 'tea-component';
import { usePlatform } from '../../../utils/platform';
export default function Textarea(_a) {
    var _b;
    var 
    // 系统属性
    id = _a.id, className = _a.className, style = _a.style, events = _a.events, 
    // 组件属性
    label = _a.label, labelVisible = _a.labelVisible, defaultValue = _a.defaultValue, placeholder = _a.placeholder, maxLength = _a.maxLength, disabled = _a.disabled, autoFocus = _a.focus, layout = _a.layout, requiredFlag = _a.requiredFlag, counterVisible = _a.counterVisible, size = _a.size;
    var platform = usePlatform();
    var cls = classNames((_b = {
            'weda-ui': true,
            'weda-textarea': true
        },
        _b[className] = className,
        _b));
    var subCls = classNames({
        'weui-cell': true,
        'weui-cell_active': true,
        'weui-cell_form': true,
        'weui-cell_disabled': disabled,
    });
    var _c = __read(React.useState(defaultValue), 2), value = _c[0], setValue = _c[1];
    var _d = __read(React.useState((defaultValue === null || defaultValue === void 0 ? void 0 : defaultValue.length) || 0), 2), counter = _d[0], setCounter = _d[1];
    // 两次默认值不同时, 需要刷新value
    var prevDefaultValueRef = React.useRef();
    React.useEffect(function () {
        if (prevDefaultValueRef.current !== defaultValue) {
            prevDefaultValueRef.current = defaultValue;
            setValue(defaultValue);
            setCounter((defaultValue === null || defaultValue === void 0 ? void 0 : defaultValue.length) || 0);
        }
    });
    var onChange = function (e) {
        var text = typeof e === 'string' ? e : e.target.value;
        setValue(text);
        setCounter(text.length || 0);
        events.change({ value: text });
    };
    var textAreaElement;
    if (platform === 'h5') {
        textAreaElement = (React.createElement("div", { className: subCls },
            React.createElement("div", { className: "weui-cell__bd" },
                React.createElement("textarea", { className: "weui-textarea", placeholder: placeholder, value: value, onChange: onChange, maxLength: maxLength, disabled: disabled, autoFocus: autoFocus }),
                counterVisible ? (React.createElement("div", { className: "weui-textarea-counter" },
                    React.createElement("span", null, counter),
                    "/",
                    maxLength)) : null)));
    }
    else {
        textAreaElement = (React.createElement(ConfigProvider, { classPrefix: "wedatea2td" },
            React.createElement(Input.TextArea, { size: size, placeholder: placeholder, value: value, onChange: onChange, maxLength: maxLength, disabled: disabled, autoFocus: autoFocus, showCount: true })));
    }
    return (React.createElement(FormCell, { size: size, id: id, className: cls, style: style, label: labelVisible ? label : null, layout: layout, multiCell: false, requiredFlag: requiredFlag }, textAreaElement));
}
//# sourceMappingURL=index.js.map