Skip to content

Commit 423d3ff

Browse files
Streamlines function names in form-store. FormProps are now save in form-store. Disabled prop introduced into FormProps.
1 parent 9a1147d commit 423d3ff

File tree

12 files changed

+119
-34
lines changed

12 files changed

+119
-34
lines changed

packages/simplr-forms-core/src/abstractions/base-container.ts

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 * as PropTypes from "prop-types";
33

44
import { FormStore } from "../stores/form-store";
5-
import { StateUpdated } from "../actions/form-store";
5+
import { StateChanged } from "../actions/form-store";
66
import { FSHContainer } from "../stores/form-stores-handler";
77

88
export interface BaseContainerProps {
@@ -48,7 +48,7 @@ export abstract class BaseContainer<TProps extends BaseContainerProps, TState> e
4848
throw new Error(`simplr-forms-core: Container is already in a Form '${this.context.FormId}' context, ${but}.`);
4949
}
5050

51-
this.FormStore.addListener(StateUpdated, this.OnStoreUpdated.bind(this));
51+
this.FormStore.addListener(StateChanged, this.OnStoreUpdated.bind(this));
5252
}
5353

5454
protected abstract OnStoreUpdated(): void;

packages/simplr-forms-core/src/abstractions/base-form.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ export abstract class BaseForm<TProps extends FormContracts.FormProps, TState> e
1919
}
2020

2121
static defaultProps: FormContracts.FormProps = {
22-
destroyOnUnmount: true
22+
destroyOnUnmount: true,
23+
disabled: false
2324
};
2425

2526
constructor(props: FormContracts.FormProps) {
2627
super();
2728
this.registerForm(props);
29+
this.FormStore.UpdateFormProps(props);
2830
}
2931

