import React, { useRef } from 'react';
import InputSearch from '../../extra/dataEntry/textSelect';
import { ApolloInputSearchProps } from '../editInterface';
import { antiAssign } from '../../../../utils/utils';
import { onBlurFn } from '../onBlurFn';
import s from './index.less';

export const ApolloInputSearch = (props: ApolloInputSearchProps) => {
    const { onChange, request } = props;
    const selfProps = antiAssign(props, ['columnConfig', 'onChange', 'request']);
    const isOpen = useRef();
    const changeValue = (value: any) => {
        if (typeof onChange === 'function') {
            if (typeof onBlurFn === 'function' && !isOpen.current) {
                onBlurFn({ ...props, value });
            }
            onChange(value, value);
        }
    };
    const singleBlurFn = (e: boolean) => {
        isOpen.current = e;
    };
    return (
        <InputSearch
            className={s.select}
            request={request}
            fieldNames={{ value: 'fieldValueValue', label: 'fieldValueName' }}
            {...selfProps}
            onDropdownVisibleChange={singleBlurFn}
            onChange={changeValue}
            initDataType="onfocus"
        />
    );
};
