Skip to content

Commit dba3d3e

Browse files
committed
fix: types and type reactivity in devtools
1 parent d36aa5e commit dba3d3e

File tree

7 files changed

+33
-31
lines changed

7 files changed

+33
-31
lines changed

packages/core/src/types/controls.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { StandardSchema } from './forms';
44
export interface ControlApi {
55
getControlElement(): HTMLElement | undefined;
66
getControlId(): string | undefined;
7-
getControlType(): typeof BuiltInControlTypes | string | undefined;
7+
getControlType(): string | undefined;
88
}
99

1010
export interface ControlProps<TValue = unknown, TInitialValue = TValue> {

packages/core/src/useFormField/useFieldController.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
AriaLabelProps,
99
Reactivify,
1010
} from '../types';
11-
import { BuiltInControlTypes, ControlApi, ControlProps } from '../types/controls';
11+
import { ControlApi, ControlProps } from '../types/controls';
1212
import { normalizeProps } from '../utils/common';
1313

1414
export interface FieldControllerProps {
@@ -39,6 +39,11 @@ export interface FieldController {
3939
*/
4040
controlId: Ref<string>;
4141

42+
/**
43+
* The type of the control, used for devtools.
44+
*/
45+
controlType: Ref<string>;
46+
4247
/**
4348
* Props for the label element.
4449
*/
@@ -73,11 +78,6 @@ export interface FieldController {
7378
* Registers a control interface, used to get the control element and id.
7479
*/
7580
registerControl: (control: ControlApi) => void;
76-
77-
/**
78-
* The type of the control, used for devtools.
79-
*/
80-
controlType: Ref<typeof BuiltInControlTypes | string>;
8181
}
8282

8383
export const FieldControllerKey: InjectionKey<FieldController> = Symbol('FieldControllerKey');

packages/core/src/useFormField/useFormField.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
ValidationResult,
1313
} from '../types';
1414
import { normalizeProps, warn } from '../utils/common';
15-
import { Simplify } from 'type-fest';
1615
import { registerField } from '@formwerk/devtools';
1716

1817
export type FormFieldInit<V = unknown> = Reactivify<FieldControllerProps> & Partial<FieldStateInit<V>>;
@@ -23,19 +22,17 @@ export type FormField<TValue = unknown> = FieldController & {
2322
state: FieldState<TValue>;
2423
};
2524

26-
export type WithFieldProps<TControlProps extends object> = Simplify<
27-
Omit<TControlProps, '_field'> & {
28-
/**
29-
* The label of the field.
30-
*/
31-
label: string;
32-
33-
/**
34-
* The description of the field.
35-
*/
36-
description?: string | undefined;
37-
}
38-
>;
25+
export type WithFieldProps<TControlProps extends object> = Omit<TControlProps, '_field'> & {
26+
/**
27+
* The label of the field.
28+
*/
29+
label: string;
30+
31+
/**
32+
* The description of the field.
33+
*/
34+
description?: string | undefined;
35+
};
3936

4037
export function useFormField<TValue = unknown>(
4138
init?: FormFieldInit<TValue>,

packages/devtools/src/helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ export function getFieldNodeTags(field: DevtoolsField, valid: boolean) {
198198
backgroundColor: bgColor,
199199
},
200200
{
201-
label: field.type,
201+
label: toValue(field.type),
202202
textColor: COLORS.black,
203203
backgroundColor: COLORS.gray,
204204
},

packages/devtools/src/init.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import {
22
App,
33
type ComponentInternalInstance,
44
getCurrentInstance,
5+
MaybeRefOrGetter,
56
nextTick,
67
onMounted,
78
onUnmounted,
8-
Ref,
99
watch,
1010
} from 'vue';
1111
import { throttle } from '../../../packages/shared/src';
@@ -228,8 +228,7 @@ export function initDevTools() {
228228
return vm;
229229
}
230230

231-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
232-
export function registerField(field: FieldState<any>, type: Ref<any> | string) {
231+
export function registerField(field: FieldState<any>, type: MaybeRefOrGetter<string>) {
233232
onMounted(async () => {
234233
const vm = initDevTools();
235234
// Makes sure forms are registered before fields in same component contexts

packages/devtools/src/registry.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { FieldState, FormReturns, Maybe } from '@core/index';
2-
import { ComponentInternalInstance, onUnmounted, Ref, toValue } from 'vue';
2+
import { ComponentInternalInstance, MaybeRefOrGetter, onUnmounted } from 'vue';
33
import { DevtoolsForm, DevtoolsRootForm } from './types';
44
import { getRootFormId } from './constants';
55

@@ -46,8 +46,11 @@ export function getAllForms() {
4646
return Array.from(TREE.values()).filter(node => !('_isRoot' in node)) as DevtoolsForm[];
4747
}
4848

49-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
50-
export function registerField(field: FieldState<any>, type: Ref<any> | string, vm: Maybe<ComponentInternalInstance>) {
49+
export function registerField(
50+
field: FieldState<unknown>,
51+
type: MaybeRefOrGetter<string>,
52+
vm: Maybe<ComponentInternalInstance>,
53+
) {
5154
const id = field.getPath() ?? field.getName() ?? '';
5255
const formId = field.form?.id ?? getRootFormId();
5356

@@ -72,7 +75,7 @@ export function registerField(field: FieldState<any>, type: Ref<any> | string, v
7275
form.fields.set(id, {
7376
...field,
7477
_vm: vm,
75-
type: toValue(type),
78+
type,
7679
});
7780

7881
onUnmounted(() => {

packages/devtools/src/types.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { FieldState as CoreFieldState, FormReturns, IssueCollection } from '@core/index';
2-
import { ComponentInternalInstance } from 'vue';
2+
import { ComponentInternalInstance, MaybeRefOrGetter } from 'vue';
33

44
interface BaseState<TValue = unknown> {
55
touched: boolean;
@@ -37,7 +37,10 @@ export interface FieldState<TValue = unknown> extends BaseState<TValue> {
3737
export type NodeState = FormState | FieldState | PathState;
3838

3939
// Devtools field type
40-
export type DevtoolsField = CoreFieldState<unknown> & { type: string; _vm?: ComponentInternalInstance | null };
40+
export type DevtoolsField = CoreFieldState<unknown> & {
41+
type: MaybeRefOrGetter<string>;
42+
_vm?: ComponentInternalInstance | null;
43+
};
4144

4245
// Devtools form type
4346
export type DevtoolsForm = FormReturns & {

0 commit comments

Comments
 (0)