mirror of
https://github.com/Drop-OSS/drop-base.git
synced 2025-11-10 04:22:15 +10:00
feat: add text input modal
This commit is contained in:
@ -5,7 +5,7 @@
|
||||
:z-height="(modalIdx + 1) * 50"
|
||||
:loading="modal.loading"
|
||||
:data="modal.data"
|
||||
@event="(event: string) => handleCallback(modalIdx, event)"
|
||||
@event="(event: string, ...args: any[]) => handleCallback(modalIdx, event, args)"
|
||||
/>
|
||||
<div id="modalstack" />
|
||||
</template>
|
||||
@ -13,7 +13,7 @@
|
||||
<script setup lang="ts">
|
||||
const stack = useModalStack();
|
||||
|
||||
async function handleCallback(modalIdx: number, event: string) {
|
||||
async function handleCallback(modalIdx: number, event: string, args: any[]) {
|
||||
const modal = stack.value[modalIdx];
|
||||
console.log(modal);
|
||||
const close = () => {
|
||||
@ -24,7 +24,7 @@ async function handleCallback(modalIdx: number, event: string) {
|
||||
// I kinda hate this but it's how Vue works so....
|
||||
(modal.loading as unknown as boolean) = true;
|
||||
try {
|
||||
await modal.callback(event, close);
|
||||
await modal.callback(event, close, ...args);
|
||||
} finally {
|
||||
(modal.loading as unknown as boolean) = false;
|
||||
}
|
||||
|
||||
114
components/TextInputModal.vue
Normal file
114
components/TextInputModal.vue
Normal file
@ -0,0 +1,114 @@
|
||||
<template>
|
||||
<TransitionRoot
|
||||
as="template"
|
||||
:show="true"
|
||||
:style="{ 'z-index': props.zHeight }"
|
||||
>
|
||||
<Dialog class="relative z-50" @close="emit('event', 'cancel')">
|
||||
<TransitionChild
|
||||
as="template"
|
||||
enter="ease-out duration-300"
|
||||
enter-from="opacity-0"
|
||||
enter-to="opacity-100"
|
||||
leave="ease-in duration-200"
|
||||
leave-from="opacity-100"
|
||||
leave-to="opacity-0"
|
||||
>
|
||||
<div
|
||||
class="fixed inset-0 bg-zinc-950 bg-opacity-75 transition-opacity"
|
||||
/>
|
||||
</TransitionChild>
|
||||
|
||||
<div class="fixed inset-0 z-10 w-screen overflow-y-auto">
|
||||
<div
|
||||
class="flex min-h-full items-start justify-center p-4 text-center sm:items-center sm:p-0"
|
||||
>
|
||||
<TransitionChild
|
||||
as="template"
|
||||
enter="ease-out duration-300"
|
||||
enter-from="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
|
||||
enter-to="opacity-100 translate-y-0 sm:scale-100"
|
||||
leave="ease-in duration-200"
|
||||
leave-from="opacity-100 translate-y-0 sm:scale-100"
|
||||
leave-to="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
|
||||
>
|
||||
<div
|
||||
class="relative transform rounded-lg bg-zinc-900 text-left shadow-xl transition-all sm:my-8 sm:w-full sm:max-w-lg"
|
||||
>
|
||||
<div class="px-4 pb-4 pt-5 space-y-4 sm:p-6 sm:pb-4">
|
||||
<div class="sm:flex sm:items-start">
|
||||
<div class="mt-3 text-center sm:mt-0 sm:text-left">
|
||||
<DialogTitle
|
||||
as="h3"
|
||||
class="text-base font-semibold text-zinc-100"
|
||||
>{{ props.data.title }}
|
||||
</DialogTitle>
|
||||
<div class="mt-2">
|
||||
<p class="text-sm text-zinc-400">
|
||||
{{ props.data.description }}
|
||||
</p>
|
||||
</div>
|
||||
<form @submit.prevent="() => emit('event', 'submit', v)">
|
||||
<input
|
||||
v-model="v"
|
||||
type="text"
|
||||
:placeholder="$t('library.collection.namePlaceholder')"
|
||||
class="block w-full rounded-md border-0 bg-zinc-800 py-1.5 text-white shadow-sm ring-1 ring-inset ring-zinc-700 placeholder:text-zinc-400 focus:ring-2 focus:ring-inset focus:ring-blue-600 sm:text-sm sm:leading-6"
|
||||
/>
|
||||
<button class="hidden" type="submit" />
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div
|
||||
class="rounded-b-lg bg-zinc-800 px-4 py-3 sm:flex sm:gap-x-2 sm:flex-row-reverse sm:px-6"
|
||||
>
|
||||
<LoadingButton
|
||||
:loading="props.loading"
|
||||
@click="emit('event', 'submit', v)"
|
||||
type="submit"
|
||||
class="w-full sm:w-fit"
|
||||
>
|
||||
{{ props.data.buttonText ?? "Confirm" }}
|
||||
</LoadingButton>
|
||||
<button
|
||||
type="button"
|
||||
class="mt-3 inline-flex w-full justify-center rounded-md bg-zinc-800 px-3 py-2 text-sm font-semibold text-zinc-100 shadow-sm ring-1 ring-inset ring-zinc-700 hover:bg-zinc-900 sm:mt-0 sm:w-auto"
|
||||
@click="emit('event', 'cancel')"
|
||||
ref="cancelButtonRef"
|
||||
>
|
||||
Cancel
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</TransitionChild>
|
||||
</div>
|
||||
</div>
|
||||
</Dialog>
|
||||
</TransitionRoot>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import {
|
||||
Dialog,
|
||||
DialogTitle,
|
||||
TransitionChild,
|
||||
TransitionRoot,
|
||||
} from "@headlessui/vue";
|
||||
import type {
|
||||
ModalDatas,
|
||||
ModalEvents,
|
||||
ModalType,
|
||||
} from "../composables/modal-stack";
|
||||
|
||||
const props = defineProps<{
|
||||
zHeight: number;
|
||||
loading: boolean;
|
||||
data: ModalDatas[ModalType.TextInput];
|
||||
}>();
|
||||
const emit = defineEmits<{
|
||||
(e: "event", v: ModalEvents[ModalType.TextInput], s?: string): void;
|
||||
}>();
|
||||
|
||||
const v = ref(props.data.dft || "");
|
||||
</script>
|
||||
@ -1,10 +1,12 @@
|
||||
import type { Component } from "vue";
|
||||
import ConfirmationModal from "../components/ConfirmationModal.vue";
|
||||
import NotificationModal from "../components/NotificationModal.vue";
|
||||
import TextInputModal from "../components/TextInputModal.vue";
|
||||
|
||||
export type ModalCallbackType<T extends ModalType> = (
|
||||
event: ModalEvents[T],
|
||||
close: () => void
|
||||
close: () => void,
|
||||
...args: any[]
|
||||
) => Promise<void> | void;
|
||||
|
||||
export interface ModalStackElement<T extends ModalType> {
|
||||
@ -18,11 +20,13 @@ export interface ModalStackElement<T extends ModalType> {
|
||||
export enum ModalType {
|
||||
Confirmation,
|
||||
Notification,
|
||||
TextInput
|
||||
}
|
||||
|
||||
export type ModalEvents = {
|
||||
[ModalType.Confirmation]: "confirm" | "cancel";
|
||||
[ModalType.Notification]: "close";
|
||||
[ModalType.TextInput]: "cancel" | "submit"
|
||||
};
|
||||
|
||||
export type ModalDatas = {
|
||||
@ -36,11 +40,19 @@ export type ModalDatas = {
|
||||
description: string;
|
||||
buttonText?: string;
|
||||
};
|
||||
[ModalType.TextInput]: {
|
||||
title: string,
|
||||
description: string,
|
||||
buttonText?: string,
|
||||
dft?: string,
|
||||
placeholder?: string,
|
||||
}
|
||||
};
|
||||
|
||||
const modalComponents: { [key in ModalType]: Component } = {
|
||||
[ModalType.Confirmation]: ConfirmationModal,
|
||||
[ModalType.Notification]: NotificationModal,
|
||||
[ModalType.TextInput]: TextInputModal,
|
||||
};
|
||||
|
||||
export function createModal<T extends ModalType>(
|
||||
|
||||
Reference in New Issue
Block a user