<!--
 * @Description:It is troublesome to implement radio button group in the form. So it is extracted independently as a separate component
-->
<template>
  <Space v-bind="attrs">
    <template v-for="(item, i) in groups" :key="`fmgrp${i}`">
      <AButton :class="{ 'form-group-actived': value === item }" @click="handleClick(item)">
        {{ item }}
      </AButton>
    </template>
    <!-- 占位符，避免没有组别时没有内容导致父组件的Flex布局受影响 -->
    <span></span>
  </Space>
</template>
<script lang="ts">
  import { defineComponent, PropType } from 'vue';
  import { Space, Button } from 'ant-design-vue';
  import { useAttrs } from '/@/hooks/core/useAttrs';

  export default defineComponent({
    name: 'RadioButtonGroup',
    components: {
      Space,
      AButton: Button,
    },
    props: {
      value: {
        type: String,
      },
      groups: {
        type: Array as PropType<string[]>,
        default: () => [],
      },
    },
    emits: ['update:value'],
    setup(props, { emit }) {
      const attrs = useAttrs();

      function handleClick(value?: string) {
        if (props.value === value) {
          emit('update:value', undefined);
        } else {
          emit('update:value', value);
        }
      }

      return { attrs, handleClick };
    },
  });
</script>
<style lang="less" scoped>
  .form-group-actived {
    color: @white;
    background: @gradient-primary-color;
  }
  .form-group-actived:hover {
    color: @white;
  }
</style>
