/** @format */

import * as React from 'react';
import {Select} from 'antd';
import {SelectProps as AntdSelectProps} from 'antd/es/select';
const {Option} = Select;

export interface SelectOptionProp {
    key: number | string;
    label: string;
    value?: string | number;
}

export interface SelectProps extends AntdSelectProps {
    options?: SelectOptionProp[];
    loading?: boolean;
}
export default React.forwardRef((props: SelectProps, ref) => {
    const {options = [], loading = false, ...rest} = props;
    return (
        <Select
            filterOption={(input, option) => {
                if (option.props.children) {
                    return (
                        option.props.children
                            .toString()
                            .toLowerCase()
                            .indexOf(input.toString().toLowerCase()) >= 0
                    );
                }
                return false;
            }}
            loading={loading}
            disabled={loading}
            placeholder='请选择'
            {...rest}
        >
            {options.map(element => {
                return (
                    <Option title={element.label} value={element.value} key={element.key} data-props={element}>
                        {element.label}
                    </Option>
                );
            })}
        </Select>
    );
});
