Skip to content

Commit

Permalink
refactor: renamed shadowed variable, make return a type [form events]
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-small committed Jun 5, 2024
1 parent d9cbade commit abed5b0
Showing 1 changed file with 35 additions and 34 deletions.
69 changes: 35 additions & 34 deletions libs/ngxtension/form-events/src/form-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ function isTouchedEvent<T>(
): event is TouchedChangeEvent {
return event instanceof TouchedChangeEvent;
}
export function allEventsObservable<T>(form: AbstractControl<T>): Observable<{

type FormEventData<T> = {
value: T;
status: FormControlStatus;
touched: boolean;
Expand All @@ -82,7 +83,11 @@ export function allEventsObservable<T>(form: AbstractControl<T>): Observable<{
pending: boolean;
dirty: boolean;
untouched: boolean;
}> {
};

export function allEventsObservable<T>(
form: AbstractControl<T>,
): Observable<FormEventData<T>> {
return combineLatest([
valueEvents$(form).pipe(
startWith(form.value),
Expand All @@ -96,50 +101,46 @@ export function allEventsObservable<T>(form: AbstractControl<T>): Observable<{
touchedEvents$(form).pipe(startWith(form.touched)),
pristineEvents$(form).pipe(startWith(form.pristine)),
]).pipe(
map(([value, status, touched, pristine]) => {
map(([valueParam, statusParam, touchedParam, pristineParam]) => {
// Original values (plus value)
const stat: FormControlStatus | StatusChangeEvent = isStatusEvent(status)
? status.status
: status;
const touch: boolean | TouchedChangeEvent = isTouchedEvent(touched)
? touched.touched
: touched;
const prist: boolean | PristineChangeEvent = isPristineEvent(pristine)
? pristine.pristine
: pristine;
const stat: FormControlStatus | StatusChangeEvent = isStatusEvent(
statusParam,
)
? statusParam.status
: statusParam;
const touch: boolean | TouchedChangeEvent = isTouchedEvent(touchedParam)
? touchedParam.touched
: touchedParam;
const prist: boolean | PristineChangeEvent = isPristineEvent(
pristineParam,
)
? pristineParam.pristine
: pristineParam;

// Derived values - not directly named as events but are aliases for something that can be derived from original values
const valid = stat === 'VALID';
const invalid = stat === 'INVALID';
const pending = stat === 'PENDING';
const dirty = !prist;
const untouched = !touch;
const validDerived = stat === 'VALID';
const invalidDerived = stat === 'INVALID';
const pendingDerived = stat === 'PENDING';
const dirtyDerived = !prist;
const untouchedDerived = !touch;

return {
value: value,
value: valueParam,
status: stat,
touched: touch,
pristine: prist,
valid: valid,
invalid: invalid,
pending: pending,
dirty: dirty,
untouched: untouched,
valid: validDerived,
invalid: invalidDerived,
pending: pendingDerived,
dirty: dirtyDerived,
untouched: untouchedDerived,
};
}),
);
}
export function allEventsSignal<T>(form: AbstractControl<T>): Signal<{
value: T;
status: FormControlStatus;
touched: boolean;
pristine: boolean;
valid: boolean;
invalid: boolean;
pending: boolean;
dirty: boolean;
untouched: boolean;
}> {
export function allEventsSignal<T>(
form: AbstractControl<T>,
): Signal<FormEventData<T>> {
return toSignal(allEventsObservable(form), {
initialValue: {
value: form.value,
Expand Down

0 comments on commit abed5b0

Please sign in to comment.