Skip to content

Commit

Permalink
Merge pull request #61 from netglade/feat/reset-to-pure-model
Browse files Browse the repository at this point in the history
Add reset to pure on model level
  • Loading branch information
petrnymsa authored May 10, 2024
2 parents 52b395d + 1f592f8 commit 6fabb27
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
3 changes: 3 additions & 0 deletions glade_forms/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 2.2.0
- **[Add]**: Add `resetToPure` on model level.

## 2.1.0
- **[Add]**: Add `defaultTranslateError` on model level.

Expand Down
15 changes: 14 additions & 1 deletion glade_forms/lib/src/core/glade_input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,12 @@ class GladeInput<T> {
///
/// Allows to sets new initialValue and value if needed.
/// By default ([invokeUpdate]=`true`) setting value will trigger listeners.
void resetToPure({ValueGetter<T>? value, ValueGetter<T>? initialValue, bool invokeUpdate = true}) {
void resetToPure({
ValueGetter<T>? value,
ValueGetter<T>? initialValue,
bool invokeUpdate = true,
bool copyValueToInitialValue = false,
}) {
this._isPure = true;

if (value != null) {
Expand All @@ -554,6 +559,14 @@ class GladeInput<T> {

if (initialValue != null) {
this._initialValue = initialValue();

if (invokeUpdate) _bindedModel?.notifyInputUpdated(this);
}

if (copyValueToInitialValue) {
this._initialValue = this.value;

if (invokeUpdate) _bindedModel?.notifyInputUpdated(this);
}
}

Expand Down
9 changes: 9 additions & 0 deletions glade_forms/lib/src/model/glade_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,13 @@ abstract class GladeModel extends ChangeNotifier {
if (union.isNotEmpty) input.onDependencyChange?.call(union.toList());
}
}

/// Resets all inputs to pure state.
///
/// When [copyValueToInitialValue] is true, input's initialValue is overriden by current value.
void resetToPure({bool copyValueToInitialValue = false}) {
for (final input in inputs) {
input.resetToPure(copyValueToInitialValue: copyValueToInitialValue);
}
}
}
2 changes: 1 addition & 1 deletion glade_forms/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: glade_forms
description: A universal way to define form validators with support of translations.
version: 2.1.0
version: 2.2.0
repository: https://github.com/netglade/glade_forms
issue_tracker: https://github.com/netglade/glade_forms/issues
screenshots:
Expand Down

0 comments on commit 6fabb27

Please sign in to comment.