Browse Source
- 添加自定义 RadioGroup 组件,支持 options 属性 - 更新 DataFormConstant,替换 Element Plus 的 ElRadioGroup 为自定义组件 - 新增 mergeExpose 函数,用于合并组件的 expose 对象 - 在 DataFormUtils 中添加新函数,增强组件合并功能 - 更新示例页面,展示 RadioGroup 组件的使用方法master
4 changed files with 147 additions and 3 deletions
@ -0,0 +1,96 @@ |
|||||
|
<script setup lang="ts"> |
||||
|
import lodash from "lodash"; |
||||
|
import { computed, createVNode, defineExpose, onMounted, reactive, useAttrs, useSlots, useTemplateRef } from "vue"; |
||||
|
import { ElRadio, ElRadioGroup } from "element-plus"; |
||||
|
import { Typeof } from "@ease-forge/shared"; |
||||
|
import { mergeExpose } from "../DataFormUtils.tsx"; |
||||
|
|
||||
|
defineOptions({ |
||||
|
name: 'RadioGroup', |
||||
|
}); |
||||
|
|
||||
|
// 组件属性 |
||||
|
const attrs = useAttrs(); |
||||
|
// 组件插槽 |
||||
|
const slots = useSlots(); |
||||
|
|
||||
|
interface OptionItem { |
||||
|
value: string | number | boolean; |
||||
|
label: string; |
||||
|
|
||||
|
[key: string]: any; |
||||
|
} |
||||
|
|
||||
|
// 定义 Props 类型 |
||||
|
interface RadioGroupProps { |
||||
|
options?: Array<OptionItem>; |
||||
|
} |
||||
|
|
||||
|
// 读取组件 props 属性 |
||||
|
const props = withDefaults(defineProps<RadioGroupProps>(), { |
||||
|
options: () => ([]), |
||||
|
}); |
||||
|
|
||||
|
// 定义 State 类型 |
||||
|
interface RadioGroupState { |
||||
|
} |
||||
|
|
||||
|
// state 属性 |
||||
|
const state = reactive<RadioGroupState>({ |
||||
|
loading: false, |
||||
|
}); |
||||
|
// 输入组件实例 |
||||
|
const input = useTemplateRef<InstanceType<typeof ElRadioGroup>>("inputRef"); |
||||
|
// 选项数据 |
||||
|
const options = computed(() => { |
||||
|
let opts: Array<OptionItem> = []; |
||||
|
let options = props.options; |
||||
|
if (options) { |
||||
|
if (Typeof.isArray(options)) { |
||||
|
opts = options; |
||||
|
} else { |
||||
|
console.log("options 配置必须是数组类型"); |
||||
|
} |
||||
|
} |
||||
|
return opts; |
||||
|
}); |
||||
|
// 输入组件 props 属性 |
||||
|
const inputProps = computed<any>(() => { |
||||
|
const res: any = { ...attrs }; |
||||
|
if (Typeof.hasValue(res.modelValue) && !Typeof.isStr(res.modelValue)) res.modelValue = lodash.toString(res.modelValue); |
||||
|
return res; |
||||
|
}); |
||||
|
|
||||
|
function toString(val: any) { |
||||
|
if (Typeof.hasValue(val) && !Typeof.isStr(val)) return lodash.toString(val); |
||||
|
return val; |
||||
|
} |
||||
|
|
||||
|
const expose = { |
||||
|
/** 组件状态 */ |
||||
|
state, |
||||
|
}; |
||||
|
|
||||
|
onMounted(() => { |
||||
|
if (input.value) mergeExpose(expose, input.value); |
||||
|
}); |
||||
|
|
||||
|
// 定义组件公开内容 |
||||
|
defineExpose(expose); |
||||
|
|
||||
|
export type { |
||||
|
OptionItem, |
||||
|
RadioGroupProps, |
||||
|
RadioGroupState, |
||||
|
} |
||||
|
</script> |
||||
|
|
||||
|
<template> |
||||
|
<component ref="inputRef" :is="createVNode(ElRadioGroup, inputProps, slots)"> |
||||
|
<ElRadio v-for="(item, idx) in options" v-bind="item" :value="toString(item.value)"/> |
||||
|
</component> |
||||
|
</template> |
||||
|
|
||||
|
<style scoped> |
||||
|
|
||||
|
</style> |
||||
Loading…
Reference in new issue