Skip to content

Commit 2523a12

Browse files
Submit works e2e: goes full cycle and FormStore.ToObject now works too.
1 parent a14b14f commit 2523a12

File tree

6 files changed

+48
-9
lines changed

6 files changed

+48
-9
lines changed

packages/simplr-forms-core/src/contracts/form-store.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,8 @@ export interface FormStoreState {
1212
}
1313

1414
export interface FormStoreStateRecord extends TypedRecord<FormStoreStateRecord>, FormStoreState { }
15+
16+
export interface BuiltFormObject {
17+
Fields: Immutable.Map<string, FieldStateRecord>;
18+
Object: any;
19+
}

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

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,15 @@ import {
1212
FieldStatePropsRecord,
1313
FieldStateProps
1414
} from "../contracts/field";
15-
import { FormState, FormStateRecord } from "../contracts/form";
16-
import { FormStoreState, FormStoreStateRecord } from "../contracts/form-store";
15+
import {
16+
FormState,
17+
FormStateRecord
18+
} from "../contracts/form";
19+
import {
20+
FormStoreState,
21+
FormStoreStateRecord,
22+
BuiltFormObject
23+
} from "../contracts/form-store";
1724
import { FieldsGroupStateRecord } from "../contracts/fields-group";
1825
import { ConstructFormError } from "../utils/form-error-helpers";
1926
import { FormError } from "../contracts/error";
@@ -26,14 +33,14 @@ export class FormStore extends ActionEmitter {
2633
}
2734

2835
protected FormId: string;
36+
protected BuiltFormObject: BuiltFormObject;
2937

3038
private state: FormStoreStateRecord;
3139
protected get State(): FormStoreStateRecord {
3240
return this.state;
3341
}
3442
protected set State(newState: FormStoreStateRecord) {
3543
this.state = newState;
36-
3744
this.emit(new Actions.StateUpdated());
3845
}
3946

@@ -255,6 +262,17 @@ export class FormStore extends ActionEmitter {
255262
}
256263
}
257264

265+
public ToObject<TObject = any>(): TObject {
266+
if (this.BuiltFormObject == null ||
267+
this.BuiltFormObject.Fields !== this.State.Fields) {
268+
this.BuiltFormObject = {
269+
Fields: this.State.Fields,
270+
Object: this.BuildFormObject(this.State)
271+
};
272+
}
273+
return this.BuiltFormObject.Object;
274+
}
275+
258276
/**
259277
* ========================
260278
* Local helper methods
@@ -294,6 +312,21 @@ export class FormStore extends ActionEmitter {
294312
};
295313
}
296314

315+
protected BuildFormObject(state: FormStoreStateRecord) {
316+
const formStoreObject: { [id: string]: any } = {};
317+
318+
this.State.Fields.forEach((field, fieldId) => {
319+
if (fieldId == null || field == null) {
320+
return;
321+
}
322+
formStoreObject[fieldId] = field.Value;
323+
});
324+
325+
// TODO: FieldsGroups values
326+
327+
return formStoreObject;
328+
}
329+
297330
protected IsPromise<T>(value: any): value is Promise<T> {
298331
return value != null && value.then != null && value.catch != null;
299332
}

packages/simplr-forms-dom/src/components/text.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as React from "react";
22
import { FieldProps, FieldValue } from "simplr-forms-core/contracts";
33

44
import { BaseDomField, BaseDomFieldState } from "../abstractions/base-dom-field";
5-
import { OnChangeCallback } from "../contracts/field";
5+
import { FieldOnChangeCallback } from "../contracts/field";
66

77
/**
88
* Override the differences between extended interfaces.
@@ -16,7 +16,7 @@ export interface TextProps extends FieldProps, React.HTMLProps<HTMLInputElement>
1616
name: string;
1717
onFocus?: React.EventHandler<React.FocusEvent<HTMLInputElement>>;
1818
onBlur?: React.EventHandler<React.FocusEvent<HTMLInputElement>>;
19-
onChange?: OnChangeCallback<HTMLInputElement>;
19+
onChange?: FieldOnChangeCallback<HTMLInputElement>;
2020
ref?: any;
2121
}
2222

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from "./contracts/field";
2+
export * from "./contracts/form";
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from "react";
22
import { FieldValue } from "simplr-forms-core/contracts";
33

4-
export interface OnChangeCallback<TElement> extends React.EventHandler<React.FormEvent<TElement>> {
4+
export interface FieldOnChangeCallback<TElement> extends React.EventHandler<React.FormEvent<TElement>> {
55
(event: React.FormEvent<TElement> | undefined, newValue: FieldValue, fieldId: string, formId: string): void;
66
}

packages/simplr-forms-dom/src/contracts/form.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import * as React from "react";
22
import { FieldValue, FormError } from "simplr-forms-core/contracts";
33
import { FormProps as CoreFormProps } from "simplr-forms-core/contracts";
44
import { FormStore } from "simplr-forms-core/stores";
5-
import { OnChangeCallback } from "../contracts/field";
5+
import { FieldOnChangeCallback } from "../contracts/field";
66

77
export interface FormOnSubmitCallback extends React.FormEventHandler<HTMLFormElement> {
88
(event: React.FormEvent<HTMLFormElement>, store: FormStore): void | Promise<never> | FormError | string;
99
}
1010

11-
export interface FormProps extends CoreFormProps {
11+
export interface FormProps extends CoreFormProps, React.HTMLProps<HTMLFormElement> {
1212
onSubmit?: FormOnSubmitCallback;
13-
onChange?: OnChangeCallback<any>;
13+
onChange?: FieldOnChangeCallback<any>;
1414
preventSubmitDefaultAndPropagation?: boolean;
1515
// tslint:disable-next-line:max-line-length
1616
// More properties at:

0 commit comments

Comments
 (0)