Skip to content

Commit 9163b0f

Browse files
authored
Merge pull request #999 from db-ui/998-input-react-controlled-components-doesnt-work
refactor: fix reacts (controlled components) and vues (v-model) inputs
2 parents f1c66b7 + 1e3493e commit 9163b0f

File tree

8 files changed

+14
-59
lines changed

8 files changed

+14
-59
lines changed

packages/components/scripts/post-build/components.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,7 @@ const getComponents = () => [
5555
},
5656
config: {
5757
vue: {
58-
vModel: [
59-
{ modelValue: 'checked', binding: ':checked' },
60-
{ modelValue: 'indeterminate', binding: ':indeterminate' }
61-
]
58+
vModel: [{ modelValue: 'checked', binding: ':checked' }]
6259
}
6360
}
6461
},

packages/components/scripts/post-build/vue.js

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,6 @@ const updateNestedComponents = (input, rootComponentName) => {
3232
const updateVModelBindings = (input, bindings) => {
3333
let fileContent = input;
3434

35-
// Replace internal underscore value
36-
bindings.forEach((bin) => {
37-
fileContent = fileContent.replace(
38-
`${bin.binding}="_${bin.modelValue}"`,
39-
`${bin.binding}="${bin.modelValue}"`
40-
);
41-
});
42-
4335
// Add emits to component config
4436

4537
fileContent = fileContent.replace(
@@ -52,19 +44,8 @@ const updateVModelBindings = (input, bindings) => {
5244
return fileContent
5345
.split('\n')
5446
.map((line) => {
55-
const foundBinding = bindings.find(
56-
(bin) =>
57-
line.includes(`this._${bin.modelValue} =`) &&
58-
!line.includes(
59-
`this._${bin.modelValue} = this.${bin.modelValue}`
60-
)
61-
);
62-
if (foundBinding) {
63-
const emitFunction = `this.$emit("update:${foundBinding.modelValue}", this._${foundBinding.modelValue});`;
64-
return `${line}\n${emitFunction}`;
65-
}
6647

67-
return line;
48+
return line.replace('// VUE:', '');
6849
})
6950
.join('\n');
7051
};

packages/components/src/components/checkbox/checkbox.lite.tsx

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ export default function DBCheckbox(props: DBCheckboxProps) {
2828
initialized: false,
2929
_id: DEFAULT_ID,
3030
_isValid: undefined,
31-
_value: '',
32-
_checked: false,
33-
_indeterminate: false,
3431

3532
handleChange: (event: any) => {
3633
if (props.onChange) {
@@ -41,15 +38,15 @@ export default function DBCheckbox(props: DBCheckboxProps) {
4138
props.change(event);
4239
}
4340

44-
state._checked = event.target?.checked;
45-
state._indeterminate = event.target?.indeterminate;
46-
4741
if (event.target?.validity?.valid != state._isValid) {
4842
state._isValid = event.target?.validity?.valid;
4943
if (props.validityChange) {
5044
props.validityChange(!!event.target?.validity?.valid);
5145
}
5246
}
47+
48+
// TODO: Replace this with the solution out of https://github.com/BuilderIO/mitosis/issues/833 after this has been "solved"
49+
// VUE:this.$emit("update:checked", event.target.checked);
5350
},
5451
handleBlur: (event: any) => {
5552
if (props.onBlur) {
@@ -78,10 +75,6 @@ export default function DBCheckbox(props: DBCheckboxProps) {
7875
state.initialized = true;
7976
state._id = props.id ? props.id : 'checkbox-' + uuid();
8077

81-
if (props.value) {
82-
state._value = props.value;
83-
}
84-
8578
if (props.stylePath) {
8679
state.stylePath = props.stylePath;
8780
}
@@ -123,7 +116,7 @@ export default function DBCheckbox(props: DBCheckboxProps) {
123116
name={props.name}
124117
checked={props.checked}
125118
disabled={props.disabled}
126-
value={state._value}
119+
value={props.value}
127120
aria-describedby={props.describedbyid}
128121
aria-invalid={props.invalid}
129122
data-size={props.size}

packages/components/src/components/checkbox/model.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ import {
99
GlobalState,
1010
FormProps,
1111
FormState,
12-
FormCheckProps,
13-
FormCheckState
12+
FormCheckProps
1413
} from '../../shared/model';
1514

1615
export interface DBCheckboxDefaultProps {
@@ -43,5 +42,4 @@ export type DBCheckboxState = DBCheckboxDefaultState &
4342
GlobalState &
4443
ChangeEventState &
4544
FocusEventState &
46-
FormState &
47-
FormCheckState;
45+
FormState;

packages/components/src/components/input/input.lite.tsx

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ export default function DBInput(props: DBInputProps) {
4949
_id: DEFAULT_ID,
5050
_isValid: undefined,
5151
_dataListId: DEFAULT_ID,
52-
_value: '',
5352
iconVisible: (icon?: string) => {
5453
return Boolean(icon && icon !== '_' && icon !== 'none');
5554
},
@@ -69,15 +68,15 @@ export default function DBInput(props: DBInputProps) {
6968
props.change(event);
7069
}
7170

72-
// using controlled components for react forces us to using state for value
73-
state._value = event.target.value;
74-
7571
if (event.target?.validity?.valid != state._isValid) {
7672
state._isValid = event.target?.validity?.valid;
7773
if (props.validityChange) {
7874
props.validityChange(!!event.target?.validity?.valid);
7975
}
8076
}
77+
78+
// TODO: Replace this with the solution out of https://github.com/BuilderIO/mitosis/issues/833 after this has been "solved"
79+
// VUE:this.$emit("update:value", event.target.value);
8180
},
8281
handleBlur: (event: any) => {
8382
if (props.onBlur) {
@@ -108,10 +107,6 @@ export default function DBInput(props: DBInputProps) {
108107
? props.dataListId
109108
: `datalist-${state._id}`;
110109

111-
if (props.value) {
112-
state._value = props.value;
113-
}
114-
115110
if (props.stylePath) {
116111
state.stylePath = props.stylePath;
117112
}
@@ -138,7 +133,7 @@ export default function DBInput(props: DBInputProps) {
138133
disabled={props.disabled}
139134
required={props.required}
140135
defaultValue={props.defaultValue}
141-
value={state._value}
136+
value={props.value}
142137
aria-invalid={props.invalid}
143138
maxLength={props.maxLength}
144139
minLength={props.minLength}

packages/components/src/components/radio/model.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ import {
99
GlobalState,
1010
FormProps,
1111
FormState,
12-
FormCheckProps,
13-
FormCheckState
12+
FormCheckProps
1413
} from '../../shared/model';
1514

1615
export interface DBRadioDefaultProps {
@@ -35,5 +34,4 @@ export type DBRadioState = DBRadioDefaultState &
3534
GlobalState &
3635
ChangeEventState &
3736
FocusEventState &
38-
FormState &
39-
FormCheckState;
37+
FormState;

packages/components/src/components/radio/radio.lite.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ export default function DBRadio(props: DBRadioProps) {
2727
const state = useStore<DBRadioState>({
2828
initialized: false,
2929
_id: DEFAULT_ID,
30-
_checked: false,
3130
_isValid: undefined,
3231

3332
handleChange: (event: any) => {
@@ -39,8 +38,6 @@ export default function DBRadio(props: DBRadioProps) {
3938
props.change(event);
4039
}
4140

42-
state._checked = event.target?.checked;
43-
4441
if (event.target?.validity?.valid != state._isValid) {
4542
state._isValid = event.target?.validity?.valid;
4643
if (props.validityChange) {

packages/components/src/shared/model.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,6 @@ export type FormState = {
121121
_value?: any;
122122
};
123123

124-
export type FormCheckState = {
125-
_checked: boolean;
126-
};
127-
128124
export type GlobalTextProps = {
129125
placeholder?: string;
130126
maxLength?: number;

0 commit comments

Comments
 (0)