优化link组件

master
yutent 2023-11-20 19:01:20 +08:00
parent 07901c84fb
commit 08e0089eea
1 changed files with 10 additions and 9 deletions

View File

@ -4,7 +4,7 @@
* @date 2023/03/16 17:40:50 * @date 2023/03/16 17:40:50
*/ */
import { css, html, Component, bind, unbind, nextTick } from 'wkit' import { css, html, Component, bind, nextTick } from 'wkit'
class Link extends Component { class Link extends Component {
static props = { static props = {
@ -106,6 +106,11 @@ class Link extends Component {
` `
] ]
#stop(ev) {
ev.preventDefault()
ev.stopPropagation()
}
mounted() { mounted() {
this.stamp = 0 this.stamp = 0
@ -113,7 +118,7 @@ class Link extends Component {
nextTick(_ => this.$refs.a.focus()) nextTick(_ => this.$refs.a.focus())
} }
this._clickFn = bind( bind(
this.$refs.a, this.$refs.a,
'click', 'click',
ev => { ev => {
@ -122,15 +127,11 @@ class Link extends Component {
// 除了事件冒泡之外, a标签的默认事件也要阻止 // 除了事件冒泡之外, a标签的默认事件也要阻止
if (disabled) { if (disabled) {
ev.preventDefault() return this.#stop(ev)
ev.stopPropagation()
return
} }
// 并发拦截 // 并发拦截
if (lazy > 0 && now - this.stamp < lazy) { if (lazy > 0 && now - this.stamp < lazy) {
ev.preventDefault() return this.#stop(ev)
ev.stopPropagation()
return
} }
this.stamp = now this.stamp = now
}, },
@ -141,7 +142,7 @@ class Link extends Component {
render() { render() {
return html` return html`
<a tabindex="0" ref="a" class="link" href=${this.to || 'javascript:;'}> <a tabindex="0" ref="a" class="link" href=${this.to || 'javascript:;'}>
<slot /> <slot></slot>
</a> </a>
` `
} }