کار اولیه
This commit is contained in:
@@ -0,0 +1,178 @@
|
||||
<template>
|
||||
<UModal
|
||||
v-model:open="isModalOpen"
|
||||
:title="localModalSchema.title || ''"
|
||||
:description="localModalSchema.description || ''"
|
||||
:ui="modalUi"
|
||||
:dismissible="localModalSchema.dismissible !== false"
|
||||
>
|
||||
<!-- Body -->
|
||||
<template #body>
|
||||
<slot name="modal-body-content" />
|
||||
</template>
|
||||
|
||||
<!-- Footer -->
|
||||
<template v-if="hasFooter" #footer>
|
||||
<div class="flex w-full flex-wrap items-center justify-between gap-3">
|
||||
<!-- right actions -->
|
||||
<div class="flex flex-wrap gap-2">
|
||||
<UButton
|
||||
v-for="action in rightActions"
|
||||
:key="action.key"
|
||||
v-bind="resolveActionProps(action)"
|
||||
@click="onActionClick(action)"
|
||||
size="md"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<!-- left actions -->
|
||||
<div class="flex flex-wrap items-center gap-2">
|
||||
<UButton
|
||||
v-for="action in leftActions"
|
||||
:key="action.key"
|
||||
v-bind="resolveActionProps(action)"
|
||||
@click="onActionClick(action)"
|
||||
size="md"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div v-if="footerText" class="mt-2 text-sm text-gray-500">
|
||||
{{ footerText }}
|
||||
</div>
|
||||
</template>
|
||||
</UModal>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { ref, computed, watch } from "vue";
|
||||
|
||||
/* ---------------- props & emits ---------------- */
|
||||
|
||||
const props = defineProps({
|
||||
modalSchema: {
|
||||
type: Object,
|
||||
default: () => ({
|
||||
title: "مدال",
|
||||
description: "",
|
||||
size: "lg",
|
||||
footerDescription: "",
|
||||
actions: {
|
||||
left: [
|
||||
{
|
||||
key: "close",
|
||||
label: "بستن",
|
||||
variant: "outline",
|
||||
color: "neutral",
|
||||
closeOnClick: true,
|
||||
},
|
||||
{
|
||||
key: "save",
|
||||
label: "ذخیره",
|
||||
color: "primary",
|
||||
closeOnClick: false,
|
||||
},
|
||||
],
|
||||
},
|
||||
}),
|
||||
},
|
||||
isOpen: {
|
||||
type: Boolean,
|
||||
default: false,
|
||||
},
|
||||
});
|
||||
|
||||
const emit = defineEmits(["modal-action", "update:isOpen", "modal-close"]);
|
||||
|
||||
/* ---------------- state ---------------- */
|
||||
|
||||
const isModalOpen = ref(props.isOpen);
|
||||
const footerText = ref("");
|
||||
|
||||
/* ---------------- computed ---------------- */
|
||||
|
||||
const localModalSchema = computed(() => props.modalSchema || {});
|
||||
|
||||
const actions = computed(() => localModalSchema.value.actions || {});
|
||||
const leftActions = computed(() => {
|
||||
const actionsList = actions.value.left || [];
|
||||
return actionsList.filter((action) => action && action.key);
|
||||
});
|
||||
const rightActions = computed(() => {
|
||||
const actionsList = actions.value.right || [];
|
||||
return actionsList.filter((action) => action && action.key);
|
||||
});
|
||||
|
||||
const hasFooter = computed(
|
||||
() => leftActions.value.length > 0 || rightActions.value.length > 0
|
||||
);
|
||||
|
||||
/* -------- modal width by size -------- */
|
||||
|
||||
const modalWidthMap = {
|
||||
sm: "max-w-[400px]",
|
||||
md: "max-w-[600px]",
|
||||
lg: "max-w-[900px]",
|
||||
xl: "max-w-[1200px]",
|
||||
};
|
||||
|
||||
const modalUi = computed(() => {
|
||||
const size = localModalSchema.value.size || "md";
|
||||
const widthClass = modalWidthMap[size] || modalWidthMap.md;
|
||||
|
||||
return {
|
||||
...localModalSchema.value.ui,
|
||||
content: [widthClass, localModalSchema.value.ui?.content]
|
||||
.filter(Boolean)
|
||||
.join(" "),
|
||||
};
|
||||
});
|
||||
|
||||
/* ---------------- watchers ---------------- */
|
||||
|
||||
watch(
|
||||
() => props.isOpen,
|
||||
(val) => {
|
||||
isModalOpen.value = val;
|
||||
},
|
||||
{ immediate: true }
|
||||
);
|
||||
|
||||
watch(isModalOpen, (newVal) => {
|
||||
emit("update:isOpen", newVal);
|
||||
|
||||
if (newVal === false) {
|
||||
emit("modal-close");
|
||||
}
|
||||
});
|
||||
|
||||
watch(
|
||||
() => localModalSchema.value.footerDescription,
|
||||
(val) => {
|
||||
footerText.value = val || "";
|
||||
},
|
||||
{ immediate: true }
|
||||
);
|
||||
|
||||
/* ---------------- methods ---------------- */
|
||||
|
||||
function onActionClick(action) {
|
||||
emit("modal-action", action.key);
|
||||
|
||||
if (action.closeOnClick) {
|
||||
isModalOpen.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
function resolveActionProps(action) {
|
||||
return {
|
||||
label: action.label,
|
||||
icon: action.icon,
|
||||
color: action.color || "neutral",
|
||||
variant: action.variant || "solid",
|
||||
size: action.size || "sm",
|
||||
loading: action.loading || false,
|
||||
disabled: action.disabled || false,
|
||||
};
|
||||
}
|
||||
</script>
|
||||
@@ -0,0 +1,33 @@
|
||||
<template>
|
||||
<UModal v-model:open="confirmState.isOpen" :title="confirmState.title">
|
||||
<template #body>
|
||||
<div class="py-2 text-sm text-gray-700 dark:text-gray-300">
|
||||
{{ confirmState.message }}
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<template #footer>
|
||||
<div class="flex justify-end gap-2 w-full">
|
||||
<UButton
|
||||
color="neutral"
|
||||
variant="outline"
|
||||
:label="confirmState.cancelText"
|
||||
@click="ucoCancelConfirmModal"
|
||||
/>
|
||||
<UButton
|
||||
color="primary"
|
||||
variant="solid"
|
||||
:label="confirmState.confirmText"
|
||||
@click="ucoConfirm"
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
</UModal>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { useConfirmState, useConfirmActions } from "~/composables/useConfirm";
|
||||
|
||||
const confirmState = useConfirmState();
|
||||
const { ucoConfirm, ucoCancelConfirmModal } = useConfirmActions();
|
||||
</script>
|
||||
Reference in New Issue
Block a user