diff --git a/glade_forms/CHANGELOG.md b/glade_forms/CHANGELOG.md index b6ba021..ba8d55e 100644 --- a/glade_forms/CHANGELOG.md +++ b/glade_forms/CHANGELOG.md @@ -1,5 +1,11 @@ +# 3.0.1 +- **[Fix]**: GladeFormProvider is missing key property [Fix 73](https://github.com/netglade/glade_forms/issues/73) +- **[Fix]**: enable value transform with text editing controller [Fix 72](https://github.com/netglade/glade_forms/issues/72) +- **[Fix]**: Input subscribed to its own changes in onDependencyChange [Fix 76](https://github.com/netglade/glade_forms/issues/76) + ## 3.0.0 + **Breaking release** - **[Add]**: Add `allowBlank` parameter to `isEmpty` string validator. diff --git a/glade_forms/lib/src/core/glade_input.dart b/glade_forms/lib/src/core/glade_input.dart index 7648d86..6a66363 100644 --- a/glade_forms/lib/src/core/glade_input.dart +++ b/glade_forms/lib/src/core/glade_input.dart @@ -628,6 +628,11 @@ class GladeInput { _textEditingController?.removeListener(_onTextControllerChange); } + @override + String toString() { + return '$inputKey ($value)'; + } + void _syncValueWithController(T value, {required bool shouldTriggerOnChange}) { final converter = stringToValueConverter ?? _defaultConverter; try { diff --git a/glade_forms/lib/src/model/glade_model.dart b/glade_forms/lib/src/model/glade_model.dart index 2777699..6053e62 100644 --- a/glade_forms/lib/src/model/glade_model.dart +++ b/glade_forms/lib/src/model/glade_model.dart @@ -122,9 +122,10 @@ abstract class GladeModel extends ChangeNotifier { } void notifyDependencies() { - final updatedKeys = _lastUpdates.map((e) => e.inputKey); + final updatedKeys = _lastUpdates.map((e) => e.inputKey).toSet(); for (final input in inputs) { - final union = input.dependencies.map((e) => e.inputKey).toSet().union(updatedKeys.toSet()); + final updatedKeysExceptInputItself = updatedKeys.difference({input.inputKey}); + final union = input.dependencies.map((e) => e.inputKey).toSet().intersection(updatedKeysExceptInputItself); if (union.isNotEmpty) input.onDependencyChange?.call(union.toList()); } diff --git a/glade_forms/pubspec.yaml b/glade_forms/pubspec.yaml index 8023bf0..4888a93 100644 --- a/glade_forms/pubspec.yaml +++ b/glade_forms/pubspec.yaml @@ -1,6 +1,6 @@ name: glade_forms description: A universal way to define form validators with support of translations. -version: 3.0.0 +version: 3.0.1 repository: https://github.com/netglade/glade_forms issue_tracker: https://github.com/netglade/glade_forms/issues screenshots: diff --git a/glade_forms/test/model/glade_model_test.dart b/glade_forms/test/model/glade_model_test.dart new file mode 100644 index 0000000..8989ee3 --- /dev/null +++ b/glade_forms/test/model/glade_model_test.dart @@ -0,0 +1,88 @@ +// ignore_for_file: avoid-duplicate-test-assertions, prefer-correct-callback-field-name, avoid-global-state + +import 'package:glade_forms/glade_forms.dart'; +import 'package:test/test.dart'; + +class _ModeWithDependencies extends GladeModel { + late GladeInput a; + late GladeInput b; + late GladeInput c; + + int aUpdated = 0; + int bUpdated = 0; + int cUpdated = 0; + + int onDepenencyCalledCount = 0; + + List updatedDependencyKeys = []; + + @override + List> get inputs => [a, b, c]; + + @override + void initialize() { + a = GladeInput.intInput(value: 0, inputKey: 'a'); + b = GladeInput.intInput(value: 1, inputKey: 'b'); + c = GladeInput.intInput( + value: 1, + inputKey: 'c', + dependencies: () => [a, b], + onDependencyChange: (keys) { + if (keys.contains('a')) aUpdated++; + if (keys.contains('b')) bUpdated++; + + onDepenencyCalledCount++; + + updatedDependencyKeys = keys; + }, + ); + + super.initialize(); + } +} + +void main() { + test('When updating [a] onDependencyChange is called for a', () { + final model = _ModeWithDependencies(); + + model.a.value = 5; + expect(model.a.value, equals(5)); + expect(model.onDepenencyCalledCount, equals(1)); + expect(model.lastUpdatedInputKeys, equals(['a'])); + expect(model.updatedDependencyKeys, equals(['a'])); + }); + + test('When updating [b] onDependencyChange is called for b', () { + final model = _ModeWithDependencies(); + + model.b.value = 5; + expect(model.b.value, equals(5)); + expect(model.onDepenencyCalledCount, equals(1)); + expect(model.lastUpdatedInputKeys, equals(['b'])); + expect(model.updatedDependencyKeys, equals(['b'])); + }); + + test('When updating [a, b] together onDependencyChange is called for [a,b]', () { + final model = _ModeWithDependencies(); + model.groupEdit(() { + model.b.value = 5; + model.a.value = 5; + }); + + expect(model.a.value, equals(5)); + expect(model.b.value, equals(5)); + expect(model.onDepenencyCalledCount, equals(1)); + expect(model.lastUpdatedInputKeys.toSet().difference({'a', 'b'}), equals({})); + expect(model.updatedDependencyKeys.toSet().difference({'a', 'b'}), equals({})); + }); + + test('When updating [c] onDependencyChange is not called', () { + final model = _ModeWithDependencies(); + model.c.value = 5; + + expect(model.c.value, equals(5)); + expect(model.lastUpdatedInputKeys.toSet().difference({'c'}), equals({})); + expect(model.onDepenencyCalledCount, isZero); + expect(model.updatedDependencyKeys.toSet().difference({}), equals({})); + }); +} diff --git a/glade_forms/test/model/group_edit_test.dart b/glade_forms/test/model/group_edit_test.dart index fea37bf..f08c941 100644 --- a/glade_forms/test/model/group_edit_test.dart +++ b/glade_forms/test/model/group_edit_test.dart @@ -40,9 +40,11 @@ class _ModeWithDependencies extends GladeModel { c = GladeInput.intInput( value: 1, inputKey: 'c', + dependencies: () => [a, b], onDependencyChange: (keys) { if (keys.contains('a')) aUpdated++; if (keys.contains('b')) bUpdated++; + if (keys.contains('c')) cUpdated++; onDepenencyCalledCount++; }, diff --git a/storybook/lib/usecases/onchange/two_way_checkbox_change.dart b/storybook/lib/usecases/onchange/two_way_checkbox_change.dart index 28de5f5..a83a901 100644 --- a/storybook/lib/usecases/onchange/two_way_checkbox_change.dart +++ b/storybook/lib/usecases/onchange/two_way_checkbox_change.dart @@ -51,9 +51,11 @@ class AgeRestrictedModel extends GladeModel { return devMessage; }, - onChange: (info) { - vipInput.value = info.value >= 18; - }, + // * cyclic dependency between ageInput and vipInput + // Better to use onDependencyChange inside vipInput + // onChange: (info) { + // vipInput.value = info.value >= 18; + // }, useTextEditingController: true, ); vipInput = GladeInput.create( @@ -61,9 +63,14 @@ class AgeRestrictedModel extends GladeModel { value: false, inputKey: 'vip-input', dependencies: () => [ageInput], + onDependencyChange: (updateInputKeys) { + if (updateInputKeys.contains('age-input')) { + vipInput.updateValue(ageInput.value >= 18, shouldTriggerOnChange: false); + } + }, onChange: (info) { if (info.value && ageInput.value < 18) { - ageInput.value = 18; + ageInput.updateValue(18, shouldTriggerOnChange: false); } }, );