-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:
useDialog
composable for dialog management
- Loading branch information
1 parent
cc1b23e
commit 41e26a5
Showing
4 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { usePanel } from "kirbyuse"; | ||
|
||
/** | ||
* 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. | ||
*/ | ||
function openTextDialog(text: string) { | ||
let result = false; | ||
|
||
return new Promise<boolean>((resolve) => { | ||
const panel = usePanel(); | ||
|
||
panel.dialog.open({ | ||
component: "k-text-dialog", | ||
props: { text }, | ||
on: { | ||
// Close event will always be triggered, even on submit | ||
close: () => { | ||
setTimeout(() => { | ||
resolve(result); | ||
}, 25); | ||
}, | ||
submit: () => { | ||
result = true; | ||
panel.dialog.close(); | ||
}, | ||
}, | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* Returns a promise that resolves when the dialog is closed. | ||
*/ | ||
function openFieldsDialog(fields: Record<string, any>) { | ||
let result = false; | ||
|
||
return new Promise<boolean>((resolve) => { | ||
const panel = usePanel(); | ||
|
||
panel.dialog.open({ | ||
component: "k-form-dialog", | ||
props: { | ||
fields, | ||
}, | ||
on: { | ||
// Close event will always be triggered, even on submit | ||
close: () => { | ||
setTimeout(() => { | ||
resolve(result); | ||
}, 25); | ||
}, | ||
submit: () => { | ||
result = true; | ||
panel.dialog.close(); | ||
}, | ||
}, | ||
}); | ||
}); | ||
} | ||
|
||
return { | ||
openTextDialog, | ||
openFieldsDialog, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters