Skip to content

Commit 7ed67ec

Browse files
Duplicate ids and unregistering of fields, fields groups and fields a… (#63)
* Duplicate ids and unregistering of fields, fields groups and fields arrays. Duplicate field, fields groups and fields arrays id checks added. Fields groups and fields array unregistering on unmounting added. * withMutations -> merge in field, fields group and fields array registration.
1 parent d6312bf commit 7ed67ec

File tree

4 files changed

+60
-27
lines changed

4 files changed

+60
-27
lines changed

packages/simplr-forms/src/abstractions/base-fields-array.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,12 @@ export class BaseFieldsArray<TProps extends FieldsArrayProps,
5050
componentWillMount() {
5151
const idBase = `${this.props.name}[${this.props.index}]`;
5252
this.FieldsArrayId = this.FormStore.GetFieldsGroupId(idBase, this.context.FieldsGroupId);
53-
if (this.FormStore.GetState().FieldsGroups.has(this.FieldsArrayId)) {
54-
throw new Error(`simplr-forms: FieldsArray '${this.FieldsArrayId}' already exists in form '${this.FormId}'.`);
55-
}
56-
5753
this.FormStore.RegisterFieldsArray(this.FieldsArrayId, this.props.name, this.props.index, this.context.FieldsGroupId);
5854
}
55+
56+
componentWillUnmount(): void {
57+
if (this.FormStore != null && this.props.destroyOnUnmount) {
58+
this.FormStore.UnregisterFieldsArray(this.FieldsArrayId);
59+
}
60+
}
5961
}

packages/simplr-forms/src/abstractions/base-fields-group.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,12 @@ export class BaseFieldsGroup<TProps extends FieldsGroupProps,
4949

5050
componentWillMount() {
5151
this.FieldsGroupId = this.FormStore.GetFieldsGroupId(this.props.name, this.context.FieldsGroupId);
52-
if (this.FormStore.GetState().FieldsGroups.has(this.FieldsGroupId)) {
53-
throw new Error(`simplr-forms: FieldsGroup '${this.FieldsGroupId}' already exists in form '${this.FormId}'.`);
54-
}
55-
5652
this.FormStore.RegisterFieldsGroup(this.FieldsGroupId, this.props.name, this.context.FieldsGroupId);
5753
}
54+
55+
componentWillUnmount(): void {
56+
if (this.FormStore != null && this.props.destroyOnUnmount) {
57+
this.FormStore.UnregisterFieldsGroup(this.FieldsGroupId);
58+
}
59+
}
5860
}

