Skip to content

Commit e43441b

Browse files
DefaultValue, InitialValue and Value fallbacks in RegisterField.
DefaultValue -> RawDefaultValue. DefaultValue processing added in RegisterField.
1 parent c9b84f8 commit e43441b

File tree

4 files changed

+44
-21
lines changed

4 files changed

+44
-21
lines changed

packages/simplr-forms-core/__tests__/test-components/test-field.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class MyTestField extends BaseField<MyFieldProps, MyFieldState> {
3030
return "";
3131
}
3232

33-
protected get DefaultValue(): FieldValue {
33+
protected get RawDefaultValue(): FieldValue {
3434
if (this.props.defaultValue != null) {
3535
return this.props.defaultValue;
3636
}

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

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ export abstract class BaseField<TProps extends FieldProps, TState extends BaseFi
140140
}
141141

142142
// Return default value
143-
return this.DefaultValue;
143+
return this.RawDefaultValue;
144144
}
145145

146146
protected ProcessValueBeforeStore(value: FieldValue) {
@@ -236,17 +236,6 @@ export abstract class BaseField<TProps extends FieldProps, TState extends BaseFi
236236
*/
237237
abstract render(): JSX.Element | null;
238238

239-
/**
240-
* Value before render.
241-
* Most common usage is for getting value from field props.
242-
*
243-
* @readonly
244-
* @protected
245-
* @type {(FieldContracts.ValueTypes | any)}
246-
* @memberOf BaseField
247-
*/
248-
protected abstract get RawValue(): FieldValue;
249-
250239
/**
251240
* Default field value.
252241
*
@@ -255,13 +244,24 @@ export abstract class BaseField<TProps extends FieldProps, TState extends BaseFi
255244
*
256245
* @memberOf BaseField
257246
*/
258-
protected abstract get DefaultValue(): FieldValue;
247+
protected abstract get RawDefaultValue(): FieldValue;
259248

260249
/**
261250
* Initial value.
262251
*/
263252
protected abstract get RawInitialValue(): FieldValue;
264253

254+
/**
255+
* Value before render.
256+
* Most common usage is for getting value from field props.
257+
*
258+
* @readonly
259+
* @protected
260+
* @type {(FieldContracts.ValueTypes | any)}
261+
* @memberOf BaseField
262+
*/
263+
protected abstract get RawValue(): FieldValue;
264+
265265
/**
266266
* ========================
267267
* Local helper methods
@@ -280,11 +280,12 @@ export abstract class BaseField<TProps extends FieldProps, TState extends BaseFi
280280
throw new Error(`simplr-forms-core: Duplicate field id '${this.FieldId}'`);
281281
}
282282

283+
const defaultValue = this.ProcessValueBeforeStore(this.RawDefaultValue);
283284
const initialValue = this.ProcessValueBeforeStore(this.RawInitialValue);
284285
const value = this.ProcessValueBeforeStore(this.RawValue);
285286
this.FormStore.RegisterField(
286287
this.FieldId,
287-
this.DefaultValue,
288+
defaultValue,
288289
initialValue,
289290
value,
290291
this.props,

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import * as PropTypes from "prop-types";
33

44
import * as FormContracts from "../contracts/form";
55
import { FormStore } from "../stores/form-store";
6-
import { FormError } from "../contracts/error";
7-
import { ConstructFormError } from "../utils/form-error-helpers";
86
import { FSHContainer, FormStoresHandler } from "../stores/form-stores-handler";
97

108
export abstract class BaseForm<TProps extends FormContracts.FormProps, TState> extends React.Component<TProps, TState> {

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

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,40 @@ export class FormStore extends ActionEmitter {
7373
public RegisterField(
7474
fieldId: string,
7575
defaultValue: FieldValue,
76-
initialValue: FieldValue,
77-
value: FieldValue,
76+
initialValue?: FieldValue,
77+
value?: FieldValue,
7878
props?: FieldStateProps,
7979
fieldsGroupId?: string
8080
): void {
8181
// Construct field state
8282
let fieldState = this.GetInitialFieldState();
83+
84+
// Set default value without fallbacks
8385
fieldState.DefaultValue = defaultValue;
84-
fieldState.InitialValue = (initialValue != null) ? initialValue : value;
85-
fieldState.Value = value;
86+
87+
// (initialValue)(value)
88+
// 0 => null
89+
// 1 => not null
90+
91+
// If neither initialValue, nor value is given
92+
// Fallback to defaultValue
93+
if (initialValue == null && value == null) {
94+
// 0 0
95+
fieldState.InitialValue = defaultValue;
96+
fieldState.Value = defaultValue;
97+
} else if (initialValue != null && value == null) {
98+
// 1 0
99+
fieldState.InitialValue = initialValue;
100+
fieldState.Value = initialValue;
101+
} else if (initialValue == null && value != null) {
102+
// 0 1
103+
fieldState.InitialValue = value;
104+
fieldState.Value = value;
105+
} else {
106+
// 0 0
107+
fieldState.InitialValue = initialValue;
108+
fieldState.Value = value;
109+
}
86110

87111
if (props != null) {
88112
fieldState.Props = recordify<FieldStateProps, FieldStatePropsRecord>(props);

0 commit comments

Comments
 (0)