import React from 'react';
import { Input } from 'antd';
import styles from './styles.less';
import { antiAssign } from '../../../../utils/utils';
import { ApolloTextAreaProps } from '../editInterface';
import { Consumer } from '../../../context';

export const ApolloTextArea = (props: ApolloTextAreaProps) => {
    const { maxLength, onChange, value } = props;
    const selfProps = antiAssign(props, ['columnConfig', 'onChange']);

    const changeValue = (e) => {
        if (typeof onChange === 'function') {
            onChange(e.target.value);
        }
    };

    return (
        <Consumer>
            {({ locale }) => {
                return (
                    <div className={styles.container}>
                        <Input.TextArea className={styles.input} {...selfProps} onChange={changeValue} />
                        {!!maxLength && (
                            <span className={styles.wordNumber}>{`${locale.alreadyInput} ${
                                (value || '').length
                            }/${maxLength}`}</span>
                        )}
                    </div>
                );
            }}
        </Consumer>
    );
};
