/
FieldArray.ts
55 lines (49 loc) · 1.1 KB
/
FieldArray.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { defineComponent, toRef, UnwrapRef, VNode } from 'vue';
import { FieldArrayContext } from './types';
import { useFieldArray } from './useFieldArray';
import { normalizeChildren } from './utils';
const FieldArrayImpl = defineComponent({
name: 'FieldArray',
inheritAttrs: false,
props: {
name: {
type: String,
required: true,
},
},
setup(props, ctx) {
const { push, remove, swap, insert, replace, update, prepend, fields } = useFieldArray(toRef(props, 'name'));
function slotProps() {
return {
fields: fields.value,
push,
remove,
swap,
insert,
update,
replace,
prepend,
};
}
ctx.expose({
push,
remove,
swap,
insert,
update,
replace,
prepend,
});
return () => {
const children = normalizeChildren(undefined, ctx, slotProps);
return children;
};
},
});
export const FieldArray = FieldArrayImpl as typeof FieldArrayImpl & {
new (): {
$slots: {
default: (arg: UnwrapRef<FieldArrayContext>) => VNode[];
};
};
};