Skip to content

Commit

Permalink
fix: issues with v-model
Browse files Browse the repository at this point in the history
  • Loading branch information
nmerget committed Jan 16, 2025
1 parent 83e2be6 commit ff370ed
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { DB<%= h.changeCase.pascal(name) %>State, DB<%= h.changeCase.pascal(name
import { cls } from "../../utils";
<% if(formValue!=="no"){ -%>
import {ChangeEvent, InteractionEvent} from "../../shared/model";
import { handleFrameworkEvent } from "../../utils/form-components";
import { handleFrameworkEventAngular, handleFrameworkEventVue } from "../../utils/form-components";
<% } -%>

useMetadata({});
Expand All @@ -29,8 +29,8 @@ export default function DB<%= h.changeCase.pascal(name) %>(props: DB<%= h.change
}
useTarget({
angular: () => handleFrameworkEvent(this, event, <%= formValue %>),
vue: () => handleFrameworkEvent(this, event, <%= formValue %>)
angular: () => handleFrameworkEventAngular(this, event, <%= formValue %>),
vue: () => handleFrameworkEventVue(this, event, <%= formValue %>)
});
},
handleBlur: (event: InteractionEvent<HTMLInputElement>) => {
Expand Down
6 changes: 0 additions & 6 deletions packages/components/scripts/post-build/angular.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,6 @@ ngAfterViewInit(): void {

export default (tmp?: boolean) => {
const outputFolder = `${tmp ? 'output/tmp' : 'output'}`;
// Activate vue specific event handling
replaceInFileSync({
files: `../../${outputFolder}/angular/src/utils/form-components.ts`,
from: /\/\/ ANGULAR:/g,
to: ''
});
for (const component of components) {
const componentName = component.name;
const upperComponentName = `DB${transformToUpperComponentName(component.name)}`;
Expand Down
19 changes: 13 additions & 6 deletions packages/components/scripts/post-build/vue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ export default (tmp?: boolean) => {
from: /react/g,
to: `vue`
});
// Activate vue specific event handling
replaceInFileSync({
files: `../../${outputFolder}/vue/src/utils/form-components.ts`,
from: `// VUE:`,
to: ``
});
for (const component of components) {
const componentName = component.name;
const vueFile = `../../${outputFolder}/vue/src/components/${componentName}/${componentName}.vue`;
Expand All @@ -43,6 +37,19 @@ export default (tmp?: boolean) => {
}
];

/* This is a workaround for valid/invalid Messages.
* If a valid/invalid message appears it will use the old this._value,
* so we need to overwrite this._value with the current event.target.value. */

if (['select', 'textarea', 'input'].includes(componentName)) {
replacements.push({
from: 'if (props.onInput) {',
to:
'this._value = (event.target as any).value;\n' +
'if (props.onInput) {'
});
}

if (component?.config?.vue?.vModel) {
replacements.push({
from: 'const props =',
Expand Down
10 changes: 7 additions & 3 deletions packages/components/src/components/checkbox/checkbox.lite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ import {
DEFAULT_VALID_MESSAGE_ID_SUFFIX
} from '../../shared/constants';
import { ChangeEvent, InteractionEvent } from '../../shared/model';
import { handleFrameworkEvent } from '../../utils/form-components';
import {
handleFrameworkEventAngular,
handleFrameworkEventVue
} from '../../utils/form-components';
import DBInfotext from '../infotext/infotext.lite';

useMetadata({
Expand Down Expand Up @@ -57,8 +60,9 @@ export default function DBCheckbox(props: DBCheckboxProps) {
}

useTarget({
angular: () => handleFrameworkEvent(this, event, 'checked'),
vue: () => handleFrameworkEvent(this, event, 'checked')
angular: () =>
handleFrameworkEventAngular(this, event, 'checked'),
vue: () => handleFrameworkEventVue(this, event, 'checked')
});

/* For a11y reasons we need to map the correct message with the checkbox */
Expand Down
9 changes: 6 additions & 3 deletions packages/components/src/components/input/input.lite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ import {
ValueLabelType
} from '../../shared/model';
import DBInfotext from '../infotext/infotext.lite';
import { handleFrameworkEvent } from '../../utils/form-components';
import {
handleFrameworkEventVue,
handleFrameworkEventAngular
} from '../../utils/form-components';

useMetadata({
angular: {
Expand Down Expand Up @@ -77,8 +80,8 @@ export default function DBInput(props: DBInputProps) {
}

useTarget({
angular: () => handleFrameworkEvent(this, event),
vue: () => handleFrameworkEvent(this, event)
angular: () => handleFrameworkEventAngular(this, event),
vue: () => handleFrameworkEventVue(this, event)
});

/* For a11y reasons we need to map the correct message with the input */
Expand Down
10 changes: 7 additions & 3 deletions packages/components/src/components/radio/radio.lite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import {
import { DBRadioProps, DBRadioState } from './model';
import { cls, getHideProp, uuid } from '../../utils';
import { ChangeEvent, InteractionEvent } from '../../shared/model';
import { handleFrameworkEvent } from '../../utils/form-components';
import {
handleFrameworkEventAngular,
handleFrameworkEventVue
} from '../../utils/form-components';

useMetadata({
angular: {
Expand All @@ -36,8 +39,9 @@ export default function DBRadio(props: DBRadioProps) {
}

useTarget({
angular: () => handleFrameworkEvent(this, event, 'checked'),
vue: () => handleFrameworkEvent(this, event, 'checked')
angular: () =>
handleFrameworkEventAngular(this, event, 'checked'),
vue: () => handleFrameworkEventVue(this, event, 'checked')
});
},
handleBlur: (event: InteractionEvent<HTMLInputElement>) => {
Expand Down
9 changes: 6 additions & 3 deletions packages/components/src/components/select/select.lite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ import {
InputEvent,
InteractionEvent
} from '../../shared/model';
import { handleFrameworkEvent } from '../../utils/form-components';
import {
handleFrameworkEventAngular,
handleFrameworkEventVue
} from '../../utils/form-components';

useMetadata({
angular: {
Expand Down Expand Up @@ -81,8 +84,8 @@ export default function DBSelect(props: DBSelectProps) {
}

useTarget({
angular: () => handleFrameworkEvent(this, event),
vue: () => handleFrameworkEvent(this, event)
angular: () => handleFrameworkEventAngular(this, event),
vue: () => handleFrameworkEventVue(this, event)
});

/* For a11y reasons we need to map the correct message with the select */
Expand Down
10 changes: 7 additions & 3 deletions packages/components/src/components/switch/switch.lite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import {
import { DBSwitchProps, DBSwitchState } from './model';
import { cls, getHideProp, uuid } from '../../utils';
import { ChangeEvent, InteractionEvent } from '../../shared/model';
import { handleFrameworkEvent } from '../../utils/form-components';
import {
handleFrameworkEventAngular,
handleFrameworkEventVue
} from '../../utils/form-components';

useMetadata({
angular: {
Expand Down Expand Up @@ -40,8 +43,9 @@ export default function DBSwitch(props: DBSwitchProps) {
state._checked = (event.target as any)?.['checked'];

useTarget({
angular: () => handleFrameworkEvent(this, event, 'checked'),
vue: () => handleFrameworkEvent(this, event, 'checked')
angular: () =>
handleFrameworkEventAngular(this, event, 'checked'),
vue: () => handleFrameworkEventVue(this, event, 'checked')
});
},
handleBlur: (event: InteractionEvent<HTMLInputElement>) => {
Expand Down
10 changes: 7 additions & 3 deletions packages/components/src/components/tab-item/tab-item.lite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import {
import type { DBTabItemProps, DBTabItemState } from './model';
import { cls, getBooleanAsString, getHideProp } from '../../utils';
import { ChangeEvent } from '../../shared/model';
import { handleFrameworkEvent } from '../../utils/form-components';
import {
handleFrameworkEventAngular,
handleFrameworkEventVue
} from '../../utils/form-components';

useMetadata({
angular: {
Expand Down Expand Up @@ -48,8 +51,9 @@ export default function DBTabItem(props: DBTabItemProps) {
});

useTarget({
angular: () => handleFrameworkEvent(this, event, 'checked'),
vue: () => handleFrameworkEvent(this, event, 'checked')
angular: () =>
handleFrameworkEventAngular(this, event, 'checked'),
vue: () => handleFrameworkEventVue(this, event, 'checked')
});
}
});
Expand Down
9 changes: 6 additions & 3 deletions packages/components/src/components/textarea/textarea.lite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ import {
DEFAULT_VALID_MESSAGE_ID_SUFFIX
} from '../../shared/constants';
import { ChangeEvent, InputEvent, InteractionEvent } from '../../shared/model';
import { handleFrameworkEvent } from '../../utils/form-components';
import {
handleFrameworkEventAngular,
handleFrameworkEventVue
} from '../../utils/form-components';

useMetadata({
angular: {
Expand Down Expand Up @@ -68,8 +71,8 @@ export default function DBTextarea(props: DBTextareaProps) {
props.change(event);
}
useTarget({
angular: () => handleFrameworkEvent(this, event),
vue: () => handleFrameworkEvent(this, event)
angular: () => handleFrameworkEventAngular(this, event),
vue: () => handleFrameworkEventVue(this, event)
});

/* For a11y reasons we need to map the correct message with the textarea */
Expand Down
18 changes: 12 additions & 6 deletions packages/components/src/utils/form-components.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
export const handleFrameworkEvent = (
/* eslint-disable @typescript-eslint/no-explicit-any */
export const handleFrameworkEventAngular = (
component: any,
event: any,
modelValue: string = 'value'
): void => {
// TODO: Replace this with the solution out of https://github.com/BuilderIO/mitosis/issues/833 after this has been "solved"
// VUE:component.emit(`update:${modelValue}`, event.target[modelValue]);
// Change event to work with reactive and template driven forms
// ANGULAR: component.propagateChange(event.target[modelValue]);
// ANGULAR: component.writeValue(event.target[modelValue]);
component.propagateChange(event.target[modelValue]);
component.writeValue(event.target[modelValue]);
};

export default { handleFrameworkEvent };
export const handleFrameworkEventVue = (
component: any,
event: any,
modelValue: string = 'value'
): void => {
// TODO: Replace this with the solution out of https://github.com/BuilderIO/mitosis/issues/833 after this has been "solved"
component.emit(`update:${modelValue}`, event.target[modelValue]);
};

0 comments on commit ff370ed

Please sign in to comment.