Skip to content

Commit 5b46d8d

Browse files
author
suqi
committed
feat: add switch component
1 parent a5bcdd2 commit 5b46d8d

File tree

6 files changed

+151
-50
lines changed

6 files changed

+151
-50
lines changed

locales/en.yml

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,41 @@ button:
44
methods: Get Table Component Instance
55
edit: Edit
66
delete: Delete
7+
78
text:
89
hope: Hope
910
help: can help you. If you like it, please give it a star
1011
empty: No Data
1112
sure: Whether to confirm the deletion?
12-
console: Please open the console to watch
13+
console: Please open the console to watch
14+
search: Search
15+
addUser: Add User
16+
name: Name
17+
age: Age
18+
address: Address
19+
date: Date
20+
startDate: Start Date
21+
endDate: End Date
22+
username: Username
23+
password: Password
24+
email: Email
25+
phone: Phone
26+
role: Role
27+
account: Account
28+
status: Status
29+
all: All
30+
abel: Able
31+
disable: Disable
32+
33+
placeholder:
34+
name: Please enter name
35+
age: Please enter age
36+
address: Please enter address
37+
date: Please enter date
38+
username: Please enter username
39+
password: Please enter password
40+
email: Please enter email
41+
phone: Please enter phone
42+
role: Please enter role
43+
account: Please enter account
44+
status: Please enter status

locales/zh-CN.yml

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,35 @@ text:
99
help: 能帮你。如果你喜欢它,请给它一个star
1010
empty: 暂无数据
1111
sure: 是否确认删除?
12-
console: 请打开浏览器控制台查看打印信息
12+
console: 请打开浏览器控制台查看打印信息
13+
search: 搜索
14+
addUser: 成员添加
15+
name: 姓名
16+
age: 年龄
17+
address: 地址
18+
date: 日期
19+
startDate: 开始日期
20+
endDate: 结束日期
21+
username: 用户名
22+
password: 密码
23+
email: 邮箱
24+
phone: 手机
25+
role: 角色
26+
account: 账号
27+
status: 状态
28+
all: 全部
29+
abel: 启用
30+
disable: 禁用
31+
32+
placeholder:
33+
name: 请输入姓名
34+
age: 请输入年龄
35+
address: 请输入地址
36+
date: 请输入日期
37+
username: 请输入用户名
38+
password: 请输入密码
39+
email: 请输入邮箱
40+
phone: 请输入手机
41+
role: 请输入角色
42+
account: 请输入账号
43+
status: 请输入状态

packages/components/form/index.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { defineComponent, ref, PropType, reactive, computed } from "vue";
22
// import type { FormInstance, FormRules } from "element-plus";
33
import { formBuilderProps } from "../../types";
4+
// import { getPlusName, renderElement } from "../helper";
45
import {
56
ElForm,
67
ElFormItem,
@@ -13,6 +14,7 @@ import {
1314
ElDatePicker,
1415
ElTimePicker,
1516
ElInputNumber,
17+
ElSwitch,
1618
type FormInstance,
1719
type FormRules
1820
} from "element-plus";
@@ -49,7 +51,7 @@ export default defineComponent({
4951
const ruleFormRef = ref<FormInstance>();
5052
const form = reactive(
5153
props.formItems.reduce((acc, item: any) => {
52-
acc[item.value] = "";
54+
acc[item.value] = item.defaultValue;
5355
return acc;
5456
}, {})
5557
);
@@ -107,6 +109,12 @@ export default defineComponent({
107109
v-model={form[item.value]}
108110
/>
109111
</ElFormItem>
112+
),
113+
// Switch
114+
renderSwitch: (item: any) => (
115+
<ElFormItem label={item.label} prop={item.value}>
116+
<ElSwitch {...(item.attribute || {})} v-model={form[item.value]} />
117+
</ElFormItem>
110118
)
111119
};
112120

@@ -133,6 +141,8 @@ export default defineComponent({
133141
return renderElement.renderSelect(item);
134142
} else if (item.type === "date") {
135143
return renderElement.renderDatePicker(item);
144+
} else if (item.type === "switch") {
145+
return renderElement.renderSwitch(item);
136146
}
137147
})}
138148
</ElCol>
@@ -146,6 +156,8 @@ export default defineComponent({
146156
return renderElement.renderSelect(item);
147157
} else if (item.type === "date") {
148158
return renderElement.renderDatePicker(item);
159+
} else if (item.type === "switch") {
160+
return renderElement.renderSwitch(item);
149161
}
150162
})}
151163
<ElFormItem>

packages/components/helper.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,10 @@
44
export function isFunction(val: unknown): val is Function {
55
return typeof val === "function";
66
}
7+
8+
/**
9+
* @description get element plus name
10+
*/
11+
export function getPlusName(name: string): string {
12+
return `El${name.replace(/( |^)[a-z]/g, L => L.toUpperCase())}`;
13+
}

packages/types/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
21
export interface formBuilderProps {
32
label?: string;
43
type?: string;
54
value?: string;
6-
defaultValue?: string;
5+
defaultValue?: any;
76
attribute?: any;
87
rowIndex?: number;
9-
}
8+
}

src/App.vue

Lines changed: 64 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -24,37 +24,39 @@
2424
</el-tooltip>
2525
</el-space>
2626

