-
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.
refs: ["VUE3-BP-T-5"]
- Loading branch information
Showing
13 changed files
with
1,256 additions
and
1 deletion.
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,182 @@ | ||
<template> | ||
<q-dialog | ||
ref="dialog" | ||
v-model="opened" | ||
:persistent="persistent" | ||
@before-show="onBeforeShow" | ||
@show="onShow" | ||
@before-hide="onBeforeHide" | ||
@hide="onHide" | ||
> | ||
<slot></slot> | ||
</q-dialog> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import type { Ref, SetupContext, UnwrapNestedRefs } from 'vue' | ||
import { defineComponent, ref } from 'vue' | ||
import { extensibleMethod, isImplemented } from 'js-common-lib' | ||
import type { QDialog } from 'quasar' | ||
//========================================================================== | ||
// | ||
// Base | ||
// | ||
//========================================================================== | ||
namespace BasePromiseDialog { | ||
export interface Props<RESULT> { | ||
readonly defaultResult: RESULT | ||
readonly persistent: boolean | ||
} | ||
export type Features<PARAMS, RESULT> = UnwrapNestedRefs<WrapFeatures<PARAMS, RESULT>> | ||
export interface WrapFeatures<PARAMS, RESULT> { | ||
readonly opened: Ref<boolean> | ||
open(params: PARAMS): Promise<RESULT> | ||
close(result: RESULT): void | ||
} | ||
export const props = { | ||
defaultResult: { default: undefined }, | ||
persistent: { type: Boolean, default: false }, | ||
} | ||
export const emits = { | ||
'before-show': null, | ||
show: null, | ||
'before-hide': null, | ||
hide: null, | ||
} | ||
export function setup<PARAMS, RESULT>( | ||
props: PromiseDialog.Props<RESULT>, | ||
ctx: SetupContext<typeof emits> | ||
) { | ||
//---------------------------------------------------------------------- | ||
// | ||
// Variables | ||
// | ||
//---------------------------------------------------------------------- | ||
const dialog = ref<QDialog>() | ||
const opened = ref(false) | ||
const closeResolve: Ref<((value: RESULT) => void) | undefined> = ref(undefined) | ||
const closeResult: Ref<RESULT> = ref(undefined as any) | ||
//---------------------------------------------------------------------- | ||
// | ||
// Methods | ||
// | ||
//---------------------------------------------------------------------- | ||
const open = extensibleMethod<PromiseDialog<PARAMS, RESULT>['open']>(() => { | ||
closeResult.value = props.defaultResult | ||
return new Promise<RESULT>(resolve => { | ||
closeResolve.value = resolve | ||
opened.value = true | ||
}) | ||
}) | ||
const close = extensibleMethod<PromiseDialog<PARAMS, RESULT>['close']>(value => { | ||
closeResult.value = value | ||
opened.value = false | ||
}) | ||
//---------------------------------------------------------------------- | ||
// | ||
// Events | ||
// | ||
//---------------------------------------------------------------------- | ||
const onBeforeShow = extensibleMethod(() => { | ||
ctx.emit('before-show') | ||
}) | ||
const onShow = extensibleMethod(() => { | ||
ctx.emit('show') | ||
}) | ||
const onBeforeHide = extensibleMethod(() => { | ||
ctx.emit('before-hide') | ||
}) | ||
const onHide = extensibleMethod(() => { | ||
closeResolve.value?.(closeResult.value) | ||
closeResolve.value = undefined | ||
ctx.emit('hide') | ||
}) | ||
//---------------------------------------------------------------------- | ||
// | ||
// Result | ||
// | ||
//---------------------------------------------------------------------- | ||
const result = { | ||
dialog, | ||
opened, | ||
closeResolve, | ||
closeResult, | ||
open, | ||
close, | ||
onBeforeShow, | ||
onShow, | ||
onBeforeHide, | ||
onHide, | ||
} | ||
return isImplemented<PromiseDialog.WrapFeatures<PARAMS, RESULT>, typeof result>(result) | ||
} | ||
} | ||
//========================================================================== | ||
// | ||
// Interfaces | ||
// | ||
//========================================================================== | ||
type PromiseDialog<PARAMS = void, RESULT = void> = PromiseDialog.Props<RESULT> & | ||
PromiseDialog.Features<PARAMS, RESULT> | ||
namespace PromiseDialog { | ||
export type Props<RESULT> = BasePromiseDialog.Props<RESULT> | ||
export type Features<PARAMS, RESULT> = UnwrapNestedRefs<WrapFeatures<PARAMS, RESULT>> | ||
export interface WrapFeatures<PARAMS, RESULT> | ||
extends BasePromiseDialog.WrapFeatures<PARAMS, RESULT> {} | ||
} | ||
//========================================================================== | ||
// | ||
// Implementation | ||
// | ||
//========================================================================== | ||
const PromiseDialog = defineComponent({ | ||
name: 'PromiseDialog', | ||
props: { ...BasePromiseDialog.props }, | ||
emits: { ...BasePromiseDialog.emits }, | ||
setup(props: PromiseDialog.Props<any>, ctx) { | ||
const base = BasePromiseDialog.setup(props, ctx) | ||
return { ...base } | ||
}, | ||
}) | ||
//========================================================================== | ||
// | ||
// Export | ||
// | ||
//========================================================================== | ||
export default PromiseDialog | ||
export { BasePromiseDialog } | ||
</script> |
Oops, something went wrong.