Skip to content

Commit e4f0c3b

Browse files
michaelmkrausmfranzkenmerget
authored
fix(button): Prevent duplicate click events across frameworks while preserving native behaviour (#4359)
* fix(button): prevent duplicate click events by stopping propagation * fix(button): prevent duplicate click events by stopping propagation * fix(button): prevent duplicate click events across frameworks while preserving native behavior * chore: update onClick for button and link with mitosis plugin (#4392) --------- Co-authored-by: Maximilian Franzke <[email protected]> Co-authored-by: Nicolas Merget <[email protected]>
1 parent c7afcb6 commit e4f0c3b

File tree

10 files changed

+61
-43
lines changed

10 files changed

+61
-43
lines changed
8.53 KB
Loading
4.37 KB
Loading
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
const onClickPlugin = require('../plugins/on-click.cjs');
2+
3+
/**
4+
* @type {import('@builder.io/mitosis').ToAngularOptions}
5+
*/
16
module.exports = {
2-
typescript: true,
37
attributePassing: {
48
customRef: '_ref'
59
},
6-
api: 'signals'
10+
api: 'signals',
11+
plugins: [onClickPlugin]
712
};

packages/components/configs/mitosis.config.cjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ module.exports = {
1010
files: 'src/**',
1111
targets: ['angular', 'vue', 'react', 'stencil'],
1212
dest: '../../output',
13+
commonOptions: {
14+
typescript: true
15+
},
1316
options: {
1417
react,
1518
angular,
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @type {import('@builder.io/mitosis').MitosisPlugin}
3+
*/
4+
module.exports = () => ({
5+
json: {
6+
pre: (json) => {
7+
if (['DBButton', 'DBLink'].includes(json.name)) {
8+
if (json.pluginData.target === 'vue') {
9+
json.children[0].bindings.onClick = {
10+
code: 'state.handleClick(event)',
11+
bindingType: 'expression',
12+
type: 'single'
13+
};
14+
15+
json.state = {
16+
...json.state,
17+
handleClick: {
18+
code: `function handleClick(event){if (props.onClick){props.onClick(event);}}`,
19+
type: 'method'
20+
}
21+
};
22+
} else {
23+
delete json.props.onClick;
24+
delete json.props.click;
25+
}
26+
}
27+
28+
return json;
29+
}
30+
}
31+
});
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
const onClickPlugin = require('../plugins/on-click.cjs');
2+
13
/**
24
* @type {import('@builder.io/mitosis').ToReactOptions}
35
*/
46
module.exports = {
5-
typescript: true
7+
plugins: [onClickPlugin]
68
};
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
const onClickPlugin = require('../plugins/on-click.cjs');
2+
3+
/**
4+
* @type {import('@builder.io/mitosis').ToStencilOptions}
5+
*/
16
module.exports = {
2-
typescript: true,
37
attributePassing: {
48
enabled: true,
59
customRef: '_ref'
6-
}
10+
},
11+
plugins: [onClickPlugin]
712
};
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
const onClickPlugin = require('../plugins/on-click.cjs');
2+
13
/**
24
* @type {import('@builder.io/mitosis').ToVueOptions}
35
*/
46
module.exports = {
5-
typescript: true,
6-
api: 'composition'
7+
api: 'composition',
8+
plugins: [onClickPlugin]
79
};

packages/components/src/components/button/button.lite.tsx

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ import {
22
Show,
33
useDefaultProps,
44
useMetadata,
5-
useRef,
6-
useStore
5+
useRef
76
} from '@builder.io/mitosis';
8-
import type { DBButtonProps, DBButtonState } from './model';
7+
import type { DBButtonProps } from './model';
98
import { cls, getBoolean, getBooleanAsString, getHideProp } from '../../utils';
10-
import { ClickEvent } from '../../shared/model';
119

1210
useMetadata({
1311
angular: {
@@ -19,16 +17,6 @@ useDefaultProps<DBButtonProps>({});
1917

2018
export default function DBButton(props: DBButtonProps) {
2119
const _ref = useRef<HTMLButtonElement | any>(null);
22-
// jscpd:ignore-start
23-
const state = useStore<DBButtonState>({
24-
handleClick: (event: ClickEvent<HTMLButtonElement>) => {
25-
if (props.onClick) {
26-
props.onClick(event);
27-
}
28-
}
29-
});
30-
31-
// jscpd:ignore-end
3220

3321
return (
3422
<button
@@ -50,10 +38,7 @@ export default function DBButton(props: DBButtonProps) {
5038
value={props.value}
5139
aria-describedby={props.describedbyid}
5240
aria-expanded={props.ariaexpanded}
53-
aria-pressed={props.ariapressed}
54-
onClick={(event: ClickEvent<HTMLButtonElement>) =>
55-
state.handleClick(event)
56-
}>
41+
aria-pressed={props.ariapressed}>
5742
<Show when={props.text} else={props.children}>
5843
{props.text}
5944
</Show>

packages/components/src/components/link/link.lite.tsx

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,17 @@ import {
22
Show,
33
useDefaultProps,
44
useMetadata,
5-
useRef,
6-
useStore
5+
useRef
76
} from '@builder.io/mitosis';
8-
import { DBLinkProps, DBLinkState } from './model';
7+
import { DBLinkProps } from './model';
98
import { cls, getBooleanAsString, getHideProp } from '../../utils';
10-
import { ClickEvent } from '../../shared/model';
119

1210
useMetadata({});
1311

1412
useDefaultProps<DBLinkProps>({});
1513

1614
export default function DBLink(props: DBLinkProps) {
1715
const _ref = useRef<HTMLAnchorElement | any>(null);
18-
// jscpd:ignore-start
19-
const state = useStore<DBLinkState>({
20-
handleClick: (event: ClickEvent<HTMLAnchorElement>) => {
21-
if (props.onClick) {
22-
props.onClick(event);
23-
}
24-
}
25-
});
26-
27-
// jscpd:ignore-end
2816

2917
return (
3018
<a
@@ -44,10 +32,7 @@ export default function DBLink(props: DBLinkProps) {
4432
data-size={props.size}
4533
data-hide-icon-after={getHideProp(props.showIcon ?? true)}
4634
data-variant={props.variant}
47-
data-content={props.content || 'internal'}
48-
onClick={(event: ClickEvent<HTMLAnchorElement>) =>
49-
state.handleClick(event)
50-
}>
35+
data-content={props.content || 'internal'}>
5136
<Show when={props.text} else={props.children}>
5237
{props.text}
5338
</Show>

0 commit comments

Comments
 (0)