Skip to content

Commit 48d77a1

Browse files
committed
feat: introduce BuiltInControlTypes for consistent control type
1 parent fbfcd24 commit 48d77a1

File tree

21 files changed

+84
-88
lines changed

21 files changed

+84
-88
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(): typeof BuiltInControlTypes | 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

@@ -338,13 +338,6 @@ export function useCalendarControl(_props: Reactivify<CalendarControlProps, 'fie
338338
};
339339
}, gridEl);
340340

341-
if (__DEV__) {
342-
// If it is its own field, we should register it with devtools.
343-
if (!props.field) {
344-
registerField(field, 'Calendar');
345-
}
346-
}
347-
348341
return {
349342
/**
350343
* 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,
@@ -19,7 +19,6 @@ import { useControlButtonProps } from '../helpers/useControlButtonProps';
1919
import { TransparentWrapper } from '../types';
2020
import { useVModelProxy } from '../reactivity/useVModelProxy';
2121
import { useFieldControllerContext } from '../useFormField/useFieldController';
22-
import { registerField } from '@formwerk/devtools';
2322

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

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

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

packages/core/src/useCustomField/useCustomControl.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export function useCustomControl<TValue = unknown>(
3232
controller?.registerControl({
3333
getControlElement: () => controlEl.value,
3434
getControlId: () => controlId,
35+
getControlType: () => props.controlType ?? 'Custom',
3536
});
3637

3738
const controlProps = useCaptureProps(() => {

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.

packages/core/src/useFormField/useFieldController.ts

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

1414
export interface FieldControllerProps {
@@ -73,6 +73,11 @@ export interface FieldController {
7373
* Registers a control interface, used to get the control element and id.
7474
*/
7575
registerControl: (control: ControlApi) => void;
76+
77+
/**
78+
* The type of the control, used for devtools.
79+
*/
80+
controlType: Ref<typeof BuiltInControlTypes | string>;
7681
}
7782

7883
export const FieldControllerKey: InjectionKey<FieldController> = Symbol('FieldControllerKey');
@@ -82,6 +87,7 @@ export function useFieldController(_props: Reactivify<FieldControllerProps>): Fi
8287
const props = normalizeProps(_props);
8388
const getControlId = () => control.value?.getControlId() ?? '';
8489
const getControlElement = () => control.value?.getControlElement();
90+
const getControlType = () => control.value?.getControlType() ?? 'Field';
8591

8692
const { descriptionProps, describedByProps } = useDescription({
8793
inputId: getControlId,
@@ -104,11 +110,13 @@ export function useFieldController(_props: Reactivify<FieldControllerProps>): Fi
104110
}
105111

106112
const controlId = computed(() => getControlId());
113+
const controlType = computed(() => getControlType());
107114

108115
const controller = {
109116
label: computed(() => toValue(props.label) ?? ''),
110117
labelProps,
111118
controlId,
119+
controlType,
112120
labelledByProps,
113121
descriptionProps,
114122
describedByProps,

0 commit comments

Comments
 (0)