Skip to content

Commit

Permalink
refactor: simplify dev-tools registration by removing formId parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
s8n11c authored and logaretm committed Feb 22, 2025
1 parent 7be32b5 commit 26cecb7
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 61 deletions.
21 changes: 3 additions & 18 deletions packages/core/src/useCheckbox/useCheckbox.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { computed, inject, nextTick, Ref, ref, toValue, watch } from 'vue';
import { computed, inject, nextTick, Ref, ref, toValue } from 'vue';
import { hasKeyCode, isEqual, isInputElement, normalizeProps, useUniqId, withRefCapture } from '../utils/common';
import {
AriaLabelableProps,
Expand All @@ -13,8 +13,7 @@ import { CheckboxGroupContext, CheckboxGroupKey } from './useCheckboxGroup';
import { useFormField, exposeField, FormField } from '../useFormField';
import { FieldTypePrefixes } from '../constants';
import { useInputValidity } from '../validation';
import { refreshInspector, registerCheckboxWithDevtools } from '@dev-tools/index';
import { FormKey } from '@core/useForm';
import { registerCheckboxWithDevtools } from '@dev-tools/index';

export interface CheckboxProps<TValue = string> {
/**
Expand Down Expand Up @@ -102,8 +101,6 @@ export function useCheckbox<TValue = string>(
_props: Reactivify<CheckboxProps<TValue>, 'schema'>,
elementRef?: Ref<HTMLElement | undefined>,
) {
const form = inject(FormKey, null);

const props = normalizeProps(_props, ['schema']);
const inputId = useUniqId(FieldTypePrefixes.Checkbox);
const getTrueValue = createTrueValueGetter(props);
Expand Down Expand Up @@ -314,19 +311,7 @@ export function useCheckbox<TValue = string>(

if (__DEV__) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
registerCheckboxWithDevtools({ ...exposedField, ...field } as any, form?.id);

watch(
() => ({
errors: errorMessage.value,
isValid: !errorMessage.value,
value: fieldValue.value,
}),
refreshInspector,
{
deep: true,
},
);
registerCheckboxWithDevtools({ ...exposedField, ...field } as any);
}

return exposedField;
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/useFormField/useFormField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export type FormField<TValue> = {
setTouched: (touched: boolean) => void;
setErrors: (messages: Arrayable<string>) => void;
displayError: () => string | undefined;
form?: FormContext | null;
};

export function useFormField<TValue = unknown>(opts?: Partial<FormFieldOptions<TValue>>): FormField<TValue> {
Expand Down Expand Up @@ -200,7 +201,7 @@ export function useFormField<TValue = unknown>(opts?: Partial<FormFieldOptions<T
form.setFieldDisabled(path, disabled);
});

return field;
return { ...field, form };
}

function useFieldValidity(getPath: Getter<string | undefined>, isDisabled: Ref<boolean>, form?: FormContext | null) {
Expand Down
21 changes: 3 additions & 18 deletions packages/core/src/useRadio/useRadioGroup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { InjectionKey, toValue, computed, onBeforeUnmount, reactive, provide, ref, inject, watch } from 'vue';
import { InjectionKey, toValue, computed, onBeforeUnmount, reactive, provide, ref } from 'vue';
import { useInputValidity } from '../validation/useInputValidity';
import { useLabel, useErrorMessage } from '../a11y';
import {
Expand All @@ -22,8 +22,7 @@ import {
import { useLocale } from '../i18n';
import { useFormField, exposeField } from '../useFormField';
import { FieldTypePrefixes } from '../constants';
import { FormKey } from '@core/useForm';
import { refreshInspector, registerRadioWithDevtools } from '@dev-tools/index';
import { registerRadioWithDevtools } from '@dev-tools/index';

export interface RadioGroupContext<TValue> {
name: string;
Expand Down Expand Up @@ -129,8 +128,6 @@ function getOrientationArrows(dir: Direction | undefined) {
}

export function useRadioGroup<TValue = string>(_props: Reactivify<RadioGroupProps<TValue>, 'schema'>) {
const form = inject(FormKey, null);

const props = normalizeProps(_props, ['schema']);
const groupId = useUniqId(FieldTypePrefixes.RadioButtonGroup);
const { direction } = useLocale();
Expand Down Expand Up @@ -300,19 +297,7 @@ export function useRadioGroup<TValue = string>(_props: Reactivify<RadioGroupProp

if (__DEV__) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
registerRadioWithDevtools({ ...exposedField, ...field } as any, form?.id);

watch(
() => ({
errors: errorMessage.value,
isValid: !errorMessage.value,
value: fieldValue.value,
}),
refreshInspector,
{
deep: true,
},
);
registerRadioWithDevtools({ ...exposedField, ...field } as any);
}

return exposedField;
Expand Down
21 changes: 3 additions & 18 deletions packages/core/src/useTextField/useTextField.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Ref, computed, inject, shallowRef, toValue, watch } from 'vue';
import { Ref, computed, shallowRef, toValue } from 'vue';
import { createDescribedByProps, normalizeProps, propsToValues, useUniqId, withRefCapture } from '../utils/common';
import {
AriaDescribableProps,
Expand All @@ -14,8 +14,7 @@ import { useLabel, useErrorMessage } from '../a11y';
import { useFormField, exposeField } from '../useFormField';
import { FieldTypePrefixes } from '../constants';
import { StandardSchema } from '../types';
import { refreshInspector, registerTextFieldWithDevtools } from '@dev-tools/index';
import { FormKey } from '@core/useForm';
import { registerTextFieldWithDevtools } from '@dev-tools/index';

export type TextInputDOMType = 'text' | 'password' | 'email' | 'number' | 'tel' | 'url';

Expand Down Expand Up @@ -118,8 +117,6 @@ export function useTextField(
_props: Reactivify<TextFieldProps, 'schema'>,
elementRef?: Ref<HTMLInputElement | HTMLTextAreaElement>,
) {
const form = inject(FormKey, null);

const props = normalizeProps(_props, ['schema']);
const inputId = useUniqId(FieldTypePrefixes.TextField);
const inputEl = elementRef || shallowRef<HTMLInputElement>();
Expand Down Expand Up @@ -212,19 +209,7 @@ export function useTextField(
);

if (__DEV__) {
registerTextFieldWithDevtools({ ...exposedField, ...field }, form?.id);

watch(
() => ({
errors: errorMessage.value,
isValid: !errorMessage.value,
value: fieldValue.value,
}),
refreshInspector,
{
deep: true,
},
);
registerTextFieldWithDevtools({ ...exposedField, ...field });
}

return exposedField;
Expand Down
49 changes: 43 additions & 6 deletions packages/dev-tools/src/registery.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { initDevTools, refreshInspector } from './init';
import { onUnmounted } from 'vue';
import { onUnmounted, watch } from 'vue';
import { CheckboxField, FIELD_TYPES, FormContext, RadioField, TextField } from './types';
import { DEVTOOLS_FIELDS, DEVTOOLS_FORMS } from './storage';

export function registerTextFieldWithDevtools(field: Omit<TextField, 'type'>, formId?: string) {
export function registerTextFieldWithDevtools(field: Omit<TextField, 'type'>) {
const vm = initDevTools();

const id = field.getPath() ?? field.getName() ?? '';
const formId = field.form?.id;

// if the field is part of a form, we need to register to add the field to the form
if (formId && DEVTOOLS_FORMS[formId]) {
Expand All @@ -21,6 +22,18 @@ export function registerTextFieldWithDevtools(field: Omit<TextField, 'type'>, fo
DEVTOOLS_FIELDS[id] = { ...field, type: FIELD_TYPES.TextField, _vm: vm };
}

watch(
() => ({
errors: field.errorMessage.value,
isValid: !field.errorMessage.value,
value: field.fieldValue.value,
}),
refreshInspector,
{
deep: true,
},
);

onUnmounted(() => {
delete DEVTOOLS_FIELDS[id];
refreshInspector();
Expand All @@ -29,11 +42,11 @@ export function registerTextFieldWithDevtools(field: Omit<TextField, 'type'>, fo
refreshInspector();
}

export function registerCheckboxWithDevtools(field: Omit<CheckboxField, 'type'>, formId?: string) {
export function registerCheckboxWithDevtools(field: Omit<CheckboxField, 'type'>) {
const vm = initDevTools();

const id = field.getPath() ?? field.getName() ?? '';

const formId = field.form?.id;
// if the field is part of a form, we need to register to add the field to the form
if (formId && DEVTOOLS_FORMS[formId]) {
DEVTOOLS_FORMS[formId].children = DEVTOOLS_FORMS[formId].children ?? [];
Expand All @@ -47,6 +60,18 @@ export function registerCheckboxWithDevtools(field: Omit<CheckboxField, 'type'>,
DEVTOOLS_FIELDS[id] = { ...field, type: FIELD_TYPES.Checkbox, _vm: vm };
}

watch(
() => ({
errors: field.errors.value,
isValid: !field.errorMessage.value,
value: field.fieldValue.value,
}),
refreshInspector,
{
deep: true,
},
);

onUnmounted(() => {
delete DEVTOOLS_FIELDS[id];
refreshInspector();
Expand All @@ -55,11 +80,11 @@ export function registerCheckboxWithDevtools(field: Omit<CheckboxField, 'type'>,
refreshInspector();
}

export function registerRadioWithDevtools(field: Omit<RadioField, 'type'>, formId?: string) {
export function registerRadioWithDevtools(field: Omit<RadioField, 'type'>) {
const vm = initDevTools();

const id = field.getPath() ?? field.getName() ?? '';

const formId = field.form?.id;
// if the field is part of a form, we need to register to add the field to the form
if (formId && DEVTOOLS_FORMS[formId]) {
DEVTOOLS_FORMS[formId].children = DEVTOOLS_FORMS[formId].children ?? [];
Expand All @@ -73,6 +98,18 @@ export function registerRadioWithDevtools(field: Omit<RadioField, 'type'>, formI
DEVTOOLS_FIELDS[id] = { ...field, type: FIELD_TYPES.Radio, _vm: vm };
}

watch(
() => ({
errors: field.errorMessage.value,
isValid: !field.errorMessage.value,
value: field.fieldValue.value,
}),
refreshInspector,
{
deep: true,
},
);

onUnmounted(() => {
delete DEVTOOLS_FIELDS[id];
refreshInspector();
Expand Down

0 comments on commit 26cecb7

Please sign in to comment.