Skip to content

Commit a491b87

Browse files
committed
#488 - Allow overwriting default value on reset
- Adds an `overwriteDefaultValue` parameter to the `FormControl.reset()` method. - When `overwriteDefaultValue` is `true`, the value passed to `reset()` becomes the new default value for the control. - This allows dynamically updating what the control considers its "default" state to reset to. - The internal `_defaultValue` field in `FormControl` is now mutable to support this change.
1 parent dbbfb0a commit a491b87

File tree

2 files changed

+81
-6
lines changed

2 files changed

+81
-6
lines changed

lib/src/models/models.dart

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,7 @@ abstract class AbstractControl<T> {
839839

840840
/// Tracks the value and validation status of an individual form control.
841841
class FormControl<T> extends AbstractControl<T> {
842-
final T? _defaultValue;
842+
T? _defaultValue;
843843
final _focusChanges = StreamController<bool>.broadcast();
844844
FocusController? _focusController;
845845
bool _hasFocus = false;
@@ -1040,6 +1040,9 @@ class FormControl<T> extends AbstractControl<T> {
10401040
/// initial value provided in the constructor.
10411041
/// - If [nonNullable] is `false`, the control resets to `null`.
10421042
///
1043+
/// If [overwriteDefaultValue] is true, then the value used to reset the
1044+
/// control becomes the new default value of the control.
1045+
///
10431046
/// The argument [disabled] is optional and resets the disabled status of the
10441047
/// control. If value is `true` then it will disable the control, if value is
10451048
/// `false` then it will enable the control, and if the value is `null` or
@@ -1101,11 +1104,16 @@ class FormControl<T> extends AbstractControl<T> {
11011104
@override
11021105
void reset({
11031106
T? value,
1107+
bool overwriteDefaultValue = false,
11041108
bool updateParent = true,
11051109
bool emitEvent = true,
11061110
bool removeFocus = false,
11071111
bool? disabled,
11081112
}) {
1113+
if (overwriteDefaultValue) {
1114+
_defaultValue = value;
1115+
}
1116+
11091117
super.reset(
11101118
value: value ?? _defaultValue,
11111119
updateParent: updateParent,

test/src/models/form_control_test.dart

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -474,19 +474,21 @@ void main() {
474474
},
475475
);
476476

477-
test('resets to null is nonNullable is false', () {
478-
// Arrange
477+
test('resets to null if nonNullable is false', () {
478+
// Given: a control with an initial value and nonNullable as false
479479
final control = FormControl<String>(
480480
value: 'initialValue',
481481
nonNullable: false,
482482
);
483+
484+
// When: set a new value
483485
control.value = 'changed value';
484486

485-
// Act
487+
// And: reset the control
486488
control.reset();
487489

488-
// Assert
489-
expect(control.value, null); // Because nonNullable is false
490+
// Then: control value is null
491+
expect(control.value, null);
490492
});
491493

492494
test(
@@ -504,4 +506,69 @@ void main() {
504506
},
505507
);
506508
});
509+
510+
group('FormControl reset with overwriteDefaultValue', () {
511+
test('updates default value when overwriteDefaultValue is true', () {
512+
// Given: a control with an initial value
513+
final control = FormControl<String>(value: 'initial');
514+
515+
// When: reset to a new value and ask to overwrite the default
516+
control.reset(value: 'newDefault', overwriteDefaultValue: true);
517+
518+
// Then: current value is the new value
519+
expect(control.value, 'newDefault');
520+
// And: the default value property is updated
521+
expect(control.defaultValue, 'newDefault');
522+
523+
// When: we make the control dirty again
524+
control.value = 'dirty value';
525+
526+
// And: we reset without arguments
527+
control.reset();
528+
529+
// Then: it resets to the NEW default value
530+
expect(control.value, 'newDefault');
531+
});
532+
533+
test(
534+
'does not update default value when overwriteDefaultValue is false',
535+
() {
536+
// Given: a control with an initial value
537+
final control = FormControl<String>(value: 'initial');
538+
539+
// When: reset with a specific value but do NOT overwrite default
540+
control.reset(value: 'tempValue', overwriteDefaultValue: false);
541+
542+
// Then: value is the temporary value
543+
expect(control.value, 'tempValue');
544+
// And: default value remains the original one
545+
expect(control.defaultValue, 'initial');
546+
547+
// When: we make the control dirty again
548+
control.value = 'dirty value';
549+
550+
// And: we reset without arguments
551+
control.reset();
552+
553+
// Then: it resets to the ORIGINAL default value
554+
expect(control.value, 'initial');
555+
},
556+
);
557+
558+
test(
559+
'updates default value to null when value is null and overwriteDefaultValue is true',
560+
() {
561+
// Given: a control with an initial value
562+
final control = FormControl<String>(value: 'initial');
563+
564+
// When: reset to null and overwrite default
565+
control.reset(value: null, overwriteDefaultValue: true);
566+
567+
// Then: value is null
568+
expect(control.value, null);
569+
// And: default value is null
570+
expect(control.defaultValue, null);
571+
},
572+
);
573+
});
507574
}

0 commit comments

Comments
 (0)