Skip to content

fix(button): Prevent duplicate click events across frameworks while preserving native behaviour #4359

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
9 changes: 7 additions & 2 deletions packages/components/configs/angular/index.cjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
const onClickPlugin = require('../plugins/on-click.cjs');

/**
* @type {import('@builder.io/mitosis').ToAngularOptions}
*/
module.exports = {
typescript: true,
attributePassing: {
customRef: '_ref'
},
api: 'signals'
api: 'signals',
plugins: [onClickPlugin]
};
3 changes: 3 additions & 0 deletions packages/components/configs/mitosis.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ module.exports = {
files: 'src/**',
targets: ['angular', 'vue', 'react', 'stencil'],
dest: '../../output',
commonOptions: {
typescript: true
},
options: {
react,
angular,
Expand Down
33 changes: 33 additions & 0 deletions packages/components/configs/plugins/on-click.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @type {import('@builder.io/mitosis').MitosisPlugin}
*/
module.exports = () => ({
json: {
pre: (json) => {
console.log(json.name);
if (['DBButton', 'DBLink'].includes(json.name)) {
if (json.pluginData.target === 'vue') {
console.log(json.state);
json.children[0].bindings.onClick = {
code: 'state.handleClick(event)',
bindingType: 'expression',
type: 'single'
};

json.state = {
...json.state,
handleClick: {
code: `function handleClick(event){if (props.onClick){props.onClick(event);}}`,
type: 'method'
}
};
} else {
delete json.props.onClick;
delete json.props.click;
}
}

return json;
}
}
});
4 changes: 3 additions & 1 deletion packages/components/configs/react/index.cjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const onClickPlugin = require('../plugins/on-click.cjs');

/**
* @type {import('@builder.io/mitosis').ToReactOptions}
*/
module.exports = {
typescript: true
plugins: [onClickPlugin]
};
9 changes: 7 additions & 2 deletions packages/components/configs/stencil/index.cjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
const onClickPlugin = require('../plugins/on-click.cjs');

/**
* @type {import('@builder.io/mitosis').ToStencilOptions}
*/
module.exports = {
typescript: true,
attributePassing: {
enabled: true,
customRef: '_ref'
}
},
plugins: [onClickPlugin]
};
6 changes: 4 additions & 2 deletions packages/components/configs/vue/index.cjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const onClickPlugin = require('../plugins/on-click.cjs');

/**
* @type {import('@builder.io/mitosis').ToVueOptions}
*/
module.exports = {
typescript: true,
api: 'composition'
api: 'composition',
plugins: [onClickPlugin]
};
18 changes: 3 additions & 15 deletions packages/components/src/components/button/button.lite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {
useDefaultProps,
useMetadata,
useRef,
useStore
useStore,
useTarget
} from '@builder.io/mitosis';
import type { DBButtonProps, DBButtonState } from './model';
import { cls, getBoolean, getBooleanAsString, getHideProp } from '../../utils';
Expand All @@ -19,16 +20,6 @@ useDefaultProps<DBButtonProps>({});

export default function DBButton(props: DBButtonProps) {
const _ref = useRef<HTMLButtonElement | any>(null);
// jscpd:ignore-start
const state = useStore<DBButtonState>({
handleClick: (event: ClickEvent<HTMLButtonElement>) => {
if (props.onClick) {
props.onClick(event);
}
}
});

// jscpd:ignore-end

return (
<button
Expand All @@ -50,10 +41,7 @@ export default function DBButton(props: DBButtonProps) {
value={props.value}
aria-describedby={props.describedbyid}
aria-expanded={props.ariaexpanded}
aria-pressed={props.ariapressed}
onClick={(event: ClickEvent<HTMLButtonElement>) =>
state.handleClick(event)
}>
aria-pressed={props.ariapressed}>
<Show when={props.text} else={props.children}>
{props.text}
</Show>
Expand Down
15 changes: 1 addition & 14 deletions packages/components/src/components/link/link.lite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,6 @@ useDefaultProps<DBLinkProps>({});

export default function DBLink(props: DBLinkProps) {
const _ref = useRef<HTMLAnchorElement | any>(null);
// jscpd:ignore-start
const state = useStore<DBLinkState>({
handleClick: (event: ClickEvent<HTMLAnchorElement>) => {
if (props.onClick) {
props.onClick(event);
}
}
});

// jscpd:ignore-end

return (
<a
Expand All @@ -44,10 +34,7 @@ export default function DBLink(props: DBLinkProps) {
data-size={props.size}
data-hide-icon-after={getHideProp(props.showIcon ?? true)}
data-variant={props.variant}
data-content={props.content || 'internal'}
onClick={(event: ClickEvent<HTMLAnchorElement>) =>
state.handleClick(event)
}>
data-content={props.content || 'internal'}>
<Show when={props.text} else={props.children}>
{props.text}
</Show>
Expand Down
Loading