Skip to content

Commit 657b595

Browse files
authored
feat: deprecate displayError() (#218)
* feat: deprecate display error * chore: changeset
1 parent a690b89 commit 657b595

File tree

13 files changed

+17
-106
lines changed

13 files changed

+17
-106
lines changed

.changeset/young-taxis-press.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@formwerk/core': minor
3+
---
4+
5+
feat: deprecate displayError

packages/core/src/useCheckbox/useCheckbox.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,6 @@ function createGroupField<TValue = unknown>(group: CheckboxGroupContext<TValue>,
340340
...group.field,
341341
errors: computed(() => []),
342342
errorMessage: computed(() => ''),
343-
displayError: () => undefined,
344343
setValue,
345344
};
346345
}

packages/core/src/useForm/useForm.spec.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,17 +1422,6 @@ describe('form validation', () => {
14221422
expect(getError('test')).toBeUndefined();
14231423
});
14241424

1425-
test('displays errors if the field is touched', async () => {
1426-
const { setTouched, displayError, setErrors } = await renderSetup(() => {
1427-
return useForm();
1428-
});
1429-
1430-
setErrors('test', 'error');
1431-
expect(displayError('test')).toBeUndefined();
1432-
setTouched('test', true);
1433-
expect(displayError('test')).toBe('error');
1434-
});
1435-
14361425
test('when looking up errors for a path, search for prefix matches when direct path is not found', async () => {
14371426
const { ...form } = useForm({
14381427
initialValues: {

packages/core/src/useForm/useForm.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,6 @@ export interface FormReturns<TInput extends FormObject = FormObject, TOutput ext
154154
*/
155155
getErrors: (path?: Path<TInput>) => string[];
156156

157-
/**
158-
* Displays the errors for a form field.
159-
* @param path - The path to display the errors for.
160-
*/
161-
displayError(path: Path<TInput>): string | undefined;
162-
163157
/**
164158
* Get the issues for a form field.
165159
*/
@@ -318,10 +312,6 @@ export function useForm<
318312
return ctx.isPathDisabled(path) ? undefined : ctx.getFieldSubmitErrors(path)[0];
319313
}
320314

321-
function displayError(path: Path<TResolvedInput>) {
322-
return ctx.isTouched(path) && !ctx.isPathDisabled(path) ? getError(path) : undefined;
323-
}
324-
325315
provide(FormKey, {
326316
...ctx,
327317
...transactionsManager,
@@ -370,7 +360,6 @@ export function useForm<
370360
setErrors: ctx.setErrors,
371361
setValues: ctx.setValues,
372362
getError,
373-
displayError,
374363
getErrors: ctx.getErrors,
375364
getIssues: ctx.getIssues,
376365
getSubmitError,

packages/core/src/useFormField/useErrorDisplay.spec.ts

Lines changed: 0 additions & 39 deletions
This file was deleted.

packages/core/src/useFormField/useErrorDisplay.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

packages/core/src/useFormField/useFormField.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
isLowPriority,
1313
} from '../utils/common';
1414
import { FormGroupKey } from '../useFormGroup';
15-
import { useErrorDisplay } from './useErrorDisplay';
1615
import { usePathPrefixer } from '../helpers/usePathPrefixer';
1716
import { createDisabledContext } from '../helpers/createDisabledContext';
1817

@@ -44,7 +43,6 @@ export type FormField<TValue> = {
4443
setValue: (value: TValue | undefined) => void;
4544
setTouched: (touched: boolean) => void;
4645
setErrors: (messages: Arrayable<string>) => void;
47-
displayError: () => string | undefined;
4846
form?: FormContext | null;
4947
};
5048

@@ -64,8 +62,6 @@ export function useFormField<TValue = unknown>(opts?: Partial<FormFieldOptions<T
6462
const { errors, setErrors, isValid, errorMessage, pathlessValidity, submitErrors, submitErrorMessage } =
6563
useFieldValidity(getPath, isDisabled, form);
6664

67-
const { displayError } = useErrorDisplay(errorMessage, isTouched);
68-
6965
const isDirty = computed(() => {
7066
if (!form) {
7167
return !isEqual(fieldValue.value, initialValue);
@@ -139,7 +135,6 @@ export function useFormField<TValue = unknown>(opts?: Partial<FormFieldOptions<T
139135
setValue,
140136
setTouched,
141137
setErrors,
142-
displayError,
143138
submitErrors,
144139
submitErrorMessage,
145140
};
@@ -410,11 +405,6 @@ function createLocalValidity() {
410405
}
411406

412407
export type ExposedField<TValue> = {
413-
/**
414-
* Display the error message for the field.
415-
*/
416-
displayError: () => string | undefined;
417-
418408
/**
419409
* The error message for the field.
420410
*/
@@ -484,7 +474,6 @@ export function exposeField<TReturns extends object, TValue>(
484474
field: FormField<TValue>,
485475
): ExposedField<TValue> & TReturns {
486476
const exposedField = {
487-
displayError: field.displayError,
488477
errorMessage: field.errorMessage,
489478
errors: field.errors,
490479
submitErrors: field.submitErrors,

packages/core/src/useFormGroup/useFormGroup.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,6 @@ export function useFormGroup<TInput extends FormObject = FormObject, TOutput ext
148148
return form?.getErrors(prefixPath(path) ?? '')?.[0];
149149
}
150150

151-
function displayError(name: string) {
152-
const msg = getError(name);
153-
const path = prefixPath(name) ?? '';
154-
155-
return form?.isTouched(path) ? msg : undefined;
156-
}
157-
158151
function prefixPath(path: string | undefined) {
159152
return _prefixPath(getPath(), path);
160153
}
@@ -216,10 +209,7 @@ export function useFormGroup<TInput extends FormObject = FormObject, TOutput ext
216209
* Whether the form group is disabled.
217210
*/
218211
isDisabled,
219-
/**
220-
* Displays an error for a given field.
221-
*/
222-
displayError,
212+
223213
/**
224214
* Validates the form group.
225215
*/

packages/core/src/useSelect/useSelect.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ function createSelect() {
5555
listBoxProps,
5656
errorMessageProps,
5757
descriptionProps,
58-
displayError,
5958
fieldValue,
6059
selectedOptions,
6160
selectedOption,
61+
errorMessage,
6262
} = useSelect(all);
6363

6464
exposedSelectedOptions = selectedOptions;
@@ -77,12 +77,12 @@ function createSelect() {
7777
listBoxProps,
7878
errorMessageProps,
7979
descriptionProps,
80-
displayError,
8180
getValue,
8281
groups,
8382
options,
8483
selectedOptions,
8584
selectedOption,
85+
errorMessage,
8686
};
8787
},
8888
template: `
@@ -128,7 +128,7 @@ function createSelect() {
128128
</div>
129129
130130
<span v-bind="errorMessageProps">
131-
{{ displayError() }}
131+
{{ errorMessage }}
132132
</span>
133133
</div>
134134
`,

packages/playground/src/components/FormGroup.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
{{ { isDirty, isTouched } }}
88

99
<div class="flex flex-col gap-2 p-8">
10-
<slot :display-error="displayError" :get-error="getError" :isTouched="isTouched" />
10+
<slot :get-error="getError" :isTouched="isTouched" />
1111
</div>
1212
</div>
1313
</template>
@@ -17,5 +17,5 @@ import { FormGroupProps, useFormGroup } from '@formwerk/core';
1717
1818
const props = defineProps<FormGroupProps>();
1919
20-
const { labelProps, groupProps, getError, displayError, isDirty, isTouched } = useFormGroup(props);
20+
const { labelProps, groupProps, getError, isDirty, isTouched } = useFormGroup(props);
2121
</script>

0 commit comments

Comments
 (0)