primevue-mirror/apps/showcase/doc/forms/dynamic/DynamicFormMessage.vue

33 lines
1.1 KiB
Vue
Raw Normal View History

2024-10-23 04:05:27 +00:00
<template>
<Message v-if="visible" :severity>
<slot>{{ message }}</slot>
</Message>
</template>
2024-10-23 21:57:52 +00:00
<script setup>
2024-10-23 04:05:27 +00:00
import { isNotEmpty } from '@primeuix/utils';
2024-10-23 21:57:52 +00:00
import { computed, inject } from 'vue';
2024-10-23 04:05:27 +00:00
2024-10-23 21:57:52 +00:00
const props = defineProps({
errorType: {
type: String,
default: undefined
2024-10-23 04:05:27 +00:00
},
2024-10-23 21:57:52 +00:00
severity: {
type: String,
default: 'error'
2024-10-23 04:05:27 +00:00
}
2024-10-23 21:57:52 +00:00
});
const $pcForm = inject('$pcForm', undefined); // Inject PrimeVue Form component
const $fcDynamicFormField = inject('$fcDynamicFormField', undefined);
const fieldName = computed(() => $fcDynamicFormField?.name);
const state = computed(() => $pcForm?.states?.[fieldName.value]);
const errors = computed(() => state.value?.errors);
const invalid = computed(() => state.value?.invalid);
const error = computed(() => errors.value?.find((error) => props.errorType === error.errorType || isNotEmpty(error[props.errorType])));
const message = computed(() => (props.errorType ? error.value?.message : errors.value?.[0]?.message));
const visible = computed(() => invalid.value && (error.value || props.errorType === undefined));
2024-10-23 04:05:27 +00:00
</script>