Skip to content

Commit

Permalink
Merge pull request #39 from yeikel16/fix-submit-event-trasformer
Browse files Browse the repository at this point in the history
fix(lyform): change submit transform event to droppable
  • Loading branch information
leynier authored Apr 22, 2024
2 parents 79b2fb1 + 6f18aaf commit da34c88
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 5 deletions.
8 changes: 5 additions & 3 deletions packages/lyform/lib/src/ly_form/ly_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ abstract class LyForm<D, E> extends Bloc<LyFormEvent, LyFormState<D, E>>
await Future.delayed(Duration.zero);
await onSubmitEvent(emit);
},
transformer: sequential(),
transformer: droppable(),
);

on<LyFormAddInputEvent>(
Expand Down Expand Up @@ -118,7 +118,8 @@ abstract class LyForm<D, E> extends Bloc<LyFormEvent, LyFormState<D, E>>

/// Called when the form is submitted.
Future<void> onSubmitEvent(Emitter<LyFormState<D, E>> emit) async {
validate();
await validate();

if (!isValid()) {
emit(invalid());
} else {
Expand Down Expand Up @@ -191,10 +192,11 @@ abstract class LyForm<D, E> extends Bloc<LyFormEvent, LyFormState<D, E>>
}

/// Validate all inputs and return [true] if they all are valids.
void validate() {
FutureOr<void> validate() async {
for (final input in _inputs) {
if (input.validator != null && !input.isPure) {
input.validate();
await Future.delayed(Duration.zero);
}
}
}
Expand Down
63 changes: 61 additions & 2 deletions packages/lyform/test/src/ly_form/ly_form_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,16 @@ void main() {
..name.dirty('ly')
..submit(),
skip: 1,
wait: const Duration(seconds: 1),
wait: const Duration(seconds: 2),
expect: () => const [
LyFormValidState<String, String>([
LyInputState(
value: 'ly',
lastNotNullValue: 'ly',
pureValue: '',
debugName: 'name',
),
]),
LyFormLoadingState<String, String>([
LyInputState<String>(
value: 'ly',
Expand All @@ -309,6 +317,39 @@ void main() {
debugName: 'name',
),
]),
LyFormSuccessState<String, String>(
'Name save success!',
[
LyInputState(
value: 'ly',
lastNotNullValue: 'ly',
pureValue: '',
debugName: 'name',
),
],
),
LyFormPureState<String, String>([
LyInputState<String>(
value: 'ly',
lastNotNullValue: 'ly',
pureValue: 'ly',
debugName: 'name',
),
]),
],
);

blocTest<TestForm, LyFormState>(
'check when submit event was called and is dropped when is more than one',
build: () => TestForm(),
act: (form) => form
..name.dirty('ly')
..submit()
..submit()
..submit(),
skip: 1,
wait: const Duration(seconds: 2),
expect: () => const [
LyFormValidState<String, String>([
LyInputState(
value: 'ly',
Expand All @@ -317,17 +358,35 @@ void main() {
debugName: 'name',
),
]),
LyFormLoadingState<String, String>([
LyInputState<String>(
value: 'ly',
lastNotNullValue: 'ly',
pureValue: '',
debugName: 'name',
),
]),
LyFormSuccessState<String, String>(
'Name save success!',
[
LyInputState(
LyInputState<String>(
value: 'ly',
lastNotNullValue: 'ly',
pureValue: '',
debugName: 'name',
),
],
),
LyFormPureState<String, String>(
[
LyInputState<String>(
value: 'ly',
lastNotNullValue: 'ly',
pureValue: 'ly',
debugName: 'name',
),
],
),
],
);

Expand Down

0 comments on commit da34c88

Please sign in to comment.