Skip to content

Commit f3d18c8

Browse files
committed
fix(Form): optimize field creation
1 parent 6050a48 commit f3d18c8

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

.changeset/quick-badgers-guess.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@cube-dev/ui-kit': minor
3+
---
4+
5+
Do not create field instance for non-exist fields in Form. Use default values from Form when creating new fields.

src/components/fields/TextInputMapper/TextInputMapper.stories.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,22 @@ const Template: StoryFn<CubeTextInputMapperProps> = ({ ...props }) => (
2828
);
2929

3030
const FormTemplate: StoryFn<CubeTextInputMapperProps> = ({ ...props }) => (
31+
<Form
32+
defaultValues={{ field: { name: 'value' } }}
33+
labelPosition="top"
34+
onSubmit={(data) => console.log('! onSubmit', data)}
35+
>
36+
<TextInputMapper
37+
name="field"
38+
label="Field Mapper"
39+
{...props}
40+
onChange={(value) => console.log('! onChange', value)}
41+
/>
42+
<Submit>Submit</Submit>
43+
</Form>
44+
);
45+
46+
const FormTemplateSync: StoryFn<CubeTextInputMapperProps> = ({ ...props }) => (
3147
<Form
3248
defaultValues={{ field: { name: 'value' } }}
3349
labelPosition="top"
@@ -66,3 +82,6 @@ WithValueAndNewMapping.play = async ({ canvasElement }) => {
6682

6783
export const WithinForm = FormTemplate.bind({});
6884
WithinForm.args = {};
85+
86+
export const WithinFormInputSync = FormTemplateSync.bind({});
87+
WithinFormInputSync.args = {};

src/components/form/Form/use-form.tsx

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export class CubeFormInstance<
3434
TFormData extends CubeFormData<T> = CubeFormData<T>,
3535
> {
3636
public forceReRender: () => void = () => {};
37-
private initialFields: PartialString<T> = {};
37+
private defaultValues: PartialString<T> = {};
3838
private fields: TFormData = {} as TFormData;
3939
public ref = {};
4040
public isSubmitting = false;
@@ -70,17 +70,17 @@ export class CubeFormInstance<
7070
newData: PartialString<T>,
7171
touched?: boolean,
7272
skipRender?: boolean,
73-
createFields = false,
7473
inputOnly = false,
7574
) => {
7675
let flag = false;
7776

77+
newData = { ...newData, ...dotize.convert(newData) };
78+
7879
Object.keys(newData).forEach((name: keyof T & string) => {
7980
let field = this.fields[name];
8081

81-
if (!field && createFields) {
82-
this.createField(name, skipRender);
83-
field = this.fields[name];
82+
if (!field) {
83+
return;
8484
}
8585

8686
if (!field || isEqual(field.value, newData[name])) {
@@ -205,35 +205,35 @@ export class CubeFormInstance<
205205
}
206206

207207
setInitialFieldsValue(values: PartialString<T>): void {
208-
this.initialFields = { ...values, ...dotize.convert(values) };
208+
this.defaultValues = { ...values, ...dotize.convert(values) };
209209
}
210210

211211
updateInitialFieldsValue(values: FieldTypes): void {
212-
this.initialFields = {
213-
...this.initialFields,
212+
this.defaultValues = {
213+
...this.defaultValues,
214214
...values,
215215
...dotize.convert(values),
216216
};
217217
}
218218

219219
resetFields(names?: (keyof T & string)[], skipRender?: boolean): void {
220220
const fieldsValue = this.getFieldsValue();
221-
const fieldNames = Object.keys({ ...fieldsValue, ...this.initialFields });
221+
const fieldNames = Object.keys({ ...fieldsValue, ...this.defaultValues });
222222
const filteredFieldNames = names
223223
? fieldNames.filter((name) => names.includes(name))
224224
: fieldNames;
225225

226226
const values = filteredFieldNames.reduce((map, name) => {
227-
if (name in this.initialFields) {
228-
map[name] = this.initialFields[name];
227+
if (name in this.defaultValues) {
228+
map[name] = this.defaultValues[name];
229229
} else {
230230
map[name] = undefined;
231231
}
232232

233233
return map;
234234
}, {});
235235

236-
this.setFieldsValue(values, false, skipRender, true);
236+
this.setFieldsValue(values, false, skipRender);
237237
}
238238

239239
async validateField<Name extends keyof T & string>(name: Name): Promise<any> {
@@ -404,6 +404,7 @@ export class CubeFormInstance<
404404
touched: false,
405405
errors: [],
406406
validationId: 0,
407+
value: this.defaultValues[name],
407408
...data,
408409
// it should be impossible to define or override status value
409410
status: data?.errors?.length ? 'invalid' : undefined,

0 commit comments

Comments
 (0)