feat(core): support v-show directive (#310)
This commit is contained in:
128
packages/runtime-dom/__tests__/directives/vShow.spec.ts
Normal file
128
packages/runtime-dom/__tests__/directives/vShow.spec.ts
Normal file
@@ -0,0 +1,128 @@
|
||||
import {
|
||||
withDirectives,
|
||||
createComponent,
|
||||
h,
|
||||
nextTick,
|
||||
VNode
|
||||
} from '@vue/runtime-core'
|
||||
import { createApp, vShow } from '@vue/runtime-dom'
|
||||
|
||||
const withVShow = (node: VNode, exp: any) =>
|
||||
withDirectives(node, [[vShow, exp]])
|
||||
|
||||
let app: any, root: any
|
||||
|
||||
beforeEach(() => {
|
||||
app = createApp()
|
||||
root = document.createElement('div') as any
|
||||
})
|
||||
|
||||
describe('runtime-dom: v-show directive', () => {
|
||||
test('should check show value is truthy', async () => {
|
||||
const component = createComponent({
|
||||
data() {
|
||||
return { value: true }
|
||||
},
|
||||
render() {
|
||||
return [withVShow(h('div'), this.value)]
|
||||
}
|
||||
})
|
||||
app.mount(component, root)
|
||||
|
||||
const $div = root.querySelector('div')
|
||||
|
||||
expect($div.style.display).toEqual('')
|
||||
})
|
||||
|
||||
test('should check show value is falsy', async () => {
|
||||
const component = createComponent({
|
||||
data() {
|
||||
return { value: false }
|
||||
},
|
||||
render() {
|
||||
return [withVShow(h('div'), this.value)]
|
||||
}
|
||||
})
|
||||
app.mount(component, root)
|
||||
|
||||
const $div = root.querySelector('div')
|
||||
|
||||
expect($div.style.display).toEqual('none')
|
||||
})
|
||||
|
||||
it('should update show value changed', async () => {
|
||||
const component = createComponent({
|
||||
data() {
|
||||
return { value: true }
|
||||
},
|
||||
render() {
|
||||
return [withVShow(h('div'), this.value)]
|
||||
}
|
||||
})
|
||||
app.mount(component, root)
|
||||
|
||||
const $div = root.querySelector('div')
|
||||
const data = root._vnode.component.data
|
||||
|
||||
expect($div.style.display).toEqual('')
|
||||
|
||||
data.value = false
|
||||
await nextTick()
|
||||
expect($div.style.display).toEqual('none')
|
||||
|
||||
data.value = {}
|
||||
await nextTick()
|
||||
expect($div.style.display).toEqual('')
|
||||
|
||||
data.value = 0
|
||||
await nextTick()
|
||||
expect($div.style.display).toEqual('none')
|
||||
|
||||
data.value = []
|
||||
await nextTick()
|
||||
expect($div.style.display).toEqual('')
|
||||
|
||||
data.value = null
|
||||
await nextTick()
|
||||
expect($div.style.display).toEqual('none')
|
||||
|
||||
data.value = '0'
|
||||
await nextTick()
|
||||
expect($div.style.display).toEqual('')
|
||||
|
||||
data.value = undefined
|
||||
await nextTick()
|
||||
expect($div.style.display).toEqual('none')
|
||||
|
||||
data.value = 1
|
||||
await nextTick()
|
||||
expect($div.style.display).toEqual('')
|
||||
})
|
||||
|
||||
test('should respect display value in style attribute', async () => {
|
||||
const component = createComponent({
|
||||
data() {
|
||||
return { value: true }
|
||||
},
|
||||
render() {
|
||||
return [
|
||||
withVShow(h('div', { style: { display: 'block' } }), this.value)
|
||||
]
|
||||
}
|
||||
})
|
||||
app.mount(component, root)
|
||||
|
||||
const $div = root.querySelector('div')
|
||||
const data = root._vnode.component.data
|
||||
|
||||
expect($div.style.display).toEqual('block')
|
||||
|
||||
data.value = false
|
||||
await nextTick()
|
||||
expect($div.style.display).toEqual('none')
|
||||
|
||||
data.value = true
|
||||
await nextTick()
|
||||
expect($div.style.display).toEqual('block')
|
||||
})
|
||||
})
|
||||
45
packages/runtime-dom/src/directives/vShow.ts
Normal file
45
packages/runtime-dom/src/directives/vShow.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { ObjectDirective } from '@vue/runtime-core'
|
||||
|
||||
interface VShowElement extends HTMLElement {
|
||||
// _vod = vue original display
|
||||
_vod: string
|
||||
}
|
||||
|
||||
export const vShow: ObjectDirective<VShowElement> = {
|
||||
beforeMount(el, { value }, { transition }) {
|
||||
el._vod = el.style.display === 'none' ? '' : el.style.display
|
||||
if (transition && value) {
|
||||
transition.beforeEnter(el)
|
||||
} else {
|
||||
setDisplay(el, value)
|
||||
}
|
||||
},
|
||||
mounted(el, { value }, { transition }) {
|
||||
if (transition && value) {
|
||||
transition.enter(el)
|
||||
}
|
||||
},
|
||||
updated(el, { value, oldValue }, { transition }) {
|
||||
if (!value === !oldValue) return
|
||||
if (transition) {
|
||||
if (value) {
|
||||
transition.beforeEnter(el)
|
||||
setDisplay(el, true)
|
||||
transition.enter(el)
|
||||
} else {
|
||||
transition.leave(el, () => {
|
||||
setDisplay(el, false)
|
||||
})
|
||||
}
|
||||
} else {
|
||||
setDisplay(el, value)
|
||||
}
|
||||
},
|
||||
beforeUnmount(el) {
|
||||
setDisplay(el, true)
|
||||
}
|
||||
}
|
||||
|
||||
function setDisplay(el: VShowElement, value: unknown): void {
|
||||
el.style.display = value ? el._vod : 'none'
|
||||
}
|
||||
@@ -68,6 +68,8 @@ export { withModifiers, withKeys } from './directives/vOn'
|
||||
// DOM-only components
|
||||
export { Transition, TransitionProps } from './components/Transition'
|
||||
|
||||
export { vShow } from './directives/vShow'
|
||||
|
||||
// re-export everything from core
|
||||
// h, Component, reactivity API, nextTick, flags & types
|
||||
export * from '@vue/runtime-core'
|
||||
|
||||
Reference in New Issue
Block a user