diff --git a/glade_forms/CHANGELOG.md b/glade_forms/CHANGELOG.md index 2480b81..861bba3 100644 --- a/glade_forms/CHANGELOG.md +++ b/glade_forms/CHANGELOG.md @@ -1,6 +1,8 @@ ## DEV: - **[Add]**: Add `IntInput` as a specialized variant of GladeInput which has additional, int related, validations such as `isBetween`, `isMin`, `isMax` - **[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 8e4bd7b..69cd79c 100644 --- a/glade_forms/lib/src/core/glade_input.dart +++ b/glade_forms/lib/src/core/glade_input.dart @@ -538,11 +538,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, @@ -575,6 +575,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(); } }