10 changed files with 158 additions and 78 deletions
@ -0,0 +1,39 @@
@@ -0,0 +1,39 @@
|
||||
<script setup lang="ts"> |
||||
import { NColorPicker, NSlider, NSwitch } from 'naive-ui' |
||||
|
||||
interface IBuilderProps { |
||||
type: string |
||||
propsOptions: any |
||||
context?: any |
||||
} |
||||
const modelValue = defineModel() |
||||
|
||||
const componentMap: Record<string, Component | undefined> = { |
||||
slider: NSlider, |
||||
switch: NSwitch, |
||||
colorPicker: NColorPicker, |
||||
} |
||||
|
||||
const props = defineProps<IBuilderProps>() |
||||
|
||||
const realProps = computed(() => { |
||||
if (typeof props.propsOptions === 'function') { |
||||
if (!props.context) { |
||||
console.warn('[FormBuilder] propsOptions 为函数但未传 context,已自动使用空对象') |
||||
return props.propsOptions({}) |
||||
} |
||||
return props.propsOptions(props.context) |
||||
} |
||||
return props.propsOptions |
||||
}) |
||||
</script> |
||||
|
||||
<template> |
||||
<component |
||||
:is="componentMap[props.type]" |
||||
v-bind="realProps" |
||||
v-model:value="modelValue" |
||||
/> |
||||
</template> |
||||
|
||||
<style scoped lang="scss"></style> |
@ -0,0 +1,31 @@
@@ -0,0 +1,31 @@
|
||||
|
||||
// custom 组件不做类型约束
|
||||
|
||||
export interface FormBuilderContext { |
||||
values: Record<string, any> |
||||
} |
||||
|
||||
export type ConfigProps = |
||||
| Record<string, any> |
||||
| ((context: FormBuilderContext) => Record<string, any>) |
||||
|
||||
export interface ConfigItem { |
||||
label: string; |
||||
type: string; |
||||
value: string; |
||||
class?: string; |
||||
style?: Record<string, string> |
||||
defaultValue: number | boolean | string; |
||||
props?: ConfigProps; |
||||
cssVar?: string; |
||||
unit?: string; |
||||
} |
||||
|
||||
export interface SchemaSection { |
||||
title: string; |
||||
type: string; |
||||
configs: ConfigItem[]; |
||||
} |
||||
|
||||
|
||||
export type SchemaType = Record<string, SchemaSection>; |
@ -0,0 +1,6 @@
@@ -0,0 +1,6 @@
|
||||
import type { ConfigItem } from "./schemaTypes.type"; |
||||
|
||||
// 过滤type中 包含 custom 的配置项
|
||||
export function filterCustomConfigs(configs: ConfigItem[]) { |
||||
return configs.filter(config => !config.type.includes('custom')); |
||||
} |
Loading…
Reference in new issue