Skip to content

Commit

Permalink
fix: Click handler on element with v-lazy-show (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
lezzhao authored Dec 17, 2024
1 parent eba661e commit c7164c3
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 5 deletions.
5 changes: 4 additions & 1 deletion playgrounds/nuxt/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import HelloWorld from './HelloWorld.vue'
const enabled = ref(false)
const role = ref('button')
function handler() {
console.log('click')

Check failure on line 8 in playgrounds/nuxt/app.vue

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
}
</script>

<template>
<button @click="enabled = !enabled">
Toggle
</button>
<div v-lazy-show="enabled">
<div v-lazy-show="enabled" @click="handler">
v-lazy-show
</div>
<div v-show="enabled">
Expand Down
9 changes: 6 additions & 3 deletions playgrounds/vite/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,27 @@ import { ref } from 'vue'
import HelloWorld from './HelloWorld.vue'
const enabled = ref(false)
function handler(msg?: string) {
console.log(msg || '111')

Check failure on line 7 in playgrounds/vite/src/App.vue

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
}
</script>

<template>
<button @click="enabled = !enabled">
Toggle
</button>
<div v-lazy-show="enabled">
<div v-lazy-show="enabled" @click="handler('v-lazy-show')">
v-lazy-show
</div>
<div v-show="enabled">
<div v-show="enabled" @click="handler('v-show')">
v-show
</div>
<div v-if="enabled">
v-if
</div>
<hr>
<div v-lazy-show="enabled">
<HelloWorld msg="v-lazy-show" />
<HelloWorld msg="v-lazy-show" :handler="handler" />
</div>
<div v-show="enabled">
<HelloWorld msg="v-show" />
Expand Down
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ export const transformLazyShow = createStructuralDirectiveTransform(

node.props
.forEach((prop) => {
if (prop.name === 'on')
return
if ('exp' in prop && prop.exp && 'content' in prop.exp && prop.exp.loc.source)
prop.exp = createSimpleExpression(prop.exp.loc.source)
})
Expand Down Expand Up @@ -115,11 +117,12 @@ export const transformLazyShow = createStructuralDirectiveTransform(
name: DIRECTIVE_NODES.SHOW,
})

const _context = Object.assign({}, context)
context.replaceNode(<TemplateChildNode><unknown>wrapNode)

return () => {
if (!node.codegenNode)
traverseNode(node, context)
traverseNode(node, _context)
}
},
)
3 changes: 3 additions & 0 deletions test/cases/event-props/input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<span v-lazy-show="foo" @click="handle" :id="id" class="111">
Hello
</span>
15 changes: 15 additions & 0 deletions test/cases/event-props/output.csr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Fragment as _Fragment, openBlock as _openBlock, createElementBlock as _createElementBlock, createCommentVNode as _createCommentVNode, createElementVNode as _createElementVNode, vShow as _vShow, withDirectives as _withDirectives } from "vue"

export function render(_ctx, _cache) {
return (_cache._lazyshow1 || _ctx.foo)
? (_cache._lazyshow1 = true, (_openBlock(), _createElementBlock(_Fragment, null, [
_withDirectives(_createElementVNode("span", {
onClick: _cache[1] || (_cache[1] = $event => (_ctx._ctx.handle && _ctx.handle(...args))),
id: _ctx.id,
class: "111"
}, " Hello ", 8 /* PROPS */, ["id"]), [
[_vShow, _ctx.foo]
])
], 64)))
: _createCommentVNode("v-show-if", true)
}
13 changes: 13 additions & 0 deletions test/cases/event-props/output.ssr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { mergeProps as _mergeProps, createVNode as _createVNode } from "vue"
import { ssrRenderAttrs as _ssrRenderAttrs } from "vue/server-renderer"

export function ssrRender(_ctx, _push, _parent, _attrs) {
if (_ctx.foo) {
_push(`<span${_ssrRenderAttrs(_mergeProps({
id: _ctx.id,
class: "111"
}, _attrs, _attrs))}> Hello </span>`)
} else {
_push(`<!---->`)
}
}

0 comments on commit c7164c3

Please sign in to comment.