Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: [MDS-682] Fix InputGroup auto validate mode #254

Merged
merged 1 commit into from
Sep 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 7 additions & 19 deletions lib/src/widgets/text_input_group/text_input_group.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,6 @@ enum MoonTextInputGroupOrientation {
typedef MoonTextInputGroupErrorBuilder = Widget Function(BuildContext context, List<String> errorText);

class MoonTextInputGroup extends StatefulWidget {
/// Used to enable/disable this TextInputGroup auto validation and update its error text.
///
/// {@template flutter.widgets.FormField.autovalidateMode}
/// If [AutovalidateMode.onUserInteraction], this TextInputGroup will only auto-validate after its content changes.
/// If [AutovalidateMode.always], it will auto-validate even without user interaction. If [AutovalidateMode.disabled],
/// auto-validation will be disabled.
///
/// Defaults to [AutovalidateMode.disabled], cannot be null.
/// {@endtemplate}
final AutovalidateMode autovalidateMode;

/// If false the widget is "disabled": it ignores taps and has a reduced opacity.
final bool enabled;

Expand Down Expand Up @@ -93,7 +82,6 @@ class MoonTextInputGroup extends StatefulWidget {
/// MDS TextInputGroup widget
const MoonTextInputGroup({
super.key,
this.autovalidateMode = AutovalidateMode.disabled,
this.enabled = true,
this.borderRadius,
this.clipBehavior,
Expand Down Expand Up @@ -121,6 +109,7 @@ class MoonTextInputGroup extends StatefulWidget {

class _MoonTextInputGroupState extends State<MoonTextInputGroup> {
late final List<String?> _validatorErrors = List.filled(widget.children.length, null);
late List<String?> _previousValidatorErrors;

bool get _groupHasValidationError => _validatorErrors.nonNulls.toList().isNotEmpty;
bool get _groupHasErrorText =>
Expand All @@ -136,20 +125,19 @@ class _MoonTextInputGroupState extends State<MoonTextInputGroup> {
bool get _shouldShowError => _groupHasError || _groupIsInErrorState;

void _handleValidationError(int index, String? errorText) {
_previousValidatorErrors = _validatorErrors;

if (_previousValidatorErrors[index] == errorText) return;

if (errorText != null) {
_validatorErrors[index] = errorText;
} else {
_validatorErrors[index] = null;
}
}

@override
void didUpdateWidget(covariant MoonTextInputGroup oldWidget) {
super.didUpdateWidget(oldWidget);

// This is necessary to reflect the children validation state visually.
WidgetsBinding.instance.addPostFrameCallback((Duration _) {
if (!mounted) return;
// Rebuild the widget to show the error
setState(() {});
});
}
Expand Down Expand Up @@ -214,7 +202,7 @@ class _MoonTextInputGroupState extends State<MoonTextInputGroup> {
autocorrect: widget.children[derivedIndex].configuration.autocorrect,
autofillHints: widget.children[derivedIndex].configuration.autofillHints,
autofocus: widget.children[derivedIndex].configuration.autofocus,
autovalidateMode: widget.autovalidateMode,
autovalidateMode: widget.children[derivedIndex].configuration.autovalidateMode,
backgroundColor: Colors.transparent,
borderRadius: widget.children[derivedIndex].configuration.borderRadius,
canRequestFocus: widget.children[derivedIndex].configuration.canRequestFocus,
Expand Down