Skip to content

Commit

Permalink
fix(Button): duplicate click handlers (#2213)
Browse files Browse the repository at this point in the history
Co-authored-by: Benjamin Canac <[email protected]>
  • Loading branch information
romhml and benjamincanac authored Sep 18, 2024
1 parent a8b26eb commit dd6bf56
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
7 changes: 4 additions & 3 deletions src/runtime/components/Button.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface ButtonProps extends UseComponentIconsProps, Omit<LinkProps, 'ra
block?: boolean
/** Set loading state automatically based on the `@click` promise state */
loadingAuto?: boolean
onClick?: (event: Event) => void | Promise<void>
onClick?: ((event: MouseEvent) => void | Promise<void>) | Array<((event: MouseEvent) => void | Promise<void>)>
class?: any
ui?: PartialString<typeof button.slots>
}
Expand Down Expand Up @@ -56,10 +56,11 @@ const { orientation, size: buttonSize } = useButtonGroup<ButtonProps>(props)
const loadingAutoState = ref(false)
const formLoading = inject<Ref<boolean> | undefined>(formLoadingInjectionKey, undefined)
async function onClickWrapper(event: Event) {
async function onClickWrapper(event: MouseEvent) {
loadingAutoState.value = true
const callbacks = Array.isArray(props.onClick) ? props.onClick : [props.onClick]
try {
await props.onClick?.(event)
await Promise.all(callbacks.map(fn => fn?.(event)))
} finally {
loadingAutoState.value = false
}
Expand Down
10 changes: 5 additions & 5 deletions src/runtime/components/LinkBase.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export interface LinkBaseProps {
as?: string
type?: string
disabled?: boolean
click?: (e: MouseEvent) => void
onClick?: (e: MouseEvent) => void | Promise<void>
href?: string
navigate?: (e: MouseEvent) => void
rel?: string
Expand All @@ -20,15 +20,15 @@ const props = withDefaults(defineProps<LinkBaseProps>(), {
type: 'button'
})
function onClick(e: MouseEvent) {
function onClickWrapper(e: MouseEvent) {
if (props.disabled) {
e.stopPropagation()
e.preventDefault()
return
}
if (props.click) {
props.click(e)
if (props.onClick) {
props.onClick(e)
}
if (props.href && props.navigate && !props.isExternal) {
Expand All @@ -54,7 +54,7 @@ function onClick(e: MouseEvent) {
}"
:rel="rel"
:target="target"
@click="onClick"
@click="onClickWrapper"
>
<slot />
</Primitive>
Expand Down

0 comments on commit dd6bf56

Please sign in to comment.