import React from "react";
import PropTypes from "prop-types";
import cn from "classnames";
import { IControlledRadioButtonProps } from "./radio-button.d";

const RadioButton = (props: IControlledRadioButtonProps) => {
  const {
    className,
    name,
    value,
    label,
    onSelected,
    inline,
    checked,
    disabled,
    globalDisabled
  } = props;

  const testId = props["data-testid"] || "honeyui-radio";

  return (
    <div
      className={cn(className, "form-radio", inline && "form-radio-inline")}
      data-testid={testId}
    >
      <input
        data-testid={`${testId}-input`}
        className="form-radio-input"
        type="radio"
        disabled={disabled || globalDisabled}
        name={name}
        id={name}
        value={String(value)}
        checked={checked}
        onChange={() => onSelected && onSelected(value)}
      />
      <label className="form-radio-label" htmlFor={name} data-testid={`${testId}-label`}>
        {label}
      </label>
    </div>
  );
};

RadioButton.displayName = "RadioButton";

RadioButton.defaultProps = {
  className: "",
  checked: false,
  inline: false,
  onSelected: () => {}
};

RadioButton.propTypes = {
  className: PropTypes.string,
  name: PropTypes.string,
  value: PropTypes.oneOfType([PropTypes.number, PropTypes.string, PropTypes.bool]).isRequired,
  checked: PropTypes.bool,
  label: PropTypes.string.isRequired,
  inline: PropTypes.bool,
  onSelected: PropTypes.func,
  disabled: PropTypes.bool
};

export default RadioButton;
