index.tsx 1.1 KB
import React from 'react';
import { Button, Space, ButtonProps } from 'antd';
import type { SpaceProps } from 'antd';

interface OptionType {
  value: any;
  label:
    | string
    | number
    | boolean
    | React.ReactElement
    | React.ReactFragment
    | React.ReactPortal
    | null
    | undefined;
}

export interface ButtonRadioProps extends SpaceProps {
  className?: string;
  prefixCls?: string;
  buttonProps?: ButtonProps;
  onChange?: (value: any) => void;
  value?: any;
  options?: OptionType[];
}

const ButtonRadio: React.FC<ButtonRadioProps> = ({
  options = [],
  buttonProps = {},
  value,
  onChange,
  ...restProps
}) => {
  return (
    <Space {...restProps}>
      {options.map((option, i) => {
        const type = option.value === value ? 'primary' : 'default';
        return (
          <Button
            key={i}
            {...buttonProps}
            type={type}
            onClick={() => {
              onChange && onChange(option.value);
            }}
          >
            {option.label}
          </Button>
        );
      })}
    </Space>
  );
};

export default ButtonRadio;