Skip to content

Commit ef2634b

Browse files
committed
fix(command): canExecuteFromNgForm validity when form is already initialized
1 parent 96ef267 commit ef2634b

File tree

4 files changed

+22315
-14
lines changed

4 files changed

+22315
-14
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 3.1.2 (2025-02-27)
2+
3+
### 🩹 Fixes
4+
5+
- **command:** `canExecuteFromNgForm` validity when form is already initialized
6+
17
## 3.1.1 (2025-02-20)
28

39
### 🩹 Fixes

libs/ngx.command/src/command.util.ts

+18-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { AbstractControl, PristineChangeEvent, StatusChangeEvent } from "@angular/forms";
2-
import { Observable, map, distinctUntilChanged, startWith, filter, combineLatest, of } from "rxjs";
2+
import { Observable, map, distinctUntilChanged, filter, combineLatest, of, defer, concat } from "rxjs";
33

44
import { CommandCreator, ICommand } from "./command.model";
55
import { Command } from "./command";
@@ -42,21 +42,26 @@ export function canExecuteFromNgForm(
4242
{ ...CAN_EXECUTE_FORM_OPTIONS_DEFAULTS, ...options }
4343
: CAN_EXECUTE_FORM_OPTIONS_DEFAULTS;
4444

45+
4546
const pristine$ = opts.dirty
46-
? form.events.pipe(
47-
filter(x => x instanceof PristineChangeEvent),
48-
map(x => x.pristine),
49-
distinctUntilChanged(),
50-
startWith(form.pristine),
51-
) : of(true);
47+
? concat(
48+
defer(() => of(form.pristine)),
49+
form.events.pipe(
50+
filter(x => x instanceof PristineChangeEvent),
51+
map(x => x.pristine),
52+
)
53+
).pipe(distinctUntilChanged(),)
54+
: of(true);
5255

5356
const valid$ = opts.validity
54-
? form.events.pipe(
55-
filter(x => x instanceof StatusChangeEvent),
56-
map(x => x.status === "VALID"),
57-
distinctUntilChanged(),
58-
startWith(form.valid),
59-
) : of(true);
57+
? concat(
58+
defer(() => of(form.valid)),
59+
form.events.pipe(
60+
filter(x => x instanceof StatusChangeEvent),
61+
map(x => x.status === "VALID"),
62+
)
63+
).pipe(distinctUntilChanged(),)
64+
: of(true);
6065

6166
return combineLatest([pristine$, valid$]).pipe(
6267
map(([pristine, valid]) => !!(!opts.validity || valid) && !!(!opts.dirty || !pristine)),

0 commit comments

Comments
 (0)