Skip to content

Commit 57aeb60

Browse files
nmergetgithub-actions[bot]mfranzke
authored
refactor: use button type for react and vue based on click listener (#4490)
* refactor: use button type for react and vue based on click listener * chore: add migration documentation * auto update snapshots (#4492) Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Maximilian Franzke <[email protected]>
1 parent a0c382e commit 57aeb60

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed
-15.6 KB
Loading

docs/migration/v2.x.x-to-v3.0.0.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
## Migration Beta (1.x.x) ➡ 2.0.0
22

3+
## db-button/DBButton type
4+
5+
We changed the behaviour of the `type` property in the `db-button`/`DBButton` component.
6+
Those changes will only affect React and Vue users.
7+
If you use `onClick` or `@click` you will get `type=button` as default, otherwise it will be `type=submit`.
8+
You can still set the `type` property manually, to overwrite this.
9+
Angular and Web Components users will not be affected by this change, the default will be `button`, because click event listeners can't be undefined in the frameworks.
10+
Please provide the correct `type` property in your code anyhow as a best practise.
11+
312
## icon-before/-after
413

514
We renamed the `data-icon-after` and `data-icon-before` properties to `data-icon-trailing` and `data-icon-leading`,

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import {
22
Show,
33
useDefaultProps,
44
useMetadata,
5-
useRef
5+
useRef,
6+
useStore
67
} from '@builder.io/mitosis';
78
import { cls, getBoolean, getBooleanAsString, getHideProp } from '../../utils';
8-
import type { DBButtonProps } from './model';
9+
import type { DBButtonProps, DBButtonState } from './model';
910

1011
useMetadata({
1112
angular: {
@@ -18,12 +19,23 @@ useDefaultProps<DBButtonProps>({});
1819
export default function DBButton(props: DBButtonProps) {
1920
const _ref = useRef<HTMLButtonElement | any>(null);
2021

22+
const state = useStore<DBButtonState>({
23+
getButtonType: () => {
24+
if (props.type) {
25+
return props.type;
26+
} else if (props.onClick) {
27+
return 'button';
28+
}
29+
return 'submit';
30+
}
31+
});
32+
2133
return (
2234
<button
2335
ref={_ref}
2436
id={props.id}
2537
class={cls('db-button', props.className)}
26-
type={props.type || 'button'}
38+
type={state.getButtonType()}
2739
disabled={getBoolean(props.disabled, 'disabled')}
2840
data-icon={props.iconLeading ?? props.icon}
2941
data-hide-icon={getHideProp(

packages/components/src/components/button/model.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {
22
ClickEventProps,
3-
ClickEventState,
43
GlobalProps,
54
GlobalState,
65
IconLeadingProps,
@@ -83,8 +82,8 @@ export type DBButtonProps = DBButtonDefaultProps &
8382
IconLeadingProps &
8483
IconTrailingProps;
8584

86-
export type DBButtonDefaultState = {};
85+
export type DBButtonDefaultState = {
86+
getButtonType: () => ButtonTypeType;
87+
};
8788

88-
export type DBButtonState = DBButtonDefaultState &
89-
GlobalState &
90-
ClickEventState<HTMLButtonElement>;
89+
export type DBButtonState = DBButtonDefaultState & GlobalState;

0 commit comments

Comments
 (0)