Skip to content

Commit d91a5d7

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 d91a5d7

File tree

2 files changed

+74
-1
lines changed

2 files changed

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

0 commit comments

Comments
 (0)