Skip to content
Source
Sign inSign up
TanStack/form
Open History to see the last commit

fieldView.lib.ts

52 lines1.8 KB

1import type { AnyFieldApi, AnyFieldMeta } from '@tanstack/form-core'
2import type { AnyInternalFieldApi } from '@tanstack/form-core/internals'
3import type { ShallowRef } from 'vue'
4​
5/**
6 * The public field shared by slots and injected components. State getters
7 * track the existing subscription in the component that reads them, while
8 * handlers follow the current core field after a reset or name change.
9 */
10export function createFieldView(
11 fieldApi: ShallowRef<AnyInternalFieldApi>,
12 selection: ShallowRef<unknown>,
13 meta: ShallowRef<AnyFieldMeta>,
14): AnyFieldApi {
15 return {
16 get form() {
17 return fieldApi.value.form
18 },
19 get name() {
20 return fieldApi.value.name
21 },
22 get atom() {
23 return fieldApi.value.atom
24 },
25 get value() {
26 void selection.value
27 return fieldApi.value.value
28 },
29 get meta() {
30 return meta.value
31 },
32 get errors() {
33 return meta.value.errors
34 },
35 handleChange: (value, options) =>
36 fieldApi.value.handleChange(value, options),
37 handleBlur: () => fieldApi.value.handleBlur(),
38 reset: () => fieldApi.value.reset(),
39 swapValues: (indexA, indexB) => fieldApi.value.swapValues(indexA, indexB),
40 moveValue: (fromIndex, toIndex, options) =>
41 fieldApi.value.moveValue(fromIndex, toIndex, options),
42 pushValue: (value, options) => fieldApi.value.pushValue(value, options),
43 insertValue: (index, value, options) =>
44 fieldApi.value.insertValue(index, value, options),
45 clearValues: (options) => fieldApi.value.clearValues(options),
46 replaceValue: (index, value, options) =>
47 fieldApi.value.replaceValue(index, value, options),
48 removeValue: (index, options) => fieldApi.value.removeValue(index, options),
49 filterValues: (predicate, options) =>
50 fieldApi.value.filterValues(predicate, options),
51 }