All files / src/components/atoms BaseRequiredField.vue

100% Statements 54/54
86.66% Branches 13/15
100% Functions 1/1
100% Lines 54/54

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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182  1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x   1x         1x 1x                           1x                         1x                       1x                   1x             1x 5x 1x     1x 5x 1x   1x 1x 1x 1x 1x     1x 5x 1x 4x 1x     1x 5x 5x 5x 5x 5x 5x 5x 5x 1x             1x 5x 5x 1x                                                                                                                
<template>
  <component
    :is="is"
    v-bind="mergedAttrs"
    :model-value="resolvedModelValue"
    @update:model-value="resolvedUpdater"
  >
    <template #label>
      <div class="inline-flex items-center gap-1">
        <v-chip v-if="required" color="error" label size="x-small" class="text-white mb-1">
          {{ t('ui.form.required') }}
        </v-chip>
        <v-chip v-else color="grey" label size="x-small" class=" mb-1" variant="outlined">
          {{ t('ui.form.optional') }}
        </v-chip>
        <span class="ml-3">{{ baseLabel }}</span>
      </div>
    </template>
    <template v-for="(_, name) in $slots" #[name]="slotData">
      <slot v-if="name !== 'label'" :name="name" v-bind="slotData || {}"></slot>
    </template>
  </component>
</template>
 
<script setup lang="ts">
// region import
import { computed, useAttrs } from 'vue'
import { useI18n } from 'vue-i18n'
// endregion import
 
// region Component
defineOptions({ name: 'BaseRequiredField' })
// endregion Component
 
// region types
// endregion types
 
// region interface
// endregion interface
 
// region props
const props = withDefaults(defineProps<{
  modelValue?: any
  is: 'v-text-field' | 'v-select' | 'v-textarea' | 'v-combobox'
  labelKey?: string
  label?: string
  required?: boolean
}>(), {
  modelValue: null,
  required: false,
})
// endregion props
 
// region emits
const emit = defineEmits<{
  (e: 'update:modelValue', v: any): void
}>()
// endregion emits
 
// region expose
// endregion expose
 
// region slots
// endregion slots
 
// region i18n
const { t } = useI18n()
// endregion i18n
 
// region router
// endregion router
 
// region store
// endregion store
 
// region consts
const rawAttrs = useAttrs() as Record<string, unknown>
// endregion consts
 
// region state
// endregion state
 
// region computed
const baseLabel = computed(() =>
  props.labelKey ? t(props.labelKey) : (props.label ?? '')
)
 
/** v-bind(field) 優先で modelValue / updater を解決 */
const resolvedModelValue = computed(() =>
  rawAttrs.modelValue !== undefined ? rawAttrs.modelValue : props.modelValue
)
 
const resolvedUpdater = (v: any) => {
  const updater = rawAttrs['onUpdate:modelValue'] as ((v: any) => void) | undefined
  if (updater) return updater(v)
  emit('update:modelValue', v)
}
 
/** v-select の placeholder を必ず無効化 */
const selectPlaceholderAttrs = computed(() =>
  props.is === 'v-select'
    ? { placeholder: '', 'persistent-placeholder': false }
    : {}
)
 
/** 二重渡し防止のため、modelValue 関連は attrs から取り除いて透過 */
const passthroughAttrs = computed(() => {
  const {
    modelValue,
    ['onUpdate:modelValue']: _omit,
    placeholder,
    ['persistent-placeholder']: _pp,
    ...rest
  } = rawAttrs
  return rest
})
// const passthroughAttrs = computed(() => {
//   const { modelValue, ['onUpdate:modelValue']: _omit, ...rest } = rawAttrs
//   return rest
// })
 
/** v-bind は1回だけにまとめる */
const mergedAttrs = computed(() => ({
  ...passthroughAttrs.value,
  ...selectPlaceholderAttrs.value,
}))
 
// endregion computed
 
// region validation
// endregion validation
 
// region watchers
// endregion watchers
 
// region effects
// endregion effects
 
// region permissions
// endregion permissions
 
// region api
// endregion api
 
// region handlers
// endregion handlers
 
// region lifecycle
// endregion lifecycle
 
// region provide
// endregion provide
 
// region inject
// endregion inject
 
// region expose-public
// endregion expose-public
 
// region devtools
// endregion devtools
 
// region test-ids
// endregion test-ids
 
// region variable
// endregion variable
 
// region properties
// endregion properties
 
// region methods
// endregion methods
 
// region export
// endregion export
</script>
 
<style scoped>
 
</style>