All files / src/qComponents/QRadio/src QRadio.vue

57.14% Statements 8/14
55.56% Branches 10/18
71.43% Functions 5/7
57.14% Lines 8/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135                                                                    23x                                                                                             1x       1x   1x       1x       1x               1x             1x                                                
<template>
  <label
    class="q-radio"
    :class="wrapClass"
    role="radio"
    :aria-checked="isChecked"
    :aria-disabled="isDisabled"
    :tabindex="tabIndex"
    @keyup.space="handleSpaceKeyUp"
  >
    <span class="q-radio__input">
      <span class="q-radio__inner" />
      <input
        class="q-radio__original"
        type="radio"
        aria-hidden="true"
        tabindex="-1"
        :name="name"
        :value="value"
        :checked="isChecked"
        :disabled="isDisabled"
        @change="handleChange"
      />
    </span>
    <span
      class="q-radio__label"
      @keydown.stop
    >
      <slot>{{ label }}</slot>
    </span>
  </label>
</template>
 
<script>
import Emitter from '../../mixins/emitter';
 
export default {
  name: 'QRadio',
  componentName: 'QRadio',
 
  mixins: [Emitter],
 
  inject: {
    qForm: {
      default: null
    },
    qFormItem: {
      default: null
    },
    qRadioGroup: {
      default: null
    }
  },
 
  model: {
    prop: 'checked',
    event: 'change'
  },
 
  props: {
    /**
     * the value of Radio label
     */
    label: { type: String, default: '' },
    /**
     * binding value
     */
    value: { type: [String, Number, Boolean], default: '' },
    checked: { type: [String, Number, Boolean], default: false },
    /**
     * whether Radio is disabled
     */
    disabled: { type: Boolean, default: false },
    /**
     * as native name
     */
    name: { type: String, default: null }
  },
 
  computed: {
    isGroup() {
      return Boolean(this.qRadioGroup);
    },
 
    isChecked() {
I      if (this.isGroup) return this.qRadioGroup?.value === this.value;
 
I      if (typeof this.checked === typeof this.value) {
        return this.checked === this.value;
      }
 
      return Boolean(this.checked);
    },
 
    isDisabled() {
      return (
        this.disabled ||
        (this.qForm?.disabled ?? false) ||
        (this.qRadioGroup?.disabled ?? false)
      );
    },
 
    wrapClass() {
      return {
        'q-radio_disabled': this.isDisabled,
        'q-radio_checked': this.isChecked
      };
    },
 
    tabIndex() {
      return this.isDisabled || (this.isGroup && !this.isChecked) ? -1 : 0;
    }
  },
 
  methods: {
    handleSpaceKeyUp() {
      if (this.isGroup) return;
 
      this.$emit('change', this.value);
    },
 
    handleChange() {
      /**
       * triggers when the value changes
       */
      this.$emit('change', this.value);
 
      if (this.isGroup) {
        this.dispatch('QRadioGroup', 'change', this.value);
      }
    }
  }
};
</script>