Skip to content

FormDesign

基础用法

View Source
vue
<script setup lang="ts">
import type { Resource } from './types'

import { ref } from 'vue'

import { form } from './resources'
import { SwitchSetter } from './setters'
import { base, groupList as defaultGroupList } from './options'

const option = ref({})

const filesDic = [
  { name: 'createTime', comment: '操作时间' },
  { name: 'createBy', comment: '操作人' },
]

// 扩展基础配置
function baseOption(context: any) {
  const defaultBaseOption = base(context)
  const commonOption = { type: 'select', dicData: filesDic, filterable: true, allowCreate: true, defaultFirstOption: true }
  return [
    // 为字段标识增加可选项
    { ...commonOption, props: { label: 'name', value: 'name', desc: 'comment' } },
    // 为字段标题增加可选项
    { ...commonOption, props: { label: 'comment', value: 'comment', desc: 'name' } },
    // 最后添加一个样式类
    ...Array.from({ length: (defaultBaseOption?.length ?? 0) - 2 })
      .fill({}).concat({ label: '样式类', prop: 'class' }),
  ]
}

// 扩展物料库
const resources: Record<string, Resource> = {
  // 给表单增加一个样式类的配置
  form: {
    settings: Array.from({ length: form.settings?.length ?? 0 })
      .fill({}).concat({ label: '样式类', prop: 'class' }),
  } as Resource,
  // 自定义组件,基于el-text封装(@yusui/components/v-text)
  customText: {
    name: 'customText',
    title: '自定义文本',
    icon: 'el-icon-minus',
    group: '自定义分组',
    props: { label: '自定义文本', component: 'v-text', value: '这是一个自定义文本', type: 'primary' },
    settings: [
      {
        label: '显示内容',
        prop: 'value',
        type: 'textarea',
      },
      {
        label: '显示类型',
        prop: 'type',
        type: 'select',
        dicData: [
          { label: 'primary', value: 'primary' },
          { label: 'success', value: 'success' },
          { label: 'warning', value: 'warning' },
          { label: 'danger', value: 'danger' },
          { label: 'info', value: 'info' },
        ],
      },
      {
        label: '显示省略号',
        prop: 'truncated',
        component: SwitchSetter,
      },
      {
        label: '自定义元素标签',
        prop: 'tag',
        labelWidth: 120,
      },
    ],
  },
  customContainer: {
    name: 'customContainer',
    title: '自定义容器',
    icon: 'el-icon-folder',
    group: '自定义分组',
    isContainer: true,
    props: { label: '自定义容器' },
  },
}

const groupList = ['自定义分组', ...defaultGroupList]
</script>

<template>
  <FormDesign
    v-model="option" :base-option="baseOption" :resources="resources" :group-list="groupList"
    style="height: 800px"
  />
</template>

属性

名称说明类型可选值默认值
v-model绑定的值AvueFormOpition--
groupList左侧分组,可以用来修改排序或显示状态string[]--
resources定义物料库及其可配置项Record<string,Resource>--
baseOption定义选中组件时右侧的基础配置Resource['settings']--
advanceOption定义选中组件时右侧的高级配置Resource['settings']--
leftWidth左侧宽度string-200px
rightWidth右侧宽度string-200px
adapterIn输入格式化(option: AvueFormOption) => ElementTreeNode-adapterIn
adapterOut输出格式化(tree: ElementTreeNode) => AvueFormOption-adapterOut

Resource

名称说明类型可选值默认值
name组件定义名称(唯一)string--
title组件中文名称string--
icon组件图标string--
description组件描述string--
docUrl文档链接string--
keywords组件关键词,用于搜索联想string--
group用于描述组件位于组件面板哪个分组string--
priority用于描述组件在同一分组中的排序number--
isContainer用于描述组件是否为容器(如子表单)boolean--
designOption设计面板渲染配置AvueFormOption | Function--
disabled禁用组件,将不会出现在组件面板中boolean--
disabledActions设计面板中禁用复制、删除等动作DesignAction[]--
disabledSettings属性面板中禁用的设置string[]--
props配置面板的默认值boolean--
settings配置面板AvueFormColumn[] | Function-
rules拖放规则--

类型定义

Details
ts
export * from './design'
export * from './props'
export * from './state'