Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
lreardon committed Jan 3, 2024
1 parent ac77d13 commit 6e55bbf
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 30 deletions.
24 changes: 18 additions & 6 deletions lib/src/form_builder_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,12 @@ class FormBuilderField<T> extends FormField<T> {
});

@override
FormBuilderFieldState<FormBuilderField<T>, T> createState() => FormBuilderFieldState<FormBuilderField<T>, T>();
FormBuilderFieldState<FormBuilderField<T>, T> createState() =>
FormBuilderFieldState<FormBuilderField<T>, T>();
}

class FormBuilderFieldState<F extends FormBuilderField<T>, T> extends FormFieldState<T> {
class FormBuilderFieldState<F extends FormBuilderField<T>, T>
extends FormFieldState<T> {
String? _customErrorText;
FormBuilderState? _formBuilderState;
bool _touched = false;
Expand All @@ -81,7 +83,10 @@ class FormBuilderFieldState<F extends FormBuilderField<T>, T> extends FormFieldS
/// Returns the initial value, which may be declared at the field, or by the
/// parent [FormBuilder.initialValue]. When declared at both levels, the field
/// initialValue prevails.
T? get initialValue => widget.initialValue ?? (_formBuilderState?.initialValue ?? const <String, dynamic>{})[widget.name] as T?;
T? get initialValue =>
widget.initialValue ??
(_formBuilderState?.initialValue ??
const <String, dynamic>{})[widget.name] as T?;

dynamic get transformedValue => widget.valueTransformer?.call(value) ?? value;

Expand All @@ -99,7 +104,9 @@ class FormBuilderFieldState<F extends FormBuilderField<T>, T> extends FormFieldS

bool get enabled => widget.enabled && (_formBuilderState?.enabled ?? true);
bool get readOnly => !(_formBuilderState?.widget.skipDisabled ?? false);
bool get _isAlwaysValidate => widget.autovalidateMode.isAlways || (_formBuilderState?.widget.autovalidateMode?.isAlways ?? false);
bool get _isAlwaysValidate =>
widget.autovalidateMode.isAlways ||
(_formBuilderState?.widget.autovalidateMode?.isAlways ?? false);

/// Will be true if the field is dirty
///
Expand Down Expand Up @@ -235,9 +242,14 @@ class FormBuilderFieldState<F extends FormBuilderField<T>, T> extends FormFieldS
}
final isValid = super.validate() && !hasError;

final fields = _formBuilderState?.fields ?? <String, FormBuilderFieldState<FormBuilderField<dynamic>, dynamic>>{};
final fields = _formBuilderState?.fields ??
<String, FormBuilderFieldState<FormBuilderField<dynamic>, dynamic>>{};

if (!isValid && focusOnInvalid && (formState?.focusOnInvalid ?? true) && enabled && !fields.values.any((e) => e.effectiveFocusNode.hasFocus)) {
if (!isValid &&
focusOnInvalid &&
(formState?.focusOnInvalid ?? true) &&
enabled &&
!fields.values.any((e) => e.effectiveFocusNode.hasFocus)) {
focus();
if (autoScrollWhenFocusOnInvalid) ensureScrollableVisibility();
}
Expand Down
78 changes: 54 additions & 24 deletions test/src/form_builder_field_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import '../form_builder_tester.dart';
void main() {
group('FormBuilderField -', () {
group('custom error -', () {
testWidgets('Should show custom error when invalidate field', (tester) async {
testWidgets('Should show custom error when invalidate field',
(tester) async {
final textFieldKey = GlobalKey<FormBuilderFieldState>();
const textFieldName = 'text2';
const errorTextField = 'error text field';
Expand Down Expand Up @@ -41,15 +42,18 @@ void main() {

expect(textFieldKey.currentState?.isValid, isFalse);
});
testWidgets('Should valid when no has error and autovalidateMode is always', (tester) async {
testWidgets(
'Should valid when no has error and autovalidateMode is always',
(tester) async {
final textFieldKey = GlobalKey<FormBuilderFieldState>();
const textFieldName = 'text';
const errorTextField = 'error text field';
final testWidget = FormBuilderTextField(
name: textFieldName,
key: textFieldKey,
autovalidateMode: AutovalidateMode.always,
validator: (value) => value == null || value.isEmpty ? errorTextField : null,
validator: (value) =>
value == null || value.isEmpty ? errorTextField : null,
);
await tester.pumpWidget(buildTestableFieldWidget(testWidget));

Expand All @@ -61,15 +65,18 @@ void main() {

expect(textFieldKey.currentState?.isValid, isTrue);
});
testWidgets('Should invalid when has error and autovalidateMode is always', (tester) async {
testWidgets(
'Should invalid when has error and autovalidateMode is always',
(tester) async {
final textFieldKey = GlobalKey<FormBuilderFieldState>();
const textFieldName = 'text';
const errorTextField = 'error text field';
final testWidget = FormBuilderTextField(
name: textFieldName,
key: textFieldKey,
autovalidateMode: AutovalidateMode.always,
validator: (value) => value == null || value.length < 10 ? errorTextField : null,
validator: (value) =>
value == null || value.length < 10 ? errorTextField : null,
);
await tester.pumpWidget(buildTestableFieldWidget(testWidget));

Expand Down Expand Up @@ -100,7 +107,8 @@ void main() {

expect(textFieldKey.currentState?.hasError, isTrue);
});
testWidgets('Should no has errors when is empty and no has validators', (tester) async {
testWidgets('Should no has errors when is empty and no has validators',
(tester) async {
final textFieldKey = GlobalKey<FormBuilderFieldState>();
const textFieldName = 'text';
final testWidget = FormBuilderTextField(
Expand All @@ -118,7 +126,9 @@ void main() {
});

group('valueIsValid -', () {
testWidgets('Should value is valid when validator passes, despite set custom error', (tester) async {
testWidgets(
'Should value is valid when validator passes, despite set custom error',
(tester) async {
final textFieldKey = GlobalKey<FormBuilderFieldState>();
const textFieldName = 'text';
const errorTextField = 'error text field';
Expand All @@ -137,7 +147,8 @@ void main() {
});

group('valueHasError -', () {
testWidgets('Should value is invalid when validator passes', (tester) async {
testWidgets('Should value is invalid when validator passes',
(tester) async {
final textFieldKey = GlobalKey<FormBuilderFieldState>();
const textFieldName = 'text';
const invalidValue = 'invalid';
Expand All @@ -155,20 +166,25 @@ void main() {
});

group('autovalidateMode -', () {
testWidgets('Should show error when init form and AutovalidateMode is always', (tester) async {
testWidgets(
'Should show error when init form and AutovalidateMode is always',
(tester) async {
const textFieldName = 'text4';
const errorTextField = 'error text field';
final testWidget = FormBuilderTextField(
name: textFieldName,
validator: (value) => value == null || value.isEmpty ? errorTextField : null,
validator: (value) =>
value == null || value.isEmpty ? errorTextField : null,
autovalidateMode: AutovalidateMode.always,
);
await tester.pumpWidget(buildTestableFieldWidget(testWidget));
await tester.pumpAndSettle();

expect(find.text(errorTextField), findsOneWidget);
});
testWidgets('Should show error when AutovalidateMode is onUserInteraction and change field', (tester) async {
testWidgets(
'Should show error when AutovalidateMode is onUserInteraction and change field',
(tester) async {
const textFieldName = 'text4';
const errorTextField = 'error text field';
final testWidget = FormBuilderTextField(
Expand All @@ -190,34 +206,40 @@ void main() {
testWidgets('Should not dirty by default', (tester) async {
const textFieldName = 'text';
final textFieldKey = GlobalKey<FormBuilderFieldState>();
final testWidget = FormBuilderTextField(name: textFieldName, key: textFieldKey);
final testWidget =
FormBuilderTextField(name: textFieldName, key: textFieldKey);
await tester.pumpWidget(buildTestableFieldWidget(testWidget));

expect(textFieldKey.currentState?.isDirty, false);
});
testWidgets('Should dirty when update field value by user', (tester) async {
testWidgets('Should dirty when update field value by user',
(tester) async {
const textFieldName = 'text';
final textFieldKey = GlobalKey<FormBuilderFieldState>();
final testWidget = FormBuilderTextField(name: textFieldName, key: textFieldKey);
final testWidget =
FormBuilderTextField(name: textFieldName, key: textFieldKey);
await tester.pumpWidget(buildTestableFieldWidget(testWidget));

final widgetFinder = find.byWidget(testWidget);
await tester.enterText(widgetFinder, 'test');

expect(textFieldKey.currentState?.isDirty, true);
});
testWidgets('Should dirty when update field value by method', (tester) async {
testWidgets('Should dirty when update field value by method',
(tester) async {
const textFieldName = 'text';
final textFieldKey = GlobalKey<FormBuilderFieldState>();
final testWidget = FormBuilderTextField(name: textFieldName, key: textFieldKey);
final testWidget =
FormBuilderTextField(name: textFieldName, key: textFieldKey);
await tester.pumpWidget(buildTestableFieldWidget(testWidget));

textFieldKey.currentState?.setValue('test');
await tester.pumpAndSettle();

expect(textFieldKey.currentState?.isDirty, true);
});
testWidgets('Should dirty when update field with initial value by user', (tester) async {
testWidgets('Should dirty when update field with initial value by user',
(tester) async {
const textFieldName = 'text';
final textFieldKey = GlobalKey<FormBuilderFieldState>();
final testWidget = FormBuilderTextField(
Expand All @@ -232,7 +254,8 @@ void main() {

expect(textFieldKey.currentState?.isDirty, true);
});
testWidgets('Should dirty when update field with initial value by method', (tester) async {
testWidgets('Should dirty when update field with initial value by method',
(tester) async {
const textFieldName = 'text';
final textFieldKey = GlobalKey<FormBuilderFieldState>();
final testWidget = FormBuilderTextField(
Expand All @@ -250,7 +273,8 @@ void main() {
testWidgets('Should not dirty when reset field value', (tester) async {
const textFieldName = 'text';
final textFieldKey = GlobalKey<FormBuilderFieldState>();
final testWidget = FormBuilderTextField(name: textFieldName, key: textFieldKey);
final testWidget =
FormBuilderTextField(name: textFieldName, key: textFieldKey);
await tester.pumpWidget(buildTestableFieldWidget(testWidget));

textFieldKey.currentState?.setValue('test');
Expand All @@ -259,7 +283,8 @@ void main() {

expect(textFieldKey.currentState?.isDirty, false);
});
testWidgets('Should not dirty when reset field with initial value', (tester) async {
testWidgets('Should not dirty when reset field with initial value',
(tester) async {
const textFieldName = 'text';
final textFieldKey = GlobalKey<FormBuilderFieldState>();
final testWidget = FormBuilderTextField(
Expand All @@ -281,15 +306,17 @@ void main() {
testWidgets('Should not touched by default', (tester) async {
const textFieldName = 'text';
final textFieldKey = GlobalKey<FormBuilderFieldState>();
final testWidget = FormBuilderTextField(name: textFieldName, key: textFieldKey);
final testWidget =
FormBuilderTextField(name: textFieldName, key: textFieldKey);
await tester.pumpWidget(buildTestableFieldWidget(testWidget));

expect(textFieldKey.currentState?.isTouched, false);
});
testWidgets('Should touched when focus input', (tester) async {
const textFieldName = 'text';
final textFieldKey = GlobalKey<FormBuilderFieldState>();
final testWidget = FormBuilderTextField(name: textFieldName, key: textFieldKey);
final testWidget =
FormBuilderTextField(name: textFieldName, key: textFieldKey);
await tester.pumpWidget(buildTestableFieldWidget(testWidget));

final widgetFinder = find.byWidget(testWidget);
Expand All @@ -303,7 +330,8 @@ void main() {
testWidgets('Should reset to null when call reset', (tester) async {
const textFieldName = 'text';
final textFieldKey = GlobalKey<FormBuilderFieldState>();
final testWidget = FormBuilderTextField(name: textFieldName, key: textFieldKey);
final testWidget =
FormBuilderTextField(name: textFieldName, key: textFieldKey);
await tester.pumpWidget(buildTestableFieldWidget(testWidget));

textFieldKey.currentState?.setValue('test');
Expand All @@ -329,7 +357,9 @@ void main() {

expect(textFieldKey.currentState?.value, equals(initialValue));
});
testWidgets('Should reset custom error when invalidate field and then reset', (tester) async {
testWidgets(
'Should reset custom error when invalidate field and then reset',
(tester) async {
final textFieldKey = GlobalKey<FormBuilderFieldState>();
const textFieldName = 'text';
const errorTextField = 'error text field';
Expand Down

0 comments on commit 6e55bbf

Please sign in to comment.