|
| 1 | +<template> |
| 2 | + <div class="vts-input-wrapper"> |
| 3 | + <UiLabel |
| 4 | + v-if="slots.label || label" |
| 5 | + :accent="labelAccent" |
| 6 | + :for="id" |
| 7 | + :href="learnMoreUrl" |
| 8 | + :icon |
| 9 | + :required="wrapperController.required" |
| 10 | + > |
| 11 | + <slot name="label">{{ label }}</slot> |
| 12 | + </UiLabel> |
| 13 | + <slot /> |
| 14 | + <UiInfo v-for="{ content, accent } of messages" :key="content" :accent="accent"> |
| 15 | + {{ content }} |
| 16 | + </UiInfo> |
| 17 | + </div> |
| 18 | +</template> |
| 19 | + |
| 20 | +<script lang="ts" setup> |
| 21 | +import UiInfo, { type InfoAccent } from '@core/components/ui/info/UiInfo.vue' |
| 22 | +import UiLabel, { type LabelAccent } from '@core/components/ui/label/UiLabel.vue' |
| 23 | +import { useMapper } from '@core/composables/mapper.composable' |
| 24 | +import { useRanked } from '@core/composables/ranked.composable.ts' |
| 25 | +import type { MaybeArray } from '@core/types/utility.type' |
| 26 | +import { IK_INPUT_WRAPPER_CONTROLLER } from '@core/utils/injection-keys.util' |
| 27 | +import { toArray } from '@core/utils/to-array.utils' |
| 28 | +import type { IconDefinition } from '@fortawesome/fontawesome-common-types' |
| 29 | +import { useArrayMap } from '@vueuse/core' |
| 30 | +import { provide, reactive, useId } from 'vue' |
| 31 | +
|
| 32 | +export type InputWrapperMessage = MaybeArray<string | { content: string; accent?: InfoAccent }> |
| 33 | +
|
| 34 | +export type InputWrapperController = { |
| 35 | + id: string |
| 36 | + labelAccent: LabelAccent |
| 37 | + required: boolean |
| 38 | +} |
| 39 | +
|
| 40 | +const { message: _message } = defineProps<{ |
| 41 | + label?: string |
| 42 | + learnMoreUrl?: string |
| 43 | + icon?: IconDefinition |
| 44 | + message?: InputWrapperMessage |
| 45 | +}>() |
| 46 | +
|
| 47 | +const slots = defineSlots<{ |
| 48 | + default(): any |
| 49 | + label?(): any |
| 50 | +}>() |
| 51 | +
|
| 52 | +const id = useId() |
| 53 | +
|
| 54 | +const unsortedMessages = useArrayMap( |
| 55 | + () => toArray(_message), |
| 56 | + item => ({ |
| 57 | + content: typeof item === 'object' ? item.content : item, |
| 58 | + accent: typeof item === 'object' ? (item.accent ?? 'info') : 'info', |
| 59 | + }) |
| 60 | +) |
| 61 | +
|
| 62 | +const messages = useRanked(unsortedMessages, ({ accent }) => accent, ['danger', 'warning', 'success', 'info']) |
| 63 | +
|
| 64 | +const labelAccent = useMapper<InfoAccent, LabelAccent>( |
| 65 | + () => messages.value[0]?.accent, |
| 66 | + { |
| 67 | + info: 'neutral', |
| 68 | + success: 'neutral', |
| 69 | + warning: 'warning', |
| 70 | + danger: 'danger', |
| 71 | + }, |
| 72 | + 'neutral' |
| 73 | +) |
| 74 | +
|
| 75 | +const wrapperController = reactive({ |
| 76 | + id, |
| 77 | + labelAccent, |
| 78 | + required: false, |
| 79 | +}) satisfies InputWrapperController |
| 80 | +
|
| 81 | +provide(IK_INPUT_WRAPPER_CONTROLLER, wrapperController) |
| 82 | +</script> |
| 83 | + |
| 84 | +<style lang="postcss" scoped> |
| 85 | +.vts-input-wrapper { |
| 86 | + display: flex; |
| 87 | + flex-direction: column; |
| 88 | + gap: 0.4rem; |
| 89 | +} |
| 90 | +</style> |
0 commit comments