Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x 1x 1x 6x 6x 6x 1x 1x 1x 1x 1x 1x 7x 7x 7x 8x 8x 8x 8x 1x 1x 8x 8x 8x 7x 7x 1x 1x 6x 7x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | <template> <form ref="smartFormRef" :label-width="labelWidth" :label-position="_labelPosition" :model="modelValue" @submit.prevent> <ItemRender v-for="item in formSchema" :key="item.key" :ref="(e) => setItems(e, item.key)" :item="item" :disabled="disabled" :model-value="(modelValue as any)[item.key]" :light="light" @update:model-value="(e, modelKey) => updateItem(modelKey ?? item.key, e)"> <template #label="data"> <slot name="label" v-bind="data" /> </template> <template #append="data"> <slot name="append" v-bind="data" /> </template> </ItemRender> </form> </template> <script setup lang="ts"> import { FormItemData, FormSchema, ObjectModel } from './types' import { LabelPoistion, provideFormState } from './inject' import ItemRender from './ItemRender.vue' import { ref, computed, reactive } from 'vue' import type { Rules } from 'async-validator' defineOptions({ name: 'SmartForm', }) const props = withDefaults( defineProps<{ formSchema: FormSchema modelValue?: ObjectModel disabled?: boolean labelWidth?: string | number labelPosition?: LabelPoistion itemClass?: string /** * 如果为真,不会自动更新model * @default false */ light?: boolean rules?: Rules }>(), { disabled: false, light: false, modelValue: (() => ({})), } ) const emit = defineEmits<{ ( name: 'itemChange', key: string, value: any, prev: any, itemData: FormItemData, ctx?: any ): void }>() const smartFormRef = ref<any>(null) // const getLabel = createGetLabel(id) const _labelPosition = computed(() => props.labelPosition) provideFormState({ emit: emit as any, el: smartFormRef, labelWidth: computed(() => typeof props.labelWidth === 'string' ? props.labelWidth : `${props.labelWidth ?? 60}px` ), labelPosition: computed(() => props.labelPosition ?? 'left'), itemClass: computed(() => props.itemClass), modelValue: computed(() => props.modelValue), rules: computed(() => props.rules ?? {}), }) const items = ref<Record<string, InstanceType<typeof ItemRender>>>({}) const setItems = (e: any, p: string) => { items.value[p] = e } const updateItem = (key: string, v: any) => { // eslint-disable-next-line vue/no-mutating-props props.modelValue[key] = v } /** * 获取结果 * @param dirty 是否只获取修改过的 @default false */ const getResult = async (dirty = false) => { const errors: Error[] = [] const res = {} for (const item of Object.entries(items.value)) { const [k, e] = item if (dirty && !e.model.$dirty) return res const r = await e.getResult() if (e.model.$anyInvalid) { errors.push(new Error(e.model.$errors[0])) } Object.assign(res, r) } if (errors.length) { throw errors[0] } return res } /** * 重制dirtry */ const reset = () => { Object.values(items.value).forEach((e) => e.reset()) } defineExpose( reactive({ getResult, items, reset, }) ) </script> |