使用 tsx 封装 form-create 通用选择组件
parent
21483448b9
commit
1f1ac1f246
@ -1,4 +1,4 @@
|
||||
import { useFormCreateDesigner } from './src/useFormCreateDesigner'
|
||||
import CurrencySelect from './src/CurrencySelect/index.vue'
|
||||
import { useCurrencySelect } from './src/components/useCurrencySelect'
|
||||
|
||||
export { useFormCreateDesigner, CurrencySelect }
|
||||
export { useFormCreateDesigner, useCurrencySelect }
|
||||
|
||||
@ -0,0 +1,59 @@
|
||||
import request from '@/config/axios'
|
||||
import { isEmpty } from '@/utils/is'
|
||||
import { CurrencySelectProps } from '@/components/FormCreate/src/type'
|
||||
|
||||
export const useCurrencySelect = (option: CurrencySelectProps) => {
|
||||
return defineComponent({
|
||||
name: option.name,
|
||||
props: {
|
||||
// 字典类型
|
||||
labelField: {
|
||||
type: String,
|
||||
default: () => option.labelField ?? ''
|
||||
},
|
||||
// 字典值类型
|
||||
valueField: {
|
||||
type: String,
|
||||
default: () => option.valueField ?? ''
|
||||
},
|
||||
// api 接口
|
||||
restful: {
|
||||
type: String,
|
||||
default: () => option.restful ?? ''
|
||||
}
|
||||
},
|
||||
setup(props) {
|
||||
const attrs = useAttrs()
|
||||
const options = ref<any[]>([]) // 下拉数据
|
||||
const getOptions = async () => {
|
||||
options.value = []
|
||||
if (isEmpty(props.restful)) {
|
||||
return
|
||||
}
|
||||
// TODO 只支持 GET 查询,复杂下拉构建条件请使用业务表单
|
||||
const data = await request.get({ url: props.restful })
|
||||
if (Array.isArray(data)) {
|
||||
options.value = data.map((item: any) => ({
|
||||
label: item[props.labelField],
|
||||
value: item[props.valueField]
|
||||
}))
|
||||
return
|
||||
}
|
||||
console.log(`接口[${props.restful}] 返回结果不是一个数组`)
|
||||
}
|
||||
|
||||
onMounted(async () => {
|
||||
await getOptions()
|
||||
})
|
||||
return () => (
|
||||
<>
|
||||
<el-select className="w-1/1" {...attrs}>
|
||||
{options.value.map((item, index) => (
|
||||
<el-option key={index} label={item.label} value={item.value} />
|
||||
))}
|
||||
</el-select>
|
||||
</>
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
||||
@ -0,0 +1,26 @@
|
||||
import { generateUUID } from '@/utils'
|
||||
import { localeProps, makeRequiredRule } from '@/components/FormCreate/src/utils'
|
||||
import selectRule from '@/components/FormCreate/src/config/selectRule'
|
||||
import { DragRule } from '@/components/FormCreate/src/type'
|
||||
|
||||
export const useDeptSelectRule = (): DragRule => {
|
||||
const label = '部门选择器'
|
||||
const name = 'DeptSelect'
|
||||
return {
|
||||
icon: 'icon-select',
|
||||
label,
|
||||
name,
|
||||
rule() {
|
||||
return {
|
||||
type: name,
|
||||
field: generateUUID(),
|
||||
title: label,
|
||||
info: '',
|
||||
$required: false
|
||||
}
|
||||
},
|
||||
props(_, { t }) {
|
||||
return localeProps(t, name + '.props', [makeRequiredRule(), ...selectRule])
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,26 @@
|
||||
import { generateUUID } from '@/utils'
|
||||
import { localeProps, makeRequiredRule } from '@/components/FormCreate/src/utils'
|
||||
import selectRule from '@/components/FormCreate/src/config/selectRule'
|
||||
import { DragRule } from '@/components/FormCreate/src/type'
|
||||
|
||||
export const useUserSelectRule = (): DragRule => {
|
||||
const label = '用户选择器'
|
||||
const name = 'UserSelect'
|
||||
return {
|
||||
icon: 'icon-select',
|
||||
label,
|
||||
name,
|
||||
rule() {
|
||||
return {
|
||||
type: name,
|
||||
field: generateUUID(),
|
||||
title: label,
|
||||
info: '',
|
||||
$required: false
|
||||
}
|
||||
},
|
||||
props(_, { t }) {
|
||||
return localeProps(t, name + '.props', [makeRequiredRule(), ...selectRule])
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,41 @@
|
||||
import { Rule } from '@form-create/element-ui' //左侧拖拽按钮
|
||||
|
||||
//左侧拖拽按钮
|
||||
export interface MenuItem {
|
||||
label: string
|
||||
name: string
|
||||
icon: string
|
||||
}
|
||||
|
||||
//左侧拖拽按钮分类
|
||||
export interface Menu {
|
||||
title: string
|
||||
name: string
|
||||
list: MenuItem[]
|
||||
}
|
||||
|
||||
export interface MenuList extends Array<Menu> {}
|
||||
|
||||
//拖拽组件的规则
|
||||
export interface DragRule {
|
||||
icon: string
|
||||
name: string
|
||||
label: string
|
||||
children?: string
|
||||
inside?: true
|
||||
drag?: true | String
|
||||
dragBtn?: false
|
||||
mask?: false
|
||||
|
||||
rule(): Rule
|
||||
|
||||
props(v: any, v1: any): Rule[]
|
||||
}
|
||||
|
||||
// 通用下拉组件 Props 类型
|
||||
export interface CurrencySelectProps {
|
||||
name: string // 组件名称
|
||||
labelField?: string // 字典类型
|
||||
valueField?: string // 字典值类型
|
||||
restful?: string // api 接口
|
||||
}
|
||||
Loading…
Reference in New Issue