Skip to content

Commit

Permalink
Merge branch 'main' into feat/63-int-input-specialized-validators
Browse files Browse the repository at this point in the history
  • Loading branch information
M1chlCZ authored Oct 7, 2024
2 parents f00ab4f + 564d061 commit f6dcdbd
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
2 changes: 2 additions & 0 deletions glade_forms/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## DEV:
- **[Add]**: Add `IntInput` as a specialized variant of GladeInput<int> 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.
Expand Down
19 changes: 15 additions & 4 deletions glade_forms/lib/src/core/glade_input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -538,11 +538,11 @@ class GladeInput<T> {
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<T>? value,
ValueGetter<T>? initialValue,
bool invokeUpdate = true,
Expand Down Expand Up @@ -575,6 +575,17 @@ class GladeInput<T> {
}
}

/// 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<T> copyWith({
String? inputKey,
Expand Down
29 changes: 23 additions & 6 deletions glade_forms/lib/src/model/glade_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ abstract class GladeModel extends ChangeNotifier {
_lastUpdates.add(input);
} else {
_lastUpdates = [input];
notifyDependecies();
notifyDependencies();
notifyListeners();
}
}
Expand All @@ -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());
Expand All @@ -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();
}
}

0 comments on commit f6dcdbd

Please sign in to comment.