<template>
  <t-radio-group
    v-model="valueData"
    allow-uncheck
    :options="options"
    :disabled="!config.writable"
    style="min-height: 32px; width: 100%"
    @change="emitChange"
  ></t-radio-group>
</template>

<script setup name="RADIO">
import { ref, computed, watch } from 'vue';
const props = defineProps({
  config: { type: Object },
  modelValue: { type: String },
});
const emit = defineEmits(['update:modelValue', 'change']);

const valueData = ref(null);
const options = computed(() => {
  const config = props.config || {};

  return !config.options
    ? []
    : typeof config.options === 'string'
    ? JSON.parse(config.options)
    : config.options;
});

const emitChange = () => {
  emit('update:modelValue', valueData.value);
  emit('change', valueData.value);
};

watch(
  () => props.modelValue,
  (val, oldval) => {
    if (val === oldval) return;
    valueData.value = val || null;
  },
  {
    immediate: true,
  }
);
</script>
