From 890927454371c358f1ef26153ddc4b2f8088e843 Mon Sep 17 00:00:00 2001 From: Hugo Pointcheval Date: Sun, 10 Jul 2022 22:10:12 +0200 Subject: [PATCH] feat(form): add helpers --- .../lib/src/cubit/form_data_state.dart | 5 ++ .../lib/src/enums/form_status.dart | 18 ++++- .../lib/src/form/form_data.dart | 79 ++++++++++++------- 3 files changed, 69 insertions(+), 33 deletions(-) diff --git a/packages/wyatt_form_bloc/lib/src/cubit/form_data_state.dart b/packages/wyatt_form_bloc/lib/src/cubit/form_data_state.dart index 2d03e217..20db50b0 100644 --- a/packages/wyatt_form_bloc/lib/src/cubit/form_data_state.dart +++ b/packages/wyatt_form_bloc/lib/src/cubit/form_data_state.dart @@ -18,8 +18,13 @@ part of 'form_data_cubit.dart'; @immutable class FormDataState extends Equatable { + /// Global status of a form. final FormStatus status; + + /// FormData with all inputs, and associated metadata. final FormData data; + + /// Optional error message. final String? errorMessage; const FormDataState({ diff --git a/packages/wyatt_form_bloc/lib/src/enums/form_status.dart b/packages/wyatt_form_bloc/lib/src/enums/form_status.dart index b5701ef6..01870220 100644 --- a/packages/wyatt_form_bloc/lib/src/enums/form_status.dart +++ b/packages/wyatt_form_bloc/lib/src/enums/form_status.dart @@ -76,11 +76,21 @@ enum FormStatus { /// Indicates whether the form submission has been canceled. bool get isSubmissionCanceled => this == FormStatus.submissionCanceled; - /// Validate a list of inputs - static FormStatus validate(List inputs) { - return inputs.every((FormInput input) => input.validator.pure) + /// Validate a list of inputs by processing them in `validate` as validators. + static FormStatus validateInputs(List inputs) { + return validate( + inputs + .map((FormInput input) => input.validator) + .toList(), + ); + } + + /// Validate a list of validators. + static FormStatus validate(List validators) { + return validators.every((FormInputValidator validator) => validator.pure) ? FormStatus.pure - : inputs.any((FormInput input) => input.validator.valid == false) + : validators + .any((FormInputValidator validator) => validator.valid == false) ? FormStatus.invalid : FormStatus.valid; } diff --git a/packages/wyatt_form_bloc/lib/src/form/form_data.dart b/packages/wyatt_form_bloc/lib/src/form/form_data.dart index 8c978cf9..7e874580 100644 --- a/packages/wyatt_form_bloc/lib/src/form/form_data.dart +++ b/packages/wyatt_form_bloc/lib/src/form/form_data.dart @@ -23,8 +23,11 @@ class FormData extends Equatable { const FormData(this._inputs); const FormData.empty() : this(const []); + /// Returns all inputs as a list + List inputs() => _inputs; + /// Returns the input for the associated key - FormInput inputByKey(String key) { + FormInput inputOf(String key) { if (contains(key)) { return _inputs.firstWhere((FormInput input) => input.key == key); } else { @@ -36,65 +39,83 @@ class FormData extends Equatable { void updateInput(String key, FormInput input) { if (contains(key)) { final index = _inputs.indexOf( - inputByKey(key), + inputOf(key), ); _inputs[index] = input; } } - /// Updates validator of a given input. (perform copyWith) - void updateValidator(String key, FormInputValidator dirtyValue) { - if (contains(key)) { - final index = _inputs.indexOf( - inputByKey(key), - ); - _inputs[index] = _inputs[index].copyWith(validator: dirtyValue); - } - } - - /// Updates metadata of a given input. (perform copyWith) - void updateMetadata(String key, FormInputMetadata meta) { - if (contains(key)) { - final index = _inputs.indexOf( - inputByKey(key), - ); - _inputs[index] = _inputs[index].copyWith(metadata: meta); - } + /// Returns all associated validators as a list + List> validators() { + return _inputs + .map>( + (FormInput input) => input.validator as FormInputValidator, + ) + .toList(); } /// A [FormInputValidator] represents the value of a single form input field. /// It contains information about the [FormInputStatus], value, as well /// as validation status. T validatorOf(String key) { - return inputByKey(key).validator as T; + return inputOf(key).validator as T; } - /// Returns a validation error if the [FormInputValidator] is invalid. + /// Updates validator of a given input. (perform copyWith) + void updateValidator(String key, FormInputValidator dirtyValue) { + if (contains(key)) { + final index = _inputs.indexOf( + inputOf(key), + ); + _inputs[index] = _inputs[index].copyWith(validator: dirtyValue); + } + } + + /// Returns a validation error if the [FormInputValidator] is invalid. /// Returns null if the [FormInputValidator] is valid. E? errorOf(String key) { - return (inputByKey(key).validator as FormInputValidator).error; + return (inputOf(key).validator as FormInputValidator).error; } - /// The value of the associated [FormInputValidator]. For example, + /// The value of the associated [FormInputValidator]. For example, /// if you have a FormInputValidator for FirstName, the value could be 'Joe'. V valueOf(String key) { - return (inputByKey(key).validator as FormInputValidator).value; + return (inputOf(key).validator as FormInputValidator).value; } /// Returns `true` if the [FormInputValidator] is not valid. /// Same as `E? errorOf(String key) != null` bool isNotValid(String key) { - return !inputByKey(key).validator.valid; + return !inputOf(key).validator.valid; + } + + /// Returns all associated metadata as a list + List> metadata() { + return _inputs + .map>( + (FormInput input) => input.metadata as FormInputMetadata, + ) + .toList(); } /// Returns the metadata associated. With `M` the type of extra data. FormInputMetadata metadataOf(String key) { - return inputByKey(key).metadata as FormInputMetadata; + return inputOf(key).metadata as FormInputMetadata; + } + + /// Updates metadata of a given input. (perform copyWith) + void updateMetadata(String key, FormInputMetadata meta) { + if (contains(key)) { + final index = _inputs.indexOf( + inputOf(key), + ); + _inputs[index] = _inputs[index].copyWith(metadata: meta); + } } /// Validate self inputs. FormStatus validate() { - return FormStatus.validate(_inputs); + return FormStatus.validateInputs(_inputs); } /// Check if this contains an input with the given key. @@ -171,7 +192,7 @@ class FormData extends Equatable { @override bool? get stringify => true; - + @override List get props => _inputs; }