import type { RadioChangeEvent, RadioGroupProps } from 'antd';
import { Form, Radio as AntRadio } from 'antd';
import type { BaseElementProps, BaseOption } from './types';

const { Item: FormItem } = Form;

type ElementProps = BaseElementProps<string | null> & {
  options: BaseOption[];
};

export default function Radio({
  value = null,
  setValue,
  options,
  status,
  isValid,
  help,
  ...props
}: ElementProps) {
  function handleChange(e: RadioChangeEvent) {
    setValue(e.target.value);
  }

  return (
    <FormItem hasFeedback validateStatus={isValid === false ? 'error' : ''} help={help}>
      <AntRadio.Group
        value={value}
        options={options}
        onChange={handleChange}
        disabled={status === 'Disabled'}
        {...props}
      />
    </FormItem>
  );
}

export type { RadioGroupProps as RadioProps };
