Skip to content

Commit 8c1c20d

Browse files
committed
Set field.element as soon as possible
Otherwise emitFieldDidValidateEvent(field) won't contain field.element
1 parent 260397f commit 8c1c20d

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## Next
2+
3+
### Fixes
4+
5+
- Set field.element as soon as possible
6+
17
## v0.15.0 (2020/03/05)
28

39
### Breaking Changes

packages/react-form-with-constraints/src/Field.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ export class Field {
1010
// Can be useful for the user to get the DOM element
1111
// See https://github.com/tkrotoff/react-form-with-constraints/issues/41
1212
//
13-
// Only available when the field has been validated
14-
// Populated by FormWithConstraints._validateFields()
13+
// Populated by FormWithConstraints.validateField()
1514
//
1615
// Cannot be set as readonly :/
1716
public element?: HTMLInput | TextInput;

packages/react-form-with-constraints/src/FormWithConstraints.tsx

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { withFieldWillValidateEventEmitter } from './withFieldWillValidateEventE
66
import { withFieldDidValidateEventEmitter } from './withFieldDidValidateEventEmitter';
77
import { withFieldDidResetEventEmitter } from './withFieldDidResetEventEmitter';
88
import { Field } from './Field';
9-
import { IHTMLInput, InputElement, HTMLInput } from './InputElement';
9+
import { IHTMLInput, InputElement, HTMLInput, TextInput } from './InputElement';
1010
import { FieldsStore } from './FieldsStore';
1111
import { FieldFeedbackValidation } from './FieldFeedbackValidation';
1212
import { flattenDeep } from './flattenDeep';
@@ -84,24 +84,33 @@ export class FormWithConstraints
8484
for (let i = 0; i < inputs.length; i++) {
8585
const input = inputs[i];
8686
// eslint-disable-next-line no-await-in-loop
87-
const field = await this.validateField(forceValidateFields, new InputElement(input));
88-
if (field !== undefined) {
89-
field.element = input as HTMLInput;
90-
fields.push(field);
91-
}
87+
const field = await this.validateField(
88+
forceValidateFields,
89+
new InputElement(input),
90+
input as HTMLInput | TextInput
91+
);
92+
if (field !== undefined) fields.push(field);
9293
}
9394

9495
return fields;
9596
}
9697

97-
private async validateField(forceValidateFields: boolean, input: InputElement) {
98+
private async validateField(
99+
forceValidateFields: boolean,
100+
input: InputElement,
101+
102+
// We need to pass the native input separately instead of it being a property of InputElement
103+
// otherwise react-form-with-constraints-native unit tests will crash
104+
nativeInput: HTMLInput | TextInput
105+
) {
98106
const fieldName = input.name;
99107
const field = this.fieldsStore.getField(fieldName);
100108

101109
if (field === undefined) {
102110
// Means the field (<input name="username">) does not have a FieldFeedbacks
103111
// so let's ignore this field
104112
} else if (forceValidateFields || !field.hasFeedbacks()) {
113+
field.element = nativeInput as HTMLInput | TextInput;
105114
field.clearValidations();
106115

107116
this.emitFieldWillValidateEvent(fieldName);

packages/react-form-with-constraints/src/InputElement.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,10 @@ export class InputElement implements IHTMLInput {
7373

7474
constructor(input: IHTMLInput | TextInput) {
7575
if (isHTMLInput(input)) {
76+
// FIXME
7677
// eslint-disable-next-line no-param-reassign
7778
input = input as IHTMLInput;
79+
7880
this.name = input.name;
7981
this.type = input.type;
8082
this.value = input.value;
@@ -90,8 +92,10 @@ export class InputElement implements IHTMLInput {
9092

9193
this.validationMessage = input.validationMessage;
9294
} else {
95+
// FIXME
9396
// eslint-disable-next-line no-param-reassign
9497
input = input as TextInput;
98+
9599
this.name = input.props!.name;
96100
this.type = undefined as any;
97101
this.value = input.props!.value!; // Tested: TextInput props.value is always a string and never undefined (empty string instead)

0 commit comments

Comments
 (0)