Skip to content

Commit

Permalink
feat: add label support to defineField closes #4530
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed Nov 26, 2023
1 parent 3df690e commit a9089bd
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/eight-adults-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'vee-validate': patch
---

feat: add label support to defineField closes #4530
1 change: 1 addition & 0 deletions packages/vee-validate/src/types/forms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ export interface BaseFieldProps {
export interface InputBindsConfig<TValue = unknown, TExtraProps extends GenericObject = GenericObject> {
props: (state: PublicPathState<TValue>) => TExtraProps;
validateOnBlur: boolean;
label: MaybeRefOrGetter<string>;
validateOnChange: boolean;
validateOnInput: boolean;
validateOnModelUpdate: boolean;
Expand Down
4 changes: 3 additions & 1 deletion packages/vee-validate/src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,9 @@ export function useForm<
path: MaybeRefOrGetter<TPath>,
config?: Partial<InputBindsConfig<TValue, TExtras>> | LazyInputBindsConfig<TValue, TExtras>,
) {
const pathState = (findPathState(toValue(path)) || createPathState(path)) as PathState<TValue>;
const label = isCallable(config) ? undefined : config?.label;
console.log(label);
const pathState = (findPathState(toValue(path)) || createPathState(path, { label })) as PathState<TValue>;
const evalConfig = () => (isCallable(config) ? config(omit(pathState, PRIVATE_PATH_STATE_KEYS)) : config || {});

function onBlur() {
Expand Down
8 changes: 3 additions & 5 deletions packages/vee-validate/tests/useFieldArray.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,11 @@ test('warns when updating a no-longer existing item', async () => {
});

const { remove, fields } = useFieldArray('users');
onMounted(() => {
onMounted(async () => {
const item = fields.value[0];
remove(0);

nextTick(() => {
item.value = 'test';
});
await nextTick();
item.value = 'test';
});
},
template: `
Expand Down
36 changes: 35 additions & 1 deletion packages/vee-validate/tests/useForm.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FieldMeta, FormContext, FormMeta, useField, useForm } from '@/vee-validate';
import { FieldMeta, FormContext, FormMeta, useField, useForm, defineRule, configure } from '@/vee-validate';
import { mountWithHoc, setValue, flushPromises, dispatchEvent } from './helpers';
import * as yup from 'yup';
import { onMounted, ref, Ref } from 'vue';
Expand Down Expand Up @@ -1066,6 +1066,40 @@ describe('useForm()', () => {
await flushPromises();
expect(document.body.innerHTML).toContain('aria-valid="true"');
});

test('can specify a label', async () => {
defineRule('required', (value: string) => {
return !!value;
});

configure({
generateMessage: ({ field }) => `${field} is bad`,
});

mountWithHoc({
setup() {
const { defineField, values, errors } = useForm({
validationSchema: {
name: 'required',
},
});

const [model, props] = defineField('name', { validateOnInput: true, label: 'First Name' });

return { model, props, values, errors };
},
template: `
<input v-model="model" v-bind="props" />
<span id="errors">{{ errors.name }}</span>
`,
});

await flushPromises();
const errorEl = document.getElementById('errors');
setValue(document.querySelector('input') as any, '');
await flushPromises();
expect(errorEl?.textContent).toBe('First Name is bad');
});
});

// #4341
Expand Down

0 comments on commit a9089bd

Please sign in to comment.