import { Form, Select as AntSelect } from 'antd';
import { ElementStatus } from 'types/elements';

const Select = ({
  value = null,
  setCurrentValue,
  status,
  validateStatus,
  message,
  options,
  nameMiddleware,
  ...props
}) => {
  return (
    <Form.Item validateStatus={validateStatus} help={message}>
      <AntSelect
        {...props}
        disabled={status === ElementStatus.Disabled}
        loading={status === ElementStatus.Loading}
        optionFilterProp="children"
        value={value}
        onChange={val => setCurrentValue(val)}
      >
        <AntSelect.Option value={null}>Не выбрано</AntSelect.Option>
        {options.map((option, i) => {
          if (option)
            return (
              <AntSelect.Option key={i} value={option.value}>
                {nameMiddleware ? nameMiddleware(option.name) : option.name}
              </AntSelect.Option>
            );
          return null;
        })}
      </AntSelect>
    </Form.Item>
  );
};

export default Select;
