Skip to content

Commit

Permalink
feat: add showByDefault option to useTemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
hunterliu1003 committed Mar 17, 2024
1 parent 829087c commit 962c168
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
17 changes: 17 additions & 0 deletions cypress/components/ShowByDefault.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<script setup lang="ts">
import { h } from 'vue'
import { TemplateProvider, useTemplate } from '../../src/index'
const props = defineProps<{
showByDefault: boolean
}>()
const text = 'Hello World!'
useTemplate({ component: () => h('div', text) }, {
showByDefault: props.showByDefault,
})
</script>

<template>
<TemplateProvider />
</template>
23 changes: 23 additions & 0 deletions cypress/components/index.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { defineAsyncComponent, h, reactive, ref } from 'vue'
import { TemplateProvider, defineTemplate, useTemplate } from '../../src/index'
import DialogConfirmPreview from './DialogConfirmPreview.vue'
import HideOnUnmounted from './HideOnUnmounted.vue'
import ShowByDefault from './ShowByDefault.vue'

describe('test useTemplate()', () => {
it('hello world - createTemplatePlugin', () => {
Expand Down Expand Up @@ -196,6 +197,28 @@ describe('test useTemplate()', () => {
})

describe('test useTemplate() options', () => {
it('showByDefault: false', () => {
cy.contains('Hello World!').should('not.exist')

cy.mount(ShowByDefault, {
props: {
showByDefault: false,
},
}).as('container')

cy.contains('Hello World!').should('not.exist')
})
it('showByDefault: true', () => {
cy.contains('Hello World!').should('not.exist')

cy.mount(ShowByDefault, {
props: {
showByDefault: true,
},
}).as('container')

cy.contains('Hello World!').should('exist')
})
it('hideOnUnmounted: false', () => {
cy.mount(HideOnUnmounted, {
props: {
Expand Down
5 changes: 4 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ export interface Provider {

export type UseTemplate = <T extends Component>(
template: Template<T>,
options?: { hideOnUnmounted?: boolean }
options?: {
showByDefault?: boolean
hideOnUnmounted?: boolean
}
) => {
show: () => Promise<void>
hide: () => Promise<void>
Expand Down
4 changes: 4 additions & 0 deletions src/useTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export function defineTemplate<T extends Component>(template: Template<T>) {
export const useTemplate: UseTemplate = <T extends Component>(
template: Template<T>,
options: Parameters<UseTemplate>[1] = {
showByDefault: false,
hideOnUnmounted: true,
},
): ReturnType<UseTemplate> => {
Expand All @@ -36,6 +37,9 @@ export const useTemplate: UseTemplate = <T extends Component>(
if (_nextTick)
checkError()

if (options?.showByDefault)
show()

if (options?.hideOnUnmounted)
tryOnUnmounted(hide)

Expand Down

0 comments on commit 962c168

Please sign in to comment.