Skip to content

Commit

Permalink
feat: return fields event for openFieldsDialog
Browse files Browse the repository at this point in the history
  • Loading branch information
johannschopplich committed Jan 17, 2025
1 parent d513379 commit 0fc403c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 27 additions & 12 deletions src/composables/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<string, any>) {
let result = false;
let result: any;

return new Promise<boolean>((resolve) => {
return new Promise<any>((resolve) => {
const panel = usePanel();

panel.dialog.open({
Expand All @@ -61,8 +76,8 @@ export function useDialog() {
resolve(result);
}, 25);
},
submit: () => {
result = true;
submit: (event: any) => {
result = event;
panel.dialog.close();
},
},
Expand Down

0 comments on commit 0fc403c

Please sign in to comment.