diff --git a/glade_forms/CHANGELOG.md b/glade_forms/CHANGELOG.md index e67c184..83aedb4 100644 --- a/glade_forms/CHANGELOG.md +++ b/glade_forms/CHANGELOG.md @@ -1,5 +1,7 @@ ## DEV: - **[Add]**: Support skipping particular validation with `shouldValidate` callback. +- **[Breaking]**: The `resetToPure` method on both GladeInput and GladeModel has been renamed to `setAsNewPure`. This change better reflects the method's behavior of setting a new pure state rather than resetting to the original state. +- **[Add]**: New `resetToPure` method added to both GladeInput and GladeModel. This method truly resets the input(s) to their initial value(s) and marks them as pure. ## 2.2.0 - **[Add]**: Add `resetToPure` on model level. diff --git a/glade_forms/lib/src/core/glade_input.dart b/glade_forms/lib/src/core/glade_input.dart index b794629..90bf5a1 100644 --- a/glade_forms/lib/src/core/glade_input.dart +++ b/glade_forms/lib/src/core/glade_input.dart @@ -533,11 +533,11 @@ class GladeInput { updateValue(value, shouldTriggerOnChange: shouldTriggerOnChange); } - /// Resets input into pure state. + /// Sets a new pure state for the input. /// - /// Allows to sets new initialValue and value if needed. - /// By default ([invokeUpdate]=`true`) setting value will trigger listeners. - void resetToPure({ + /// Allows to set new initialValue and value if needed. + /// By default ([invokeUpdate]=`true`) setting value will trigger listeners. + void setAsNewPure({ ValueGetter? value, ValueGetter? initialValue, bool invokeUpdate = true, @@ -570,6 +570,17 @@ class GladeInput { } } + /// Resets the input value to its initial value and sets it as pure. + void resetToPure() { + this._isPure = true; + if (_useTextEditingController) { + _syncValueWithController(_initialValue as T, shouldTriggerOnChange: true); + } else { + _value = _initialValue as T; + } + _bindedModel?.notifyInputUpdated(this); + } + @protected GladeInput copyWith({ String? inputKey, diff --git a/glade_forms/lib/src/model/glade_model.dart b/glade_forms/lib/src/model/glade_model.dart index f04e02e..2777699 100644 --- a/glade_forms/lib/src/model/glade_model.dart +++ b/glade_forms/lib/src/model/glade_model.dart @@ -103,7 +103,7 @@ abstract class GladeModel extends ChangeNotifier { _lastUpdates.add(input); } else { _lastUpdates = [input]; - notifyDependecies(); + notifyDependencies(); notifyListeners(); } } @@ -116,12 +116,12 @@ abstract class GladeModel extends ChangeNotifier { _groupEdit = false; - notifyDependecies(); + notifyDependencies(); notifyListeners(); } - void notifyDependecies() { + void notifyDependencies() { final updatedKeys = _lastUpdates.map((e) => e.inputKey); for (final input in inputs) { final union = input.dependencies.map((e) => e.inputKey).toSet().union(updatedKeys.toSet()); @@ -130,12 +130,29 @@ abstract class GladeModel extends ChangeNotifier { } } - /// Resets all inputs to pure state. + /// Sets a new pure state for all inputs in the model. /// /// When [copyValueToInitialValue] is true, input's initialValue is overriden by current value. - void resetToPure({bool copyValueToInitialValue = false}) { + void setAsNewPure({ + bool invokeUpdate = true, + bool copyValueToInitialValue = false, + }) { for (final input in inputs) { - input.resetToPure(copyValueToInitialValue: copyValueToInitialValue); + input.setAsNewPure( + invokeUpdate: invokeUpdate, + copyValueToInitialValue: copyValueToInitialValue, + ); } + if (invokeUpdate) { + notifyListeners(); + } + } + + /// Resets all inputs in the model to their initial values and sets them as pure. + void resetToPure() { + for (final input in inputs) { + input.resetToPure(); + } + notifyListeners(); } } diff --git a/glade_forms/lib/src/widgets/glade_form_builder.dart b/glade_forms/lib/src/widgets/glade_form_builder.dart index 0f2672d..987209a 100644 --- a/glade_forms/lib/src/widgets/glade_form_builder.dart +++ b/glade_forms/lib/src/widgets/glade_form_builder.dart @@ -9,7 +9,6 @@ class GladeFormBuilder extends StatelessWidget { // ignore: prefer-correct-callback-field-name, ok name final CreateModelFunction? create; final M? value; - // ignore: prefer-correct-callback-field-name, ok name final GladeFormWidgetBuilder builder; final Widget? child; diff --git a/glade_forms/lib/src/widgets/glade_form_consumer.dart b/glade_forms/lib/src/widgets/glade_form_consumer.dart index 98b5155..5ea4deb 100644 --- a/glade_forms/lib/src/widgets/glade_form_consumer.dart +++ b/glade_forms/lib/src/widgets/glade_form_consumer.dart @@ -4,7 +4,6 @@ import 'package:glade_forms/src/widgets/glade_form_builder.dart'; import 'package:glade_forms/src/widgets/glade_form_listener.dart'; class GladeFormConsumer extends StatelessWidget { - // ignore: prefer-correct-callback-field-name, ok name final GladeFormWidgetBuilder builder; // ignore: prefer-correct-callback-field-name, ok name final GladeFormListenerFn? listener;