From 0fc403c25710e890490b77d807a4346165255282 Mon Sep 17 00:00:00 2001 From: Johann Schopplich Date: Fri, 17 Jan 2025 18:23:49 +0100 Subject: [PATCH] feat: return fields event for `openFieldsDialog` --- README.md | 17 ++++++++++------- src/composables/dialog.ts | 39 +++++++++++++++++++++++++++------------ 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 1141110..20e8811 100644 --- a/README.md +++ b/README.md @@ -164,15 +164,18 @@ import { useDialog } from "kirbyuse"; const { openTextDialog, openFieldsDialog } = useDialog(); -const result = await openTextDialog("Are you sure?"); -console.log(result); // -> true or false +const isOk = await openTextDialog("Are you sure?"); +console.log(isOk); // -> true or false -await openFieldsDialog({ - info: { - type: "info", - text: "An info message", +const fields = { + email: { + type: "email", + label: "Email", }, -}); +}; + +const result = await openFieldsDialog(fields); +console.log(result); // -> { email: "..." } ``` ### useI18n diff --git a/src/composables/dialog.ts b/src/composables/dialog.ts index 13a5749..cf7c220 100644 --- a/src/composables/dialog.ts +++ b/src/composables/dialog.ts @@ -2,18 +2,18 @@ import { usePanel } from "./panel"; /** * Provides methods to open different types of dialogs. - * - * @example - * ```ts - * const { openTextDialog } = useDialog() - * - * const result = await openTextDialog("Are you sure?") - * console.log(result) // -> true or false - * ``` */ export function useDialog() { /** * Returns a promise that resolves when the dialog is closed. + * + * @example + * ```ts + * const { openTextDialog } = useDialog() + * + * const result = await openTextDialog("Are you sure?") + * console.log(result) // -> true or false + * ``` */ function openTextDialog(text: string) { let result = false; @@ -42,11 +42,26 @@ export function useDialog() { /** * Returns a promise that resolves when the dialog is closed. + * + * @example + * ```ts + * const { openFieldsDialog } = useDialog() + * + * const fields = { + * email: { + * type: "email", + * label: "Email", + * } + * } + * + * const result = await openFieldsDialog(fields) + * console.log(result) // -> { email: "..." } + * ``` */ function openFieldsDialog(fields: Record) { - let result = false; + let result: any; - return new Promise((resolve) => { + return new Promise((resolve) => { const panel = usePanel(); panel.dialog.open({ @@ -61,8 +76,8 @@ export function useDialog() { resolve(result); }, 25); }, - submit: () => { - result = true; + submit: (event: any) => { + result = event; panel.dialog.close(); }, },