Skip to content

Commit

Permalink
Add markAsPending to controls
Browse files Browse the repository at this point in the history
issue #274

- Allow to mark an abstract control as pending
by demand.
  • Loading branch information
joanpablo committed Aug 21, 2023
1 parent f6c1377 commit 987768f
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 0 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# 16.1.0

## Features

- Add `markAsPending()` method to `AbstractControl` to allow set the status
to PENDING by demand.

# 16.0.4

## Fixes
Expand Down
24 changes: 24 additions & 0 deletions lib/src/models/models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ abstract class AbstractControl<T> {
/// * VALID: This control has passed all validation checks.
/// * INVALID: This control has failed at least one validation check.
/// * PENDING: This control is in the midst of conducting a validation check.
/// * DISABLED: This control is exempt from ancestor calculations of
/// validity or value.
///
/// These status values are mutually exclusive, so a control cannot be both
/// valid AND invalid or invalid AND pending.
Expand Down Expand Up @@ -420,6 +422,28 @@ abstract class AbstractControl<T> {
_updateAncestors(updateParent);
}

/// Marks the control as `pending`.
///
/// A control is pending while the control performs async validation.
///
/// When [updateParent] is false, mark only this control. When true or not
/// supplied, marks all direct ancestors. Default is true.
///
/// When [emitEvent] is true or not supplied (the default), a [statusChanged]
/// event is emitted.
///
void markAsPending({bool updateParent = true, bool emitEvent = true}) {
_status = ControlStatus.pending;

if (emitEvent) {
this._statusChanges.add(_status);
}

if (updateParent) {
parent?.markAsPending(updateParent: updateParent, emitEvent: emitEvent);
}
}

/// Disposes the control
void dispose() {
_statusChanges.close();
Expand Down
18 changes: 18 additions & 0 deletions test/src/models/form_array_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,24 @@ void main() {
// Then: array value is patched
expect(array.value, [2, 2], reason: 'array value not patched');
});

test('Test that markAsPending() a control, set pending status to the array '
'as well.', () {
// Given: an array with valid status.
final array = FormArray<int>([
FormControl<int>(value: 1),
]);

// Expect: the array to be VALID and not PENDING.
expect(array.valid, true);
expect(array.pending, false);

// When: I call mark a child control as PENDING.
array.controls.first.markAsPending();

// Then: the status of the array is PENDING as well.
expect(array.pending, true);
});
});
}

Expand Down
15 changes: 15 additions & 0 deletions test/src/models/form_control_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -371,5 +371,20 @@ void main() {
// Then: a new async validator is added
expect(formControl.asyncValidators.length, 1);
});

test('Test that markAsPending() change the status to PENDING.', () {
// Given: a control with valid status.
final control = FormControl<String>(value: 'Reactive Forms');

// Expect: the control to be VALID and not PENDING.
expect(control.valid, true);
expect(control.pending, false);

// When: I call markAsPending() method.
control.markAsPending();

// Then: the status is PENDING.
expect(control.pending, true);
});
});
}
18 changes: 18 additions & 0 deletions test/src/models/form_group_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -982,5 +982,23 @@ void main() {
// Expect: an assertion error
expect(form, throwsAssertionError);
});

test('Test that markAsPending() a control, set pending status to the group '
'as well.', () {
// Given: a form group with valid status.
final form = FormGroup({
'name': FormControl<String>(value: "Reactive Forms")
});

// Expect: the group to be VALID and not PENDING.
expect(form.valid, true);
expect(form.pending, false);

// When: I call mark a child control as PENDING.
form.control('name').markAsPending();

// Then: the status of the Form Group is PENDING as well.
expect(form.pending, true);
});
});
}

0 comments on commit 987768f

Please sign in to comment.