Skip to content

Commit 4b87f4a

Browse files
Copilotmfranzkemichaelmkrausnmerget
authored
fix(DB Switch): double event firing with Angular signals (#4779)
* Initial plan * Initial exploration and understanding of the DB Switch double event issue Co-authored-by: mfranzke <[email protected]> * Fix double event firing in DB Switch with Angular signals Co-authored-by: mfranzke <[email protected]> * Remove unrelated files from PR - keep only the actual DB Switch fix Co-authored-by: mfranzke <[email protected]> * docs: provide an example for a signal based switch * refactor: regenerated package lock file * refactor: regenerated package lock file * Changes before error encountered Co-authored-by: mfranzke <[email protected]> * Update .gitignore * refactor: updated package-lock.json file from main * fix(switch): prevent double change events by skipping props.onChange in Angular target * chore(switch): remove console.log * fix(switch): call onchange only if set * fix(switch): use old path to components * chore: add changeset --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: mfranzke <[email protected]> Co-authored-by: Maximilian Franzke <[email protected]> Co-authored-by: Michael Kraus <[email protected]> Co-authored-by: Nicolas Merget <[email protected]> Co-authored-by: Nicolas Merget <[email protected]>
1 parent da3901a commit 4b87f4a

File tree

5 files changed

+35
-9
lines changed

5 files changed

+35
-9
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"@db-ux/core-components": patch
3+
"@db-ux/ngx-core-components": patch
4+
"@db-ux/react-core-components": patch
5+
"@db-ux/v-core-components": patch
6+
"@db-ux/wc-core-components": patch
7+
---
8+
9+
- fix(DB Switch): double event firing with Angular signals

packages/components/src/components/switch/switch.lite.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,15 @@ export default function DBSwitch(props: DBSwitchProps) {
3939
const state = useStore<DBSwitchState>({
4040
_id: undefined,
4141
handleChange: (event: ChangeEvent<HTMLInputElement>) => {
42-
if (props.onChange) {
43-
props.onChange(event);
44-
}
45-
4642
useTarget({
4743
angular: () =>
4844
handleFrameworkEventAngular(state, event, 'checked'),
49-
vue: () => handleFrameworkEventVue(() => {}, event, 'checked')
45+
vue: () => handleFrameworkEventVue(() => {}, event, 'checked'),
46+
default: () => {
47+
if (props.onChange) {
48+
props.onChange(event);
49+
}
50+
}
5051
});
5152
},
5253
handleBlur: (event: InteractionEvent<HTMLInputElement>) => {

packages/components/src/utils/form-components.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ export const handleFrameworkEventAngular = (
44
event: any,
55
modelValue: string = 'value'
66
): void => {
7-
// Change event to work with reactive and template driven forms
87
component.propagateChange(event.target[modelValue]);
9-
component.writeValue(event.target[modelValue]);
108
};
119

1210
export const handleFrameworkEventVue = (

showcases/angular-showcase/src/app/components/form/form.component.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,14 @@
200200
</db-button>
201201
</fieldset>
202202
</form>
203+
204+
<p>Switch with Angular signals</p>
205+
<db-switch
206+
[checked]="checkedSignal()"
207+
(change)="handleChange($event)"
208+
label="My Switch"
209+
>
210+
</db-switch>
203211
</div>
204212
<div>
205213
<h1>Output</h1>

showcases/angular-showcase/src/app/components/form/form.component.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import {
22
Component,
33
CUSTOM_ELEMENTS_SCHEMA,
4-
NO_ERRORS_SCHEMA
4+
NO_ERRORS_SCHEMA,
5+
signal
56
} from '@angular/core';
67
import {
78
FormControl,
@@ -16,6 +17,7 @@ import {
1617
DBInput,
1718
DBRadio,
1819
DBSelect,
20+
DBSwitch,
1921
DBTabItem,
2022
DBTabList,
2123
DBTabPanel,
@@ -44,12 +46,16 @@ import { environment } from '../../../environments/environment';
4446
DBTabs,
4547
DBTabList,
4648
DBTabItem,
47-
DBTabPanel
49+
DBTabPanel,
50+
DBSwitch
4851
],
4952
standalone: true,
5053
schemas: [NO_ERRORS_SCHEMA, CUSTOM_ELEMENTS_SCHEMA]
5154
})
5255
export class FormComponent {
56+
// DB Switch with Angular signals
57+
checkedSignal = signal(false);
58+
5359
array = ['X', 'Y', 'Z'];
5460
radio = '';
5561
input = '';
@@ -150,4 +156,8 @@ export class FormComponent {
150156
})
151157
);
152158
}
159+
160+
handleChange(event: any) {
161+
this.checkedSignal.set(event.target.checked);
162+
}
153163
}

0 commit comments

Comments
 (0)