27-
<div>
28-
<ElpForm
29-
:formItems="formItems"
30-
:rules="rules"
31-
:submitLoading="submitLoading"
32-
:layoutConfig="layoutConfig"
33-
@resetForm="resetForm"
34-
@search="search"
35-
>
36-
1212</ElpForm
37-
>
38-
</div>
39-
<div>
40-
<h2>添加用户</h2>
41-
<el-button type="primary" @click="dialogVisible = true">添加账号</el-button>
42-
<el-dialog v-model="dialogVisible" title="添加账号" width="40%">
27+
<div class="container">
28+
<el-card class="box-card">
29+
<template #header>
30+
<div class="card-header">
31+
<span>{{ t("text.search") }}</span>
32+
</div>
33+
</template>
34+
<ElpForm
35+
:formItems="formItems"
36+
:rules="rules"
37+
:submitLoading="submitLoading"
38+
:formAttr="{ inline: true }"
39+
@resetForm="resetForm"
40+
@search="search"
41+
>
42+
</ElpForm>
43+
</el-card>
44+
45+
<el-card class="box-card" style="width: 480px">
46+
<template #header>
47+
<div class="card-header">
48+
<span>{{ t("text.addUser") }}</span>
49+
</div>
50+
</template>
4351
<ElpForm
4452
:formItems="addUserForm"
4553
:rules="rules"
4654
:submitLoading="submitLoading"
4755
@resetForm="resetForm"
4856
@search="search"
49-
/>
50-
</el-dialog>
51-
<ElpForm
52-
:formItems="addUserForm"
53-
:rules="rules"
54-
:submitLoading="submitLoading"
55-
@resetForm="resetForm"
56-
@search="search"
57-
/>
57+
>
58+
</ElpForm>
59+
</el-card>
5860
</div>
5961
</template>
6062

@@ -108,20 +110,20 @@ const rules = reactive<FormRules>({
108110
});
109111
const formItems = reactive([
110112
{
111-
label: "姓名",
113+
label: t("text.name"),
112114
type: "input",
113115
value: "name",
114116
defaultValue: "",
115117
116118
attribute: {
117119
maxlength: 20,
118120
clearable: true,
119-
placeholder: "请输入姓名"
121+
placeholder: t("text.name")
120122
},
121123
rowIndex: 0
122124
},
123125
{
124-
label: "电话",
126+
label: t("text.phone"),
125127
type: "input",
126128
value: "mobile",
127129
defaultValue: "",
@@ -130,44 +132,44 @@ const formItems = reactive([
130132
maxlength: 11,
131133
showWordLimit: true,
132134
clearable: true,
133-
placeholder: "请输入电话"
135+
placeholder: t("text.phone")
134136
},
135137
rowIndex: 1
136138
},
137139
{
138-
label: "状态",
140+
label: t("text.status"),
139141
type: "select",
140142
value: "selectValue",
141143
defaultValue: "0",
142144
143145
attribute: {
144-
placeholder: "请选择状态"
146+
placeholder: t("text.status")
145147
},
146148
options: [
147149
{
148-
label: "全部",
150+
label: t("text.all"),
149151
value: "0"
150152
},
151153
{
152-
label: "启用",
154+
label: t("text.abel"),
153155
value: "1"
154156
},
155157
{
156-
label: "禁用",
158+
label: t("text.disable"),
157159
value: "2"
158160
}
159161
],
160162
rowIndex: 2
161163
},
162164
// 日期
163165
{
164-
label: "日期",
166+
label: t("text.date"),
165167
type: "date",
166168
value: "date",
167169
defaultValue: "",
168170
attribute: {
169-
startPlaceholder: "开始日期",
170-
endPlaceholder: "结束日期",
171+
startPlaceholder: t("text.startDate"),
172+
endPlaceholder: t("text.endDate"),
171173
type: "datetimerange",
172174
// "value-format": "yyyy-MM-dd HH:mm:ss",
173175
"range-separator": "",
@@ -183,39 +185,49 @@ const formItems = reactive([
183185
]);
184186
const addUserForm = [
185187
{
186-
label: "姓名",
188+
label: t("text.username"),
189+
type: "switch",
190+
value: "switch",
191+
defaultValue: false,
192+
attribute: {
193+
// placeholder: t("placeholder.username"),
194+
},
195+
rowIndex: 10
196+
},
197+
{
198+
label: t("text.username"),
187199
type: "input",
188200
value: "username",
189201
defaultValue: "",
190202
191203
attribute: {
192-
placeholder: "请输入姓名",
204+
placeholder: t("placeholder.username"),
193205
maxlength: 20,
194206
clearable: true
195207
},
196208
rowIndex: 0
197209
},
198210
{
199-
label: "账号",
211+
label: t("text.account"),
200212
type: "input",
201213
value: "password",
202214
defaultValue: "",
203215
204216
attribute: {
205-
placeholder: "请输入账号",
217+
placeholder: t("placeholder.account"),
206218
maxlength: 20,
207219
clearable: true
208220
},
209221
rowIndex: 1
210222
},
211223
{
212-
label: "密码",
224+
label: t("text.password"),
213225
type: "input",
214226
value: "password",
215227
defaultValue: "",
216228
217229
attribute: {
218-
placeholder: "请输入密码",
230+
placeholder: t("placeholder.password"),
219231
maxlength: 20,
220232
clearable: true
221233
},
@@ -263,7 +275,7 @@ const layoutConfig = [
263275
onMounted(() => {});
264276
</script>
265277

266-
<style>
278+
<style lang="scss">
267279
.el-empty__description {
268280
margin: 0 !important;
269281
}
@@ -275,4 +287,12 @@ onMounted(() => {});
275287
align-items: center;
276288
align-content: center;
277289
}
290+
291+
.container {
292+
// background: red;
293+
margin-top: 1.5rem;
294+
.box-card:not(:last-child) {
295+
margin-bottom: 20px;
296+
}
297+
}
278298
</style>

0 commit comments

Comments
 (0)