Skip to content

Commit 58cfafe

Browse files
genulogaretm
andauthored
chore: register with devtools in useFormField (#214)
* chore: register with devtools in `useFormField` * refactor: update registerField to accept Ref type and use * feat: introduce BuiltInControlTypes for consistent control type * chore: fix typo * fix: types and type reactivity in devtools --------- Co-authored-by: Abdelrahman Awad <[email protected]>
1 parent 040b78a commit 58cfafe

File tree

26 files changed

+122
-119
lines changed

26 files changed

+122
-119
lines changed

packages/core/src/types/controls.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { StandardSchema } from './forms';
44
export interface ControlApi {
55
getControlElement(): HTMLElement | undefined;
66
getControlId(): string | undefined;
7+
getControlType(): string | undefined;
78
}
89

910
export interface ControlProps<TValue = unknown, TInitialValue = TValue> {
@@ -42,3 +43,23 @@ export interface ControlProps<TValue = unknown, TInitialValue = TValue> {
4243
*/
4344
required?: boolean;
4445
}
46+
47+
export const BuiltInControlTypes = {
48+
Text: 'Text',
49+
Calendar: 'Calendar',
50+
Date: 'Date',
51+
Time: 'Time',
52+
File: 'File',
53+
Select: 'Select',
54+
Number: 'Number',
55+
OTP: 'OTP',
56+
Slider: 'Slider',
57+
Switch: 'Switch',
58+
Checkbox: 'Checkbox',
59+
CheckboxGroup: 'CheckboxGroup',
60+
RadioGroup: 'RadioGroup',
61+
ComboBox: 'ComboBox',
62+
Hidden: 'Hidden',
63+
Search: 'Search',
64+
Custom: 'Custom',
65+
} as const;

packages/core/src/useCalendar/useCalendarControl.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { computed, inject, nextTick, provide, Ref, shallowRef, toValue, watch } from 'vue';
22
import { CalendarContext, CalendarViewType } from './types';
33
import { hasKeyCode, normalizeProps, useCaptureProps, useUniqId } from '../utils/common';
4-
import { ControlProps, Maybe, Reactivify } from '../types';
4+
import { BuiltInControlTypes, ControlProps, Maybe, Reactivify } from '../types';
55
import { useLocale } from '../i18n';
66
import { FieldTypePrefixes } from '../constants';
77
import { blockEvent } from '../utils/events';
@@ -17,7 +17,6 @@ import { createDisabledContext } from '../helpers/createDisabledContext';
1717
import { useVModelProxy } from '../reactivity/useVModelProxy';
1818
import { useConstraintsValidator, useInputValidity } from '../validation';
1919
import { useFieldControllerContext } from '../useFormField/useFieldController';
20-
import { registerField } from '@formwerk/devtools';
2120

2221
export interface CalendarControlProps extends ControlProps<Date | undefined> {
2322
/**
@@ -128,6 +127,7 @@ export function useCalendarControl(_props: Reactivify<CalendarControlProps, 'fie
128127
controller?.registerControl({
129128
getControlId: () => calendarId,
130129
getControlElement: () => calendarEl.value,
130+
getControlType: () => BuiltInControlTypes.Calendar,
131131
});
132132
}
133133

@@ -340,13 +340,6 @@ export function useCalendarControl(_props: Reactivify<CalendarControlProps, 'fie
340340
};
341341
}, gridEl);
342342

343-
if (__DEV__) {
344-
// If it is its own field, we should register it with devtools.
345-
if (!props.field) {
346-
registerField(field, 'Calendar');
347-
}
348-
}
349-
350343
return {
351344
/**
352345
* The id of the calendar element.

packages/core/src/useCheckbox/useCheckbox.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { computed, inject, nextTick, toValue, shallowRef } from 'vue';
2-
import { registerField } from '@formwerk/devtools';
32
import { hasKeyCode, isEqual, isInputElement, normalizeProps, useUniqId, useCaptureProps } from '../utils/common';
43
import {
54
AriaLabelableProps,
@@ -274,10 +273,6 @@ export function useCheckbox<TValue = string>(_props: Reactivify<CheckboxProps<TV
274273

275274
const isGrouped = !!group;
276275

277-
if (__DEV__) {
278-
registerField(field.state, 'Checkbox');
279-
}
280-
281276
return exposeField(
282277
{
283278
/**

packages/core/src/useCheckbox/useCheckboxGroup.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
Arrayable,
1010
StandardSchema,
1111
ControlProps,
12+
BuiltInControlTypes,
1213
} from '../types';
1314
import {
1415
useUniqId,
@@ -119,6 +120,7 @@ export function useCheckboxGroup<TCheckbox>(_props: Reactivify<CheckboxGroupProp
119120
field.registerControl({
120121
getControlId: () => groupId,
121122
getControlElement: () => undefined,
123+
getControlType: () => BuiltInControlTypes.CheckboxGroup,
122124
});
123125

124126
const { fieldValue, setValue, isTouched, setTouched, isDisabled } = field.state;

packages/core/src/useComboBox/useComboBoxControl.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ref, toValue, watch, shallowRef, computed } from 'vue';
2-
import { ControlProps, InputEvents, Maybe, Reactivify } from '../types';
2+
import { BuiltInControlTypes, ControlProps, InputEvents, Maybe, Reactivify } from '../types';
33
import { Orientation } from '../types';
44
import {
55
debounce,
@@ -18,7 +18,6 @@ import { FilterFn } from '../collections';
1818
import { useControlButtonProps } from '../helpers/useControlButtonProps';
1919
import { useVModelProxy } from '../reactivity/useVModelProxy';
2020
import { useFieldControllerContext } from '../useFormField/useFieldController';
21-
import { registerField } from '@formwerk/devtools';
2221

2322
export interface ComboBoxControlProps<TOption, TValue = TOption> extends ControlProps<TValue> {
2423
/**
@@ -79,6 +78,7 @@ export function useComboBoxControl<TOption, TValue = TOption>(
7978
controller?.registerControl({
8079
getControlElement: () => inputEl.value,
8180
getControlId: () => inputId,
81+
getControlType: () => BuiltInControlTypes.ComboBox,
8282
});
8383

8484
const {
@@ -318,10 +318,6 @@ export function useComboBoxControl<TOption, TValue = TOption>(
318318
watch(inputValue, debounce(filter.debounceMs, updateHiddenState));
319319
}
320320

321-
if (__DEV__) {
322-
registerField(field, 'ComboBox');
323-
}
324-
325321
return {
326322
/**
327323
* The id of the input element.

packages/core/src/useCustomField/useCustomControl.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { resolveFieldState } from '../useFormField';
55
import { normalizeProps, propsToValues, useUniqId, useCaptureProps } from '../utils/common';
66
import { useInputValidity } from '../validation';
77
import { useFieldControllerContext } from '../useFormField/useFieldController';
8-
import { registerField } from '@formwerk/devtools';
98

109
export interface CustomControlProps<TValue = unknown> extends ControlProps<TValue> {
1110
/**
@@ -33,6 +32,7 @@ export function useCustomControl<TValue = unknown>(
3332
controller?.registerControl({
3433
getControlElement: () => controlEl.value,
3534
getControlId: () => controlId,
35+
getControlType: () => props.controlType ?? 'Custom',
3636
});
3737

3838
const controlProps = useCaptureProps(() => {
@@ -47,10 +47,6 @@ export function useCustomControl<TValue = unknown>(
4747
};
4848
}, controlEl);
4949

50-
if (__DEV__) {
51-
registerField(field, props.controlType ?? 'Custom');
52-
}
53-
5450
return {
5551
/**
5652
* The id of the control element.

packages/core/src/useCustomField/useCustomField.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { toValue } from 'vue';
12
import { Reactivify } from '../types';
23
import { exposeField, getFieldInit, useFormField, WithFieldProps } from '../useFormField';
34
import { normalizeProps } from '../utils/common';
@@ -7,7 +8,7 @@ export type CustomFieldProps<TValue = unknown> = WithFieldProps<CustomControlPro
78

89
export function useCustomField<TValue = unknown>(_props: Reactivify<CustomFieldProps<TValue>, 'schema'>) {
910
const props = normalizeProps(_props, ['schema']);
10-
const field = useFormField(getFieldInit<TValue>(props));
11+
const field = useFormField(getFieldInit<TValue>(props), toValue(props.controlType) ?? 'Custom Field');
1112
const control = useCustomControl<TValue>({
1213
...(props as CustomControlProps<TValue>),
1314
_field: field,

packages/core/src/useDateTime/useDateControl.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ControlProps, Reactivify } from '../types';
1+
import { BuiltInControlTypes, ControlProps, Reactivify } from '../types';
22
import type { CalendarProps } from '../useCalendar';
33
import { normalizeProps, useUniqId, useCaptureProps } from '../utils/common';
44
import { computed, shallowRef, toValue } from 'vue';
@@ -12,7 +12,6 @@ import { useInputValidity } from '../validation';
1212
import { useConstraintsValidator } from '../validation/useConstraintsValidator';
1313
import { useVModelProxy } from '../reactivity/useVModelProxy';
1414
import { useFieldControllerContext } from '../useFormField/useFieldController';
15-
import { registerField } from '@formwerk/devtools';
1615

1716
export interface DateControlProps extends ControlProps<Date | undefined> {
1817
/**
@@ -84,6 +83,7 @@ export function useDateControl(_props: Reactivify<DateControlProps, '_field' | '
8483
controller?.registerControl({
8584
getControlElement: () => controlEl.value,
8685
getControlId: () => controlId,
86+
getControlType: () => BuiltInControlTypes.Date,
8787
});
8888

8989
const min = computed(() => fromDateToCalendarZonedDateTime(toValue(props.min), calendar.value, timeZone.value));
@@ -146,10 +146,6 @@ export function useDateControl(_props: Reactivify<DateControlProps, '_field' | '
146146
};
147147
}, controlEl);
148148

149-
if (__DEV__) {
150-
registerField(field, 'Date');
151-
}
152-
153149
return {
154150
/**
155151
* The id of the control element.

packages/core/src/useDateTime/useTimeControl.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ControlProps, Maybe, Reactivify } from '../types';
1+
import { BuiltInControlTypes, ControlProps, Maybe, Reactivify } from '../types';
22
import { isNullOrUndefined, normalizeProps, useUniqId, useCaptureProps } from '../utils/common';
33
import { computed, shallowRef, toValue } from 'vue';
44
import { resolveFieldState } from '../useFormField';
@@ -14,7 +14,6 @@ import { merge } from '../../../shared/src';
1414
import { Simplify } from 'type-fest';
1515
import { useVModelProxy } from '../reactivity/useVModelProxy';
1616
import { useFieldControllerContext } from '../useFormField/useFieldController';
17-
import { registerField } from '@formwerk/devtools';
1817

1918
export type TimeFormatOptions = Simplify<
2019
Pick<Intl.DateTimeFormatOptions, 'hour' | 'minute' | 'second' | 'dayPeriod' | 'timeZone' | 'hour12'>
@@ -88,6 +87,7 @@ export function useTimeControl(_props: Reactivify<TimeControlProps, '_field' | '
8887
controller?.registerControl({
8988
getControlElement: () => controlEl.value,
9089
getControlId: () => controlId,
90+
getControlType: () => BuiltInControlTypes.Time,
9191
});
9292

9393
const temporalValue = useTemporalStore({
@@ -128,10 +128,6 @@ export function useTimeControl(_props: Reactivify<TimeControlProps, '_field' | '
128128
};
129129
}, controlEl);
130130

131-
if (__DEV__) {
132-
registerField(field, 'Time');
133-
}
134-
135131
return {
136132
/**
137133
* The id of the control element.

packages/core/src/useFileField/useFileControl.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { computed, markRaw, nextTick, provide, readonly, ref, toValue, shallowRef } from 'vue';
2-
import { Arrayable, ControlProps, Reactivify } from '../types';
2+
import { Arrayable, BuiltInControlTypes, ControlProps, Reactivify } from '../types';
33
import {
44
isNullOrUndefined,
55
normalizeArrayable,
@@ -18,7 +18,6 @@ import { FileEntryCollectionKey, FilePickerOptions } from './types';
1818
import { useControlButtonProps } from '../helpers/useControlButtonProps';
1919
import { useVModelProxy } from '../reactivity/useVModelProxy';
2020
import { useFieldControllerContext } from '../useFormField/useFieldController';
21-
import { registerField } from '@formwerk/devtools';
2221

2322
export interface FileUploadContext {
2423
/**
@@ -91,6 +90,7 @@ export function useFileControl(_props: Reactivify<FileControlProps, 'onUpload' |
9190
controller?.registerControl({
9291
getControlElement: () => inputEl.value,
9392
getControlId: () => inputId,
93+
getControlType: () => BuiltInControlTypes.File,
9494
});
9595

9696
function isMultiple() {
@@ -339,10 +339,6 @@ export function useFileControl(_props: Reactivify<FileControlProps, 'onUpload' |
339339
getRemoveButtonLabel: () => toValue(props.removeButtonLabel) ?? 'Remove file',
340340
});
341341

342-
if (__DEV__) {
343-
registerField(field, 'File');
344-
}
345-
346342
return {
347343
/**
348344
* The id of the input element.

0 commit comments

Comments
 (0)