Skip to content

Commit 248e3dc

Browse files
committed
refactor(demos): update and create messages composable
1 parent 593aff1 commit 248e3dc

File tree

7 files changed

+45
-21
lines changed

7 files changed

+45
-21
lines changed

dev/App.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import AppTopBar from '@/components/app/AppTopbar.vue'
55

66
<template>
77
<main class="p-2">
8+
<Toast position="bottom-right" />
89
<div class="">
910
<AppTopBar />
1011
<div class="pl-2">

dev/auto-import.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,6 @@ declare global {
7474
// for type re-export
7575
declare global {
7676
// @ts-ignore
77-
export type { Component, ComponentPublicInstance, ComputedRef, ExtractDefaultPropTypes, ExtractPropTypes, ExtractPublicPropTypes, InjectionKey, PropType, Ref, VNode, WritableComputedRef } from 'vue'
77+
export type { Component, ComponentPublicInstance, ComputedRef, DirectiveBinding, ExtractDefaultPropTypes, ExtractPropTypes, ExtractPublicPropTypes, InjectionKey, PropType, Ref, MaybeRef, MaybeRefOrGetter, VNode, WritableComputedRef } from 'vue'
7878
import('vue')
7979
}

dev/components.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ declare module 'vue' {
99
export interface GlobalComponents {
1010
AppFooter: typeof import('./components/app/AppFooter.vue')['default']
1111
AppTopbar: typeof import('./components/app/AppTopbar.vue')['default']
12+
PrimeData: typeof import('./components/demo/PrimeData.vue')['default']
1213
PrimeInput: typeof import('./components/demo/PrimeInput.vue')['default']
1314
PrimeOutput: typeof import('./components/demo/PrimeOutput.vue')['default']
1415
PrimeSchemaEditor: typeof import('./components/demo/PrimeSchemaEditor.vue')['default']

dev/components/demo/PrimeInput.vue

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
<script setup lang='ts'>
2-
import type { ToastMessageOptions } from 'primevue/toast'
32
import { FormKit, FormKitSchema } from '@formkit/vue'
43
import JsonEditorVue from 'json-editor-vue'
5-
import { useToast } from 'primevue/usetoast'
64
import { ref } from 'vue'
75
import FormKitDebug from '../../../src/components/FormKitDebug.vue'
6+
import { useMessages } from '../../composables/messages'
87
98
const props = defineProps<{
109
header: string
@@ -16,11 +15,7 @@ const props = defineProps<{
1615
hideTabs?: boolean
1716
}>()
1817
19-
const toast = useToast()
20-
21-
function showMessage(severity: ToastMessageOptions['severity'], summary: string, detail: string) {
22-
toast.add({ severity, summary, detail, life: 1000 })
23-
}
18+
const { showSuccessMessage } = useMessages()
2419
2520
const formSchema = ref(props.schema)
2621
const formData = ref(props.data)
@@ -29,13 +24,12 @@ const documentationLink = `https://primevue.org/${props.header.replace('Prime',
2924
const inputClass = 'p-button p-component p-formkit-button'
3025
3126
async function submitHandler() {
32-
showMessage('success', 'Form Submitted ...', `${props.header} submitted successfully`)
27+
showSuccessMessage('Form Submitted ...', `${props.header} submitted successfully`)
3328
}
3429
</script>
3530

3631
<template>
3732
<div>
38-
<Toast position="bottom-right" />
3933
<h2 class="text-color-[var(--p-primary-color)] pb-2">
4034
{{ header }}
4135
</h2>

dev/components/demo/PrimeOutput.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ const formData = ref(props.data)
1818

1919
<template>
2020
<div>
21-
<Toast position="bottom-right" />
2221
<h2 class="text-color-[var(--p-primary-color)] pb-2">
2322
{{ header }}
2423
</h2>

dev/components/demo/PrimeSchemaEditor.vue

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
<script setup lang='ts'>
2-
import type { ToastMessageOptions } from 'primevue/toast'
32
import { FormKitSchema } from '@formkit/vue'
43
import { useClipboard } from '@vueuse/core'
54
import { useInputEditor } from 'my-library'
6-
import { useToast } from 'primevue/usetoast'
75
86
import { ref } from 'vue'
7+
import { useMessages } from '../../composables/messages'
98
109
const props = defineProps<{
1110
header: string
@@ -18,27 +17,23 @@ const schemaResultFormKey = ref(0)
1817
1918
const { editorDataToSchema, editorDataToJson, editorDataToCode } = useInputEditor()
2019
21-
const toast = useToast()
22-
23-
function showMessage(severity: ToastMessageOptions['severity'], summary: string, detail: string) {
24-
toast.add({ severity, summary, detail, life: 2000 })
25-
}
20+
const { showSuccessMessage } = useMessages()
2621
2722
const formSchema = ref(props.schema)
2823
const formData = reactive(props.data)
2924
3025
const data = ref({})
3126
3227
async function submitHandler() {
33-
showMessage('success', `Form Input (${formData._dollar_formkit}) updated`, 'Input creation completed successfully ...')
28+
showSuccessMessage(`Form Input (${formData._dollar_formkit}) updated`, 'Input creation completed successfully ...')
3429
3530
data.value = { }
3631
schemaResultFormKey.value += 1
3732
Object.assign(formData, { ...formData, schemaResultFormKey: schemaResultFormKey.value })
3833
}
3934
4035
async function submitHandler2() {
41-
showMessage('success', `Data submitted`, data.value)
36+
showSuccessMessage(`Data submitted`, data.value.toString())
4237
}
4338
4439
const schemaResult = computed(() => editorDataToSchema(formData))
@@ -56,7 +51,6 @@ function copyObject() {
5651

5752
<template>
5853
<div>
59-
<Toast position="bottom-right" />
6054
<h2 class="text-color-[var(--p-primary-color)] pb-2">
6155
{{ header }}
6256
</h2>

dev/composables/messages.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import type { ToastMessageOptions } from 'primevue/toast'
2+
import { useToast } from 'primevue/usetoast'
3+
4+
export enum MessageSeverity {
5+
SUCCESS = 'success',
6+
INFO = 'info',
7+
WARN = 'warn',
8+
ERROR = 'error',
9+
}
10+
11+
export function useMessages() {
12+
const toast = useToast()
13+
14+
function showMessage(severity: ToastMessageOptions['severity'], summary: string, detail: string, life: number = 3000) {
15+
toast.add({ severity, summary, detail, life })
16+
}
17+
18+
function showSuccessMessage(summary: string, detail: string = summary, life: number = 3000) {
19+
showMessage(MessageSeverity.SUCCESS, summary, detail, life)
20+
}
21+
22+
function showInfoMessage(summary: string, detail: string = summary, life: number = 3000) {
23+
showMessage(MessageSeverity.INFO, summary, detail, life)
24+
}
25+
26+
function showWarnMessage(summary: string, detail: string = summary, life: number = 3000) {
27+
showMessage(MessageSeverity.WARN, summary, detail, life)
28+
}
29+
30+
function showErrorMessage(summary: string, detail: string = summary, life: number = 3000) {
31+
showMessage(MessageSeverity.ERROR, summary, detail, life)
32+
}
33+
34+
return { showSuccessMessage, showInfoMessage, showWarnMessage, showErrorMessage }
35+
}

0 commit comments

Comments
 (0)