Skip to content

chore: update onClick for button and link with mitosis plugin #4392

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]
};
9 changes: 2 additions & 7 deletions packages/components/configs/angular/mitosis.config.cjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const angular = require('./index.cjs');
/**
* @type {import('@builder.io/mitosis').MitosisConfig}
*/
Expand All @@ -6,12 +7,6 @@ module.exports = {
targets: ['angular'],
dest: '../../output/tmp',
options: {
angular: {
experimental: {
outputs: (_json, propName) => {
return propName === 'click' ? 'click' : undefined;
}
}
}
angular
}
};
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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nmerget should this console.log remain in there?

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]
};
19 changes: 1 addition & 18 deletions packages/components/src/components/button/button.lite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,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 @@ -51,14 +41,7 @@ export default function DBButton(props: DBButtonProps) {
value={props.value}
aria-describedby={props.describedbyid}
aria-expanded={props.ariaexpanded}
aria-pressed={props.ariapressed}
{...useTarget({
vue: {
onClick: (event: ClickEvent<HTMLButtonElement>) =>
state.handleClick(event)
},
default: {}
})}>
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