Skip to content

Commit

Permalink
fix: handle change on country selection (#247)
Browse files Browse the repository at this point in the history
* fix: handle change on country selection

* docs: update changelog

* test: add case where onChange should be triggered when a country is selected

* fix: swapped the parameter placement in the expect function so that it makes more sense
  • Loading branch information
Suman085 committed May 31, 2024
1 parent 5b11e92 commit 170203c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## [9.2.5]
- fix: handle change on country selection. onChanged callback would trigger if country field is updated
- added Korean localization messages

## [9.2.4]
Expand Down
2 changes: 2 additions & 0 deletions lib/src/phone_form_field_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ class PhoneFormFieldState extends FormFieldState<PhoneNumber> {
final selected = await widget.countrySelectorNavigator.show(context);
if (selected != null) {
controller.changeCountry(selected);
didChange(controller.value);
widget.onChanged?.call(controller.value);
}
focusNode.requestFocus();
}
Expand Down
26 changes: 26 additions & 0 deletions test/phone_form_field_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,32 @@ void main() {
);
});

testWidgets('Should call onChange when countryCode updated', (tester) async {
bool changed = false;
PhoneNumber? phoneNumber =
PhoneNumber.parse('', destinationCountry: IsoCode.FR);
void onChanged(PhoneNumber? p) {
changed = true;
phoneNumber = p;
}

await tester.pumpWidget(
getWidget(
initialValue: phoneNumber,
onChanged: onChanged,
),
);
await tester.tap(find.byType(CountryButton));
await tester.pumpAndSettle();
expect(find.byType(CountrySelectorPage), findsOneWidget);
await tester.tap(find.byType(ListTile).first);
expect(changed, equals(true));
expect(
phoneNumber,
equals(PhoneNumber.parse('', destinationCountry: IsoCode.AF)),
);
});

group('validator', () {
testWidgets(
'Should display invalid message when PhoneValidator.valid is used '
Expand Down

0 comments on commit 170203c

Please sign in to comment.