3032
protected get FormStoresHandler(): FormStoresHandler {
@@ -53,7 +55,7 @@ export abstract class BaseForm<TProps extends FormContracts.FormProps, TState> e
5355
/*
5456
* Local helpers
5557
*/
56-
private registerForm(props: FormContracts.FormProps) {
58+
private registerForm(props: FormContracts.FormProps): void {
5759
if (props.formId == null) {
5860
if (!props.destroyOnUnmount) {
5961
throw new Error("simplr-forms-core: destroyOnUnmount cannot be falsy when formId is not set.");

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ export abstract class CoreField<TProps extends CoreFieldProps, TState extends Co
8282
throw new Error("simplr-forms-core: Field must be used inside a Form component.");
8383
}
8484
this.StoreEventSubscription =
85-
this.FormStore.addListener<FormStoreActions.StateUpdated>(
86-
FormStoreActions.StateUpdated,
85+
this.FormStore.addListener<FormStoreActions.StateChanged>(
86+
FormStoreActions.StateChanged,
8787
this.OnStoreUpdated.bind(this));
8888
this.registerFieldInFormStore();
8989
}
@@ -94,7 +94,7 @@ export abstract class CoreField<TProps extends CoreFieldProps, TState extends Co
9494
throw new Error(`simplr-forms-core: Field name must be constant`);
9595
}
9696

97-
this.FormStore.UpdateProps(this.FieldId, nextProps);
97+
this.FormStore.UpdateFieldProps(this.FieldId, nextProps);
9898
}
9999

100100
componentWillUnmount() {
@@ -133,9 +133,12 @@ export abstract class CoreField<TProps extends CoreFieldProps, TState extends Co
133133
return this.RawDefaultValue;
134134
}
135135

136-
protected ProcessValueBeforeStore(value: FieldValue) {
136+
protected ProcessValueBeforeStore(value: FieldValue): FieldValue {
137137
// Parse and normalize value
138-
return this.NormalizeValue(this.ParseValue(value));
138+
if (value != null) {
139+
return this.NormalizeValue(this.ParseValue(value));
140+
}
141+
return value;
139142
}
140143

141144
protected ProcessValueFromStore(value: FieldValue) {
@@ -212,7 +215,7 @@ export abstract class CoreField<TProps extends CoreFieldProps, TState extends Co
212215
newValue = this.ProcessValueBeforeStore(newValue);
213216
}
214217

215-
this.FormStore.ValueChanged(this.FieldId, newValue);
218+
this.FormStore.UpdateFieldValue(this.FieldId, newValue);
216219
}
217220

218221
/**

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { FieldValue } from "../contracts/field";
22

3-
export class StateUpdated { }
3+
export class StateChanged { }
44

55
export class FieldRegistered {
66
constructor(private fieldId: string, private initialValue: FieldValue) { }
@@ -26,10 +26,12 @@ export class ValueChanged {
2626
}
2727
}
2828

29-
export class PropsChanged {
29+
export class FieldPropsChanged {
3030
constructor(private fieldId: string) { }
3131

3232
public get FieldId() {
3333
return this.fieldId;
3434
}
3535
}
36+
37+
export class FormPropsChanged { }

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ import { TypedRecord } from "typed-immutable-record";
33

44
import { FieldStateRecord } from "../contracts/field";
55
import { FieldsGroupStoreState } from "../contracts/fields-group";
6-
import { FormStateRecord } from "../contracts/form";
6+
import { FormStateRecord, FormPropsRecord } from "../contracts/form";
77

88
export interface FormStoreState {
99
Fields: Immutable.Map<string, FieldStateRecord>;
1010
FieldsGroups: Immutable.Map<string, FieldsGroupStoreState>;
1111
Form: FormStateRecord;
12+
FormProps: FormPropsRecord;
1213
}
1314

1415
export interface FormStoreStateRecord extends TypedRecord<FormStoreStateRecord>, FormStoreState { }

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ export interface FormProps {
88
formStore?: FormStore;
99
destroyOnUnmount?: boolean;
1010
forceSubmit?: boolean;
11+
disabled?: boolean;
1112
}
1213

14+
export interface FormPropsRecord extends TypedRecord<FormPropsRecord>, FormProps { }
15+
1316
export interface FormChildContext {
1417
FormId: string;
1518
}

packages/simplr-forms-core/src/modifiers/currency.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ export class CurrencyModifier extends BaseModifier<CurrencyProps, {}> {
1717
}
1818
Parse(value: FieldValue): FieldValue {
1919
if (value === this.emptyFormattedValue) {
20-
console.log(`Parser got an emptyFormattedValue.`);
2120
return 0;
2221
}
2322
if (ValueOfType<string>(value, CurrencyModifier.name, "string")) {

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

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import {
1313
} from "../contracts/field";
1414
import {
1515
FormState,
16-
FormStateRecord
16+
FormStateRecord,
17+
FormProps,
18+
FormPropsRecord
1719
} from "../contracts/form";
1820
import {
1921
FormStoreState,
@@ -40,7 +42,7 @@ export class FormStore extends ActionEmitter {
4042
}
4143
protected set State(newState: FormStoreStateRecord) {
4244
this.state = newState;
43-
this.emit(new Actions.StateUpdated());
45+
this.emit(new Actions.StateChanged());
4446
}
4547

4648
public GetState(): FormStoreStateRecord {
@@ -141,15 +143,22 @@ export class FormStore extends ActionEmitter {
141143
return this.State.Fields.get(fieldId);
142144
}
143145

144-
public SetSubmitCallback(submitCallback: () => void): void {
146+
public SetFormSubmitCallback(submitCallback: () => void): void {
145147
this.State = this.State.withMutations(state => {
146148
state.Form = state.Form.merge({
147149
SubmitCallback: submitCallback
148150
} as FormState);
149151
});
150152
}
151153

152-
public UpdateProps(fieldId: string, props: FieldStateProps): void {
154+
public UpdateFormProps(props: FormProps): void {
155+
this.State = this.State.withMutations(state => {
156+
state.FormProps = recordify<FormProps, FormPropsRecord>(props);
157+
this.emit(new Actions.FormPropsChanged());
158+
});
159+
}
160+
161+
public UpdateFieldProps(fieldId: string, props: FieldStateProps): void {
153162
const propsRecord = recordify<FieldStateProps, FieldStatePropsRecord>(props);
154163
const fieldState = this.State.Fields.get(fieldId);
155164

@@ -164,10 +173,10 @@ export class FormStore extends ActionEmitter {
164173
} as FieldState));
165174
});
166175

167-
this.emit(new Actions.PropsChanged(fieldId));
176+
this.emit(new Actions.FieldPropsChanged(fieldId));
168177
}
169178

170-
public ValueChanged(fieldId: string, newValue: FieldValue): void {
179+
public UpdateFieldValue(fieldId: string, newValue: FieldValue): void {
171180
this.State = this.State.withMutations(state => {
172181
const fieldState = state.Fields.get(fieldId);
173182
state.Fields = state.Fields.set(fieldId, fieldState.merge({
@@ -178,7 +187,7 @@ export class FormStore extends ActionEmitter {
178187
this.emit(new Actions.ValueChanged(fieldId, newValue));
179188
}
180189

181-
public async Validate(fieldId: string, validationPromise: Promise<never>): Promise<void> {
190+
public async ValidateiField(fieldId: string, validationPromise: Promise<never>): Promise<void> {
182191
const field = this.State.Fields.get(fieldId);
183192
const fieldValue = field.Value;
184193

@@ -237,14 +246,14 @@ export class FormStore extends ActionEmitter {
237246
}
238247
}
239248

240-
public InitiateSubmit(): void {
249+
public InitiateFormSubmit(): void {
241250
if (this.State.Form.SubmitCallback == null) {
242251
throw new Error("simplr-forms-core: Submit method is called before SubmitCallback is set.");
243252
}
244253
this.State.Form.SubmitCallback();
245254
}
246255

247-
public async Submit(result: Promise<void> | FormError | any): Promise<void> {
256+
public async SubmitForm(result: Promise<void> | FormError | any): Promise<void> {
248257
let promise: Promise<void>;
249258
if (this.IsPromise<void>(result)) {
250259
promise = result;
@@ -356,7 +365,8 @@ export class FormStore extends ActionEmitter {
356365
return recordify<FormStoreState, FormStoreStateRecord>({
357366
Fields: Immutable.Map<string, FieldStateRecord>(),
358367
FieldsGroups: Immutable.Map<string, FieldsGroupStateRecord>(),
359-
Form: recordify<FormState, FormStateRecord>(this.GetInitialFormState())
368+
Form: recordify<FormState, FormStateRecord>(this.GetInitialFormState()),
369+
FormProps: recordify<FormProps, FormPropsRecord>({})
360370
});
361371
}
362372

packages/simplr-forms-core/src/utils/value-helpers.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ export function ProcessValue<TProcessor, TProcessedResult>(
4141

4242
export function IsComponentOfType(component: JSX.Element, requiredType: string) {
4343
let componentType = component.type as any;
44+
if (componentType == null && typeof component === "string") {
45+
console.warn("simplr-forms-core: text should not be rendered inside fields:", component);
46+
return false;
47+
}
4448
return (componentType[requiredType] != null);
4549
}
4650

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export class Form extends BaseForm<FormProps, {}> {
88

99
protected SetElementRef = (element: HTMLFormElement) => {
1010
this.Element = element;
11-
this.FormStore.SetSubmitCallback(() => {
11+
this.FormStore.SetFormSubmitCallback(() => {
1212
element.dispatchEvent(new Event("submit"));
1313
});
1414
}
@@ -36,7 +36,7 @@ export class Form extends BaseForm<FormProps, {}> {
3636
event.persist();
3737

3838
const result = this.props.onSubmit(event, this.FormStore);
39-
this.FormStore.Submit(result);
39+
this.FormStore.SubmitForm(result);
4040
}
4141

4242
render() {

0 commit comments

Comments
 (0)