packages/simplr-forms/src/abstractions/core-field.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,6 @@ export abstract class CoreField<TProps extends CoreFieldProps, TState extends Co
286286
* Registers a field in FormStore or throws if the field was already registered
287287
*/
288288
private registerFieldInFormStore(): void {
289-
if (this.FormStore.HasField(this.FieldId)) {
290-
throw new Error(`simplr-forms: Duplicate field id '${this.FieldId}'`);
291-
}
292-
293289
const defaultValue = this.ProcessValueBeforeStore(this.RawDefaultValue);
294290
const initialValue = this.ProcessValueBeforeStore(this.RawInitialValue);
295291
const value = this.ProcessValueBeforeStore(this.RawValue);

packages/simplr-forms/src/stores/form-store.ts

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ export class FormStore extends ActionEmitter {
103103
props?: FieldProps,
104104
fieldsGroupId?: string
105105
): void {
106+
if (this.State.Fields.has(fieldId) ||
107+
this.State.FieldsGroups.has(fieldId)) {
108+
throw new Error(`simplr-forms: Field '${fieldId}' already exists in form '${this.FormId}.`);
109+
}
110+
106111
// Construct field state
107112
const fieldState: FieldStoreState = {
108113
Name: name,
@@ -154,40 +159,54 @@ export class FormStore extends ActionEmitter {
154159
}
155160

156161
// Add field into form store state
157-
this.State = this.State.withMutations(state => {
158-
state.Fields = state.Fields.set(fieldId, recordify<FieldStoreState, FieldStoreStateRecord>(fieldState));
159-
});
162+
this.State = this.State.merge({
163+
Fields: this.State.Fields.set(fieldId, recordify<FieldStoreState, FieldStoreStateRecord>(fieldState))
164+
} as FormStoreStateRecord);
160165

161166
this.emit(new Actions.FieldRegistered(this.FormId, fieldId));
162167
}
163168

164-
public RegisterFieldsGroup(id: string, name: string, parentId?: string): void {
169+
public RegisterFieldsGroup(fieldsGroupId: string, name: string, parentId?: string): void {
170+
if (this.State.Fields.has(fieldsGroupId) ||
171+
this.State.FieldsGroups.has(fieldsGroupId)) {
172+
throw new Error(`simplr-forms: FieldsGroup '${fieldsGroupId}' already exists in form '${this.FormId}.`);
173+
}
174+
165175
const fgState: FieldsGroupStoreState = {
166176
Name: name,
167177
Parent: parentId
168178
};
169179

170180
const fgStateRecord = recordify<FieldsGroupStoreState, FieldsGroupStoreStateRecord>(fgState);
171-
this.State = this.State.withMutations(state => {
172-
state.FieldsGroups = state.FieldsGroups.set(id, fgStateRecord);
173-
});
174181

175-
this.emit(new Actions.FieldsGroupRegistered(this.FormId, id));
182+
// Add fields group into form store state
183+
this.State = this.State.merge({
184+
FieldsGroups: this.State.FieldsGroups.set(fieldsGroupId, fgStateRecord)
185+
} as FormStoreStateRecord);
186+
187+
this.emit(new Actions.FieldsGroupRegistered(this.FormId, fieldsGroupId));
176188
}
177189

178-
public RegisterFieldsArray(id: string, name: string, index: number, parentId?: string): void {
179-
const fgState: FieldsGroupStoreState = {
190+
public RegisterFieldsArray(fieldsArrayId: string, name: string, index: number, parentId?: string): void {
191+
if (this.State.Fields.has(fieldsArrayId) ||
192+
this.State.FieldsGroups.has(fieldsArrayId)) {
193+
throw new Error(`simplr-forms: FieldsArray '${fieldsArrayId}' already exists in form '${this.FormId}.`);
194+
}
195+
196+
const faState: FieldsGroupStoreState = {
180197
Name: name,
181198
ArrayName: name,
182199
Parent: parentId
183200
};
184201

185-
const fgStateRecord = recordify<FieldsGroupStoreState, FieldsGroupStoreStateRecord>(fgState);
186-
this.State = this.State.withMutations(state => {
187-
state.FieldsGroups = state.FieldsGroups.set(id, fgStateRecord);
188-
});
202+
const faStateRecord = recordify<FieldsGroupStoreState, FieldsGroupStoreStateRecord>(faState);
189203

190-
this.emit(new Actions.FieldsArrayRegistered(this.FormId, id));
204+
// Add fields array into form store state
205+
this.State = this.State.merge({
206+
FieldsGroups: this.State.FieldsGroups.set(fieldsArrayId, faStateRecord)
207+
} as FormStoreStateRecord);
208+
209+
this.emit(new Actions.FieldsArrayRegistered(this.FormId, fieldsArrayId));
191210
}
192211

193212
public UnregisterField(fieldId: string): void {
@@ -197,6 +216,20 @@ export class FormStore extends ActionEmitter {
197216
});
198217
}
199218

219+
public UnregisterFieldsGroup(fieldsGroupId: string): void {
220+
// Remove fields group from form store state
221+
this.State = this.State.withMutations(state => {
222+
state.FieldsGroups = state.FieldsGroups.remove(fieldsGroupId);
223+
});
224+
}
225+
226+
public UnregisterFieldsArray(fieldsGroupId: string): void {
227+
// Remove fields array from form store state
228+
this.State = this.State.withMutations(state => {
229+
state.FieldsGroups = state.FieldsGroups.remove(fieldsGroupId);
230+
});
231+
}
232+
200233
public HasField(fieldId: string): boolean {
201234
return this.State.Fields.has(fieldId);
202235
}

0 commit comments

Comments
 (0)