-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
260 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { computed, inject, unref } from 'vue'; | ||
import { FieldContext, FormSymbol } from './symbols'; | ||
import { MaybeReactive } from './types'; | ||
import { getFromPath, injectWithSelf } from './utils'; | ||
|
||
/** | ||
* Gives access to a field's current value | ||
*/ | ||
export function useFieldValue<TValue = any>(path?: MaybeReactive<string>) { | ||
const form = injectWithSelf(FormSymbol); | ||
// We don't want to use self injected context as it doesn't make sense | ||
const field = path ? undefined : inject(FieldContext); | ||
|
||
return computed(() => { | ||
if (path) { | ||
return getFromPath(form?.values, unref(path)) as TValue | undefined; | ||
} | ||
|
||
return field?.value?.value as TValue | undefined; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { computed } from 'vue'; | ||
import { FormSymbol } from './symbols'; | ||
import { FormContext } from './types'; | ||
import { injectWithSelf, warn } from './utils'; | ||
|
||
/** | ||
* Gives access to a form's values | ||
*/ | ||
export function useFormValues<TValues extends Record<string, any> = Record<string, any>>() { | ||
const form = injectWithSelf(FormSymbol) as FormContext<TValues> | undefined; | ||
if (!form) { | ||
warn('No vee-validate <Form /> or `useForm` was detected in the component tree'); | ||
} | ||
|
||
return computed(() => { | ||
return form?.values || ({} as Partial<TValues>); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import flushPromises from 'flush-promises'; | ||
import { useField, useFieldValue, useForm } from '@/vee-validate'; | ||
import { mountWithHoc, setValue } from './helpers'; | ||
import { defineComponent } from 'vue'; | ||
|
||
describe('useFieldValue()', () => { | ||
const REQUIRED_MESSAGE = 'Field is required'; | ||
const validate = (val: any) => (val ? true : REQUIRED_MESSAGE); | ||
|
||
test('gives access to a single field value', async () => { | ||
mountWithHoc({ | ||
setup() { | ||
useForm(); | ||
const { value } = useField('test', validate); | ||
const currValue = useFieldValue('test'); | ||
|
||
return { | ||
value, | ||
currValue, | ||
}; | ||
}, | ||
template: ` | ||
<input name="field" v-model="value" /> | ||
<span>{{ currValue }}</span> | ||
`, | ||
}); | ||
|
||
await flushPromises(); | ||
const input = document.querySelector('input'); | ||
const valueSpan = document.querySelector('span'); | ||
const inputValue = '1234'; | ||
setValue(input as any, inputValue); | ||
await flushPromises(); | ||
expect(valueSpan?.textContent).toBe(inputValue); | ||
}); | ||
|
||
test('gives access to a single field value in a child component with specifying a path', async () => { | ||
const CustomErrorComponent = defineComponent({ | ||
template: '<span>{{ value }}</span>', | ||
setup() { | ||
const value = useFieldValue(); | ||
|
||
return { | ||
value, | ||
}; | ||
}, | ||
}); | ||
mountWithHoc({ | ||
components: { | ||
CustomErrorComponent, | ||
}, | ||
setup() { | ||
useForm(); | ||
const { value } = useField('test', validate); | ||
|
||
return { | ||
value, | ||
}; | ||
}, | ||
template: ` | ||
<input name="field" v-model="value" /> | ||
<CustomErrorComponent /> | ||
`, | ||
}); | ||
|
||
await flushPromises(); | ||
const input = document.querySelector('input'); | ||
const valueSpan = document.querySelector('span'); | ||
const inputValue = '1234'; | ||
setValue(input as any, inputValue); | ||
await flushPromises(); | ||
expect(valueSpan?.textContent).toBe(inputValue); | ||
}); | ||
|
||
test('returns undefined if field not found', async () => { | ||
mountWithHoc({ | ||
setup() { | ||
useForm(); | ||
const value = useFieldValue('something'); | ||
|
||
return { | ||
value, | ||
}; | ||
}, | ||
template: ` | ||
<span>{{ value }}</span> | ||
`, | ||
}); | ||
|
||
await flushPromises(); | ||
const error = document.querySelector('span'); | ||
expect(error?.textContent).toBe(''); | ||
}); | ||
|
||
test('returns undefined if form is not found', async () => { | ||
mountWithHoc({ | ||
setup() { | ||
const value = useFieldValue('something'); | ||
|
||
return { | ||
value, | ||
}; | ||
}, | ||
template: ` | ||
<span>{{ value }}</span> | ||
`, | ||
}); | ||
|
||
await flushPromises(); | ||
const error = document.querySelector('span'); | ||
expect(error?.textContent).toBe(''); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import flushPromises from 'flush-promises'; | ||
import { useField, useForm, useFormValues } from '@/vee-validate'; | ||
import { mountWithHoc, setValue } from './helpers'; | ||
|
||
describe('useFormValues()', () => { | ||
const REQUIRED_MESSAGE = 'Field is required'; | ||
const validate = (val: any) => (val ? true : REQUIRED_MESSAGE); | ||
|
||
test('gives access to all form values', async () => { | ||
mountWithHoc({ | ||
setup() { | ||
useForm(); | ||
const { value } = useField('test', validate); | ||
const values = useFormValues(); | ||
|
||
return { | ||
value, | ||
values, | ||
}; | ||
}, | ||
template: ` | ||
<input name="field" v-model="value" /> | ||
<span>{{ values.test }}</span> | ||
`, | ||
}); | ||
|
||
await flushPromises(); | ||
const input = document.querySelector('input'); | ||
const valueSpan = document.querySelector('span'); | ||
const inputValue = '1234'; | ||
setValue(input as any, inputValue); | ||
await flushPromises(); | ||
expect(valueSpan?.textContent).toBe(inputValue); | ||
}); | ||
|
||
test('returns empty object and warns if form is not found', async () => { | ||
const spy = jest.spyOn(console, 'warn').mockImplementation(); | ||
|
||
mountWithHoc({ | ||
setup() { | ||
const values = useFormValues(); | ||
|
||
return { | ||
values, | ||
}; | ||
}, | ||
template: ` | ||
<span>{{ values }}</span> | ||
`, | ||
}); | ||
|
||
await flushPromises(); | ||
const valuesSpan = document.querySelector('span'); | ||
expect(valuesSpan?.textContent).toBe('{}'); | ||
expect(spy).toHaveBeenCalled(); | ||
spy.mockRestore(); | ||
}); | ||
}); |