Skip to content

Commit

Permalink
add helper to set maybeRef
Browse files Browse the repository at this point in the history
  • Loading branch information
Cysword committed Oct 3, 2024
1 parent 4c37ed8 commit 4878c5f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 37 deletions.
44 changes: 7 additions & 37 deletions libs/core/src/form/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {type CustomHookItem, createHookManager} from '@myparcel-vfb/hook-manager
import {isOfType, asyncEvery, type PromiseOr} from '@myparcel/ts-utils';
import {isRequired} from '../validators';
import {normalizeFieldConfiguration} from '../utils/normalizeFieldConfiguration';
import {useDynamicWatcher} from '../utils';
import {useDynamicWatcher, updateMaybeRef} from '../utils';
import {
type ToRecord,
type FieldConfiguration,
Expand Down Expand Up @@ -160,57 +160,27 @@ export class Field<Type = unknown, Props extends ComponentProps = ComponentProps
};

public setValue(value: Type): void {
if (isRef(this.ref)) {
this.ref.value = value;
} else {
// @ts-expect-error todo
this.ref = value;
}
updateMaybeRef(this.ref, value);
}

public setDisabled(value: boolean): void {
if (isRef(this.isDisabled)) {
this.isDisabled.value = value;
} else {
// @ts-expect-error update types to for when the refs are unwrapped
this.isDisabled = value;
}
updateMaybeRef(this.isDisabled, value);
}

public setOptional(value: boolean): void {
if (isRef(this.isOptional)) {
this.isOptional.value = value;
} else {
// @ts-expect-error update types to for when the refs are unwrapped
this.isOptional = value;
}
updateMaybeRef(this.isOptional, value);
}

public setReadOnly(value: boolean): void {
if (isRef(this.isReadOnly)) {
this.isReadOnly.value = value;
} else {
// @ts-expect-error update types to for when the refs are unwrapped
this.isReadOnly = value;
}
updateMaybeRef(this.isReadOnly, value);
}

public addError(error: string): void {
if (isRef(this.errors)) {
this.errors.value.push(error);
} else {
// @ts-expect-error update types to for when the refs are unwrapped
this.errors.push(error);
}
updateMaybeRef(this.errors, [...toValue(this.errors), error]);
}

public setInvalid(): void {
if (isRef(this.isValid)) {
this.isValid.value = false;
} else {
// @ts-expect-error update types to for when the refs are unwrapped
this.isValid = false;
}
updateMaybeRef(this.isValid, false);
}

public validate = async (): Promise<boolean> => {
Expand Down
1 change: 1 addition & 0 deletions libs/core/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from './defineField';
export * from './defineForm';
export * from './generateFieldName';
export * from './markComponentAsRaw';
export * from './updateMaybeRef';
export * from './useDynamicWatcher';
export {getDefaultFormConfiguration} from './getDefaultFormConfiguration';

Expand Down
10 changes: 10 additions & 0 deletions libs/core/src/utils/updateMaybeRef.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {isRef, type MaybeRef} from 'vue';

export const updateMaybeRef = <T>(maybeRef: MaybeRef<T>, value: T): void => {
if (isRef(maybeRef)) {
maybeRef.value = value;
return;
}

maybeRef = value;
};

Check warning on line 10 in libs/core/src/utils/updateMaybeRef.ts

View check run for this annotation

Codecov / codecov/patch

libs/core/src/utils/updateMaybeRef.ts#L9-L10

Added lines #L9 - L10 were not covered by tests

0 comments on commit 4878c5f

Please sign in to comment.