<template>
  <a-radio-group :default-value="defaultValue" button-style="solid" @change="radioChange">
    <a-radio-button v-for="item in list" :key="item.value" :value="item.value">
      {{ item.label }}
    </a-radio-button>
  </a-radio-group>
</template>

<script>
export default {
  name: 'HRadioButton',
  /**
   * 自组件对父组件暴露的属性
   * @property {String} defaultValue - 默认值
   * @property {Array<Object>>} list - 列表 [{ value, label }]
   */
  props: {
    defaultValue: {
      type: String,
      required: false,
      default: ''
    },
    list: {
      type: Array,
      required: false,
      default: () => [
        { value: 'date', mode: 'date', label: '日' },
        { value: 'month', mode: 'month', label: '月' },
        { value: 'year', mode: 'year', label: '年' }
      ],
      validator: arr => {
        const isArray = Object.prototype.toString.call(arr) === '[object Array]'
        if (isArray) {
          let flag = true
          for (let v of arr) {
            if (!v.hasOwnProperty('value') || !v.hasOwnProperty('label')) {
              flag = false
            }
          }
          return flag
        }
      }
    }
  },
  methods: {
    radioChange(e) {
      this.$emit('change', e.target.value)
    }
  }
}
</script>
