Skip to content

Commit b4d85b7

Browse files
author
puhui999
committed
form-create: 封装通用选择器 hook
1 parent c1c21d8 commit b4d85b7

File tree

8 files changed

+114
-63
lines changed

8 files changed

+114
-63
lines changed

src/components/DictSelect/index.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/components/DictSelect/src/DictSelect.vue

Lines changed: 0 additions & 46 deletions
This file was deleted.

src/components/FormCreate/src/components/useCurrencySelect.tsx

Lines changed: 93 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,54 @@
11
import request from '@/config/axios'
22
import { isEmpty } from '@/utils/is'
33
import { CurrencySelectProps } from '@/components/FormCreate/src/type'
4+
import { getBoolDictOptions, getIntDictOptions, getStrDictOptions } from '@/utils/dict'
45

56
export const useCurrencySelect = (option: CurrencySelectProps) => {
67
return defineComponent({
78
name: option.name,
89
props: {
9-
// 字典类型
10+
// 选项标签
1011
labelField: {
1112
type: String,
12-
default: () => option.labelField ?? ''
13+
default: () => option.labelField ?? 'label'
1314
},
14-
// 字典值类型
15+
// 选项的值
1516
valueField: {
1617
type: String,
17-
default: () => option.valueField ?? ''
18+
default: () => option.valueField ?? 'value'
1819
},
1920
// api 接口
2021
restful: {
2122
type: String,
2223
default: () => option.restful ?? ''
24+
},
25+
// 字典类型
26+
dictType: {
27+
type: String,
28+
default: ''
29+
},
30+
// 字典值类型 'str' | 'int' | 'bool'
31+
dictValueType: {
32+
type: String,
33+
default: 'str'
34+
},
35+
// 选择器类型,下拉框 select、多选框 checkbox、单选框 radio
36+
selectType: {
37+
type: String,
38+
default: 'select'
2339
}
2440
},
2541
setup(props) {
2642
const attrs = useAttrs()
2743
const options = ref<any[]>([]) // 下拉数据
2844
const getOptions = async () => {
2945
options.value = []
46+
// 字典选择器
47+
if (option.isDict) {
48+
options.value = getDictOptions()
49+
return
50+
}
51+
// 接口选择器
3052
if (isEmpty(props.restful)) {
3153
return
3254
}
@@ -41,17 +63,78 @@ export const useCurrencySelect = (option: CurrencySelectProps) => {
4163
}
4264
console.log(`接口[${props.restful}] 返回结果不是一个数组`)
4365
}
44-
66+
// 获得字典配置
67+
const getDictOptions = () => {
68+
switch (props.dictValueType) {
69+
case 'str':
70+
return getStrDictOptions(props.dictType)
71+
case 'int':
72+
return getIntDictOptions(props.dictType)
73+
case 'bool':
74+
return getBoolDictOptions(props.dictType)
75+
default:
76+
return []
77+
}
78+
}
4579
onMounted(async () => {
4680
await getOptions()
4781
})
82+
const buildSelect = () => {
83+
return (
84+
<>
85+
<el-select class="w-1/1" {...attrs}>
86+
{options.value.map((item, index) => (
87+
<el-option key={index} label={item.label} value={item.value} />
88+
))}
89+
</el-select>
90+
</>
91+
)
92+
}
93+
const buildCheckbox = () => {
94+
if (isEmpty(options.value)) {
95+
options.value = [
96+
{ label: '选项1', value: '选项1' },
97+
{ label: '选项2', value: '选项2' }
98+
]
99+
}
100+
return (
101+
<>
102+
<el-checkbox-group class="w-1/1" {...attrs}>
103+
{options.value.map((item, index) => (
104+
<el-checkbox key={index} label={item.label} value={item.value} />
105+
))}
106+
</el-checkbox-group>
107+
</>
108+
)
109+
}
110+
const buildRadio = () => {
111+
if (isEmpty(options.value)) {
112+
options.value = [
113+
{ label: '选项1', value: '选项1' },
114+
{ label: '选项2', value: '选项2' }
115+
]
116+
}
117+
return (
118+
<>
119+
<el-radio-group class="w-1/1" {...attrs}>
120+
{options.value.map((item, index) => (
121+
<el-radio key={index} value={item.value}>
122+
{item.label}
123+
</el-radio>
124+
))}
125+
</el-radio-group>
126+
</>
127+
)
128+
}
48129
return () => (
49130
<>
50-
<el-select className="w-1/1" {...attrs}>
51-
{options.value.map((item, index) => (
52-
<el-option key={index} label={item.label} value={item.value} />
53-
))}
54-
</el-select>
131+
{props.selectType === 'select'
132+
? buildSelect()
133+
: props.selectType === 'radio'
134+
? buildRadio()
135+
: props.selectType === 'checkbox'
136+
? buildCheckbox()
137+
: buildSelect()}
55138
</>
56139
)
57140
}

src/components/FormCreate/src/config/selectRule.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
const selectRule = [
2+
{
3+
type: 'select',
4+
field: 'selectType',
5+
title: '选择器类型',
6+
value: 'select',
7+
options: [
8+
{ label: '下拉框', value: 'select' },
9+
{ label: '单选框', value: 'radio' },
10+
{ label: '多选框', value: 'checkbox' }
11+
]
12+
},
213
{ type: 'switch', field: 'multiple', title: '是否多选' },
314
{
415
type: 'switch',

src/components/FormCreate/src/config/useDictSelectRule.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export const useDictSelectRule = () => {
4646
},
4747
{
4848
type: 'select',
49-
field: 'valueType',
49+
field: 'dictValueType',
5050
title: '字典值类型',
5151
value: 'str',
5252
options: [

src/components/FormCreate/src/type/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ export interface DragRule {
3535
// 通用下拉组件 Props 类型
3636
export interface CurrencySelectProps {
3737
name: string // 组件名称
38-
labelField?: string // 字典类型
39-
valueField?: string // 字典值类型
38+
labelField?: string // 选项标签
39+
valueField?: string // 选项的值
4040
restful?: string // api 接口
41+
isDict?: boolean // 是否字典选择器
4142
}
4243

4344
// 选择组件规则配置类型

src/components/FormCreate/src/useFormCreateDesigner.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ export const useFormCreateDesigner = async (designer: Ref) => {
6767
designer.value?.removeMenuItem('fc-editor')
6868
// 移除自带的下拉选择器组件,使用 currencySelectRule 替代
6969
designer.value?.removeMenuItem('select')
70+
designer.value?.removeMenuItem('radio')
71+
designer.value?.removeMenuItem('checkbox')
7072
const components = [
7173
editorRule,
7274
uploadFileRule,

src/plugins/formCreate/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import formCreate from '@form-create/element-ui'
1919
import install from '@form-create/element-ui/auto-import'
2020
//======================= 自定义组件 =======================
2121
import { UploadFile, UploadImg, UploadImgs } from '@/components/UploadFile'
22-
import { DictSelect } from '@/components/DictSelect'
2322
import { useCurrencySelect } from '@/components/FormCreate'
2423
import { Editor } from '@/components/Editor'
2524

@@ -38,6 +37,10 @@ const DeptSelect = useCurrencySelect({
3837
const RestfulSelect = useCurrencySelect({
3938
name: 'RestfulSelect'
4039
})
40+
const DictSelect = useCurrencySelect({
41+
name: 'DictSelect',
42+
isDict: true
43+
})
4144
const components = [
4245
ElAside,
4346
ElPopconfirm,

0 commit comments

Comments
 (0)