import React, { useEffect, useState } from 'react';
import { Input } from 'antd';
import styles from './styles.less';
import { antiAssign } from '@/apollo-table/utils/utils';
import { ApolloTextAreaProps } from '../editInterface';

const TextArea = (props: ApolloTextAreaProps) => {
    const { maxLength, onChange, emitTrigger, onEmitChange } = props;
    const selfProps = antiAssign(props, ['value', 'onChange', 'emitTrigger', 'onEmitChange']);
    const [value, setValue] = useState(props.value);
    useEffect(() => {
        setValue(props.value);
    }, [props.value]);
    const changeValue = (e) => {
        setValue(e.target.value);
        if (typeof onChange === 'function') {
            onChange(e.target.value);
        }
        if (!emitTrigger || emitTrigger === 'onChange') {
            emitChange(e);
        }
    };

    const emitChange = (e) => {
        if (typeof onEmitChange === 'function') {
            onEmitChange(e.target.value);
        }
    };
    if (emitTrigger === 'onBlur') {
        selfProps.onBlur = emitChange;
    }
    return (
        <div className={styles.container}>
            <Input.TextArea className={styles.input} {...selfProps} value={value} onChange={changeValue} />
            {maxLength && (
                <span className={styles.wordNumber}>{`已输入${(value || '').length}/${maxLength}`}</span>
            )}
        </div>
    );
};
export default TextArea;
