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

export const ApolloInput = (props: ApolloInputProps) => {
    const { maxLength, onChange, value, style } = props;
    const selfProps = antiAssign(props, ['columnConfig', 'onChange', 'style', 'onEmitChange']);
    const changeValue = (value: any) => {
        if (typeof onChange === 'function') {
            onChange(value);
        }
    };

    const newStyle: any = {
        ...style,
    };
    const wordNumStyle: any = {};
    if (maxLength) {
        newStyle.paddingRight = '110px';
        if (style && style.minHeight) {
            newStyle.minHeight = Number(style.minHeight) + 10;
            newStyle.paddingRight = '11px';
            newStyle.paddingBottom = '28px';
            wordNumStyle.bottom = '4px';
            wordNumStyle.marginTop = '0';
        }
    }
    return (
        <Consumer>
            {({ locale }) => {
                return (
                    <div className={styles.container}>
                        <Input
                            className={styles.input}
                            style={newStyle}
                            {...selfProps}
                            onChange={(e) => {
                                changeValue(e.target.value);
                            }}
                        />
                        {!!maxLength && (
                            <span className={styles.wordNumber} style={wordNumStyle}>{`${locale.alreadyInput} ${
                                (value || '').length
                            }/${maxLength}`}</span>
                        )}
                    </div>
                );
            }}
        </Consumer>
    );
};
