feat: pageSearch组件,添加间距样式配置,自适应布局按钮位置配置,添加使用案例

This commit is contained in:
超凡
2025-04-16 23:28:29 +08:00
parent 3499152d60
commit 5f886825bb
9 changed files with 277 additions and 172 deletions

View File

@@ -1,65 +1,68 @@
<template>
<el-card
v-show="visible"
v-hasPerm="searchConfig?.pageName ? `${searchConfig.pageName}:query` : '*:*:*'"
v-bind="cardAttrs"
class="mb-2.5!"
>
<el-form ref="queryFormRef" :model="queryParams" :inline="true" :class="isGrid">
<template v-for="(item, index) in formItems" :key="item.prop">
<el-form-item
v-show="isExpand ? true : index < showNumber"
:label="item?.label"
:prop="item.prop"
>
<!-- Label -->
<template v-if="item?.tips" #label>
<span class="flex-y-center">
{{ item.label }}
<el-tooltip v-bind="getTooltipProps(item.tips)">
<QuestionFilled class="w-4 h-4 mx-1" />
</el-tooltip>
{{ searchConfig.colon ? ":" : "" }}
</span>
</template>
<template v-else #label>{{ item.label }} {{ searchConfig.colon ? ":" : "" }}</template>
<component
:is="componentMap[item?.type ? item?.type : 'input']"
v-model.trim="queryParams[item.prop]"
style="width: 100%"
v-bind="item.attrs"
:config="item.attrs"
v-on="item.events || {}"
<div v-show="visible" style="margin-bottom: 12px" v-bind="cardAttrs">
<el-card
v-hasPerm="searchConfig?.pageName ? `${searchConfig.pageName}:query` : '*:*:*'"
v-bind="cardAttrs"
>
<el-form ref="queryFormRef" :model="queryParams" v-bind="formAttrs" :class="isGrid">
<template v-for="(item, index) in formItems" :key="item.prop">
<el-form-item
v-show="isExpand ? true : index < showNumber"
:label="item?.label"
:prop="item.prop"
>
<template v-if="item.type === 'select'">
<template v-for="opt in item.options">
<el-option :label="opt.label" :value="opt.value" />
</template>
<!-- Label -->
<template v-if="item?.tips" #label>
<span class="flex-y-center">
{{ item.label }}
<el-tooltip v-bind="getTooltipProps(item.tips)">
<QuestionFilled class="w-4 h-4 mx-1" />
</el-tooltip>
{{ searchConfig.colon ? ":" : "" }}
</span>
</template>
</component>
</el-form-item>
</template>
<el-form-item>
<el-button icon="search" type="primary" @click="handleQuery">搜索</el-button>
<el-button icon="refresh" @click="handleReset">重置</el-button>
<!-- 展开/收起 -->
<template v-if="isExpandable && formItems.length > showNumber">
<el-link class="ml-3" type="primary" :underline="false" @click="isExpand = !isExpand">
{{ isExpand ? "收起" : "展开" }}
<component :is="isExpand ? ArrowUp : ArrowDown" class="w-4 h-4 ml-2" />
</el-link>
<template v-else #label>{{ item.label }} {{ searchConfig.colon ? ":" : "" }}</template>
<el-cascader
v-if="item.type === 'cascader'"
v-model.trim="queryParams[item.prop]"
v-bind="{ style: { width: '100%' }, ...item.attrs }"
v-on="item.events || {}"
/>
<component
:is="componentMap.get(item?.type ?? 'input')"
v-else
v-model.trim="queryParams[item.prop]"
v-bind="{ style: { width: '100%' }, ...item.attrs }"
v-on="item.events || {}"
>
<template v-if="item.type === 'select'">
<template v-for="opt in item.options">
<el-option :label="opt.label" :value="opt.value" />
</template>
</template>
</component>
</el-form-item>
</template>
</el-form-item>
</el-form>
</el-card>
<el-form-item :class="{ 'col-[auto/-1] justify-self-end': searchConfig?.grid === 'right' }">
<el-button icon="search" type="primary" @click="handleQuery">搜索</el-button>
<el-button icon="refresh" @click="handleReset">重置</el-button>
<!-- 展开/收起 -->
<template v-if="isExpandable && formItems.length > showNumber">
<el-link class="ml-3" type="primary" :underline="false" @click="isExpand = !isExpand">
{{ isExpand ? "收起" : "展开" }}
<component :is="isExpand ? ArrowUp : ArrowDown" class="w-4 h-4 ml-2" />
</el-link>
</template>
</el-form-item>
</el-form>
</el-card>
</div>
</template>
<script setup lang="ts">
import type { IObject, ISearchConfig, ComponentType } from "./types";
import { ArrowUp, ArrowDown } from "@element-plus/icons-vue";
import { ElInput, ElInputNumber, ElInputTag, ElSelect, ElCascader } from "element-plus";
import { ElTimePicker, ElTimeSelect, ElOption, ElDatePicker, ElTreeSelect } from "element-plus";
import { type FormInstance } from "element-plus";
import InputTag from "@/components/InputTag/index.vue";
@@ -71,18 +74,21 @@ const emit = defineEmits<{
resetClick: [queryParams: IObject];
}>();
// 组件映射表
const componentMap: Record<ComponentType, Component> = {
input: markRaw(ElInput),
select: markRaw(ElSelect),
cascader: markRaw(ElCascader),
"input-number": markRaw(ElInputNumber),
"date-picker": markRaw(ElDatePicker),
"time-picker": markRaw(ElTimePicker),
"time-select": markRaw(ElTimeSelect),
"tree-select": markRaw(ElTreeSelect),
"input-tag": markRaw(ElInputTag),
"custom-tag": markRaw(InputTag),
};
const componentMap = new Map<ComponentType, Component>([
/* eslint-disable */
// @ts-ignore
["input", markRaw(ElInput)], // @ts-ignore
["select", markRaw(ElSelect)], // @ts-ignore
["cascader", markRaw(ElCascader)], // @ts-ignore
["input-number", markRaw(ElInputNumber)], // @ts-ignore
["date-picker", markRaw(ElDatePicker)], // @ts-ignore
["time-picker", markRaw(ElTimePicker)], // @ts-ignore
["time-select", markRaw(ElTimeSelect)], // @ts-ignore
["tree-select", markRaw(ElTreeSelect)], // @ts-ignore
["input-tag", markRaw(ElInputTag)], // @ts-ignore
["custom-tag", markRaw(InputTag)],
/* eslint-enable */
]);
const queryFormRef = ref<FormInstance>();
// 是否显示
@@ -97,11 +103,13 @@ const isExpand = ref(false);
const showNumber = computed(() =>
isExpandable.value ? (props.searchConfig?.showNumber ?? 3) : formItems.length
);
// 卡片组件自定义属性(累名、阴影、权限等)
// 卡片组件自定义属性(阴影、自定义边距样式等)
const cardAttrs = computed<IObject>(() => {
return props.searchConfig?.cardAttrs && props.searchConfig.cardAttrs instanceof Object
? { shadow: "never", ...props.searchConfig.cardAttrs }
: { shadow: "never" };
return { shadow: "never", ...props.searchConfig?.cardAttrs };
});
// 表单组件自定义属性label位置、宽度、对齐方式等
const formAttrs = computed(() => {
return { inline: true, ...props.searchConfig?.form };
});
// 是否使用自适应网格布局
const isGrid = computed(() =>
@@ -119,7 +127,7 @@ const getTooltipProps = (tips: any) => {
onMounted(() => {
formItems.forEach((item) => {
item.initFn && item.initFn(item);
if (item.type === "input-tag" || item.type === "custom-tag") {
if (["input-tag", "custom-tag", "cascader"].includes(item?.type ?? "")) {
queryParams[item.prop] = Array.isArray(item.initialValue) ? item.initialValue : [];
} else if (item.type === "input-number") {
queryParams[item.prop] = item.initialValue ?? null;

View File

@@ -13,6 +13,7 @@ import type PageContent from "./PageContent.vue";
import type PageForm from "./PageForm.vue";
import type PageModal from "./PageModal.vue";
import type PageSearch from "./PageSearch.vue";
import { CSSProperties } from "vue";
export type PageSearchInstance = InstanceType<typeof PageSearch>;
export type PageContentInstance = InstanceType<typeof PageContent>;
@@ -71,9 +72,11 @@ export interface ISearchConfig {
// 默认展示的表单项数量(默认3)
showNumber?: number;
// 卡片属性
cardAttrs?: Partial<CardProps>;
cardAttrs?: Partial<CardProps> & { style?: CSSProperties };
// form组件属性
form?: IForm;
// 自适应网格布局(使用时表单不要添加 style: { width: "200px" })
grid?: boolean;
grid?: boolean | "left" | "right";
}
export interface IContentConfig<T = any> {

View File

@@ -1,26 +1,29 @@
<template>
<div class="flex-y-center gap-2">
<el-tag
v-for="tag in tags"
:key="tag"
closable
:disable-transitions="false"
v-bind="config.tagAttrs"
@close="handleClose(tag)"
>
{{ tag }}
</el-tag>
<el-input
v-if="inputVisible"
ref="inputRef"
v-model.trim="inputValue"
@keyup.enter.stop.prevent="handleInputConfirm"
@blur.stop.prevent="handleInputConfirm"
/>
<el-button v-else v-bind="config.buttonAttrs" @click="showInput">
{{ config.buttonAttrs.btnText ? config.buttonAttrs.btnText : "+ New Tag" }}
</el-button>
</div>
<el-scrollbar>
<div class="flex-y-center gap-2">
<el-tag
v-for="tag in tags"
:key="tag"
closable
:disable-transitions="false"
v-bind="config.tagAttrs"
@close="handleClose(tag)"
>
{{ tag }}
</el-tag>
<el-input
v-if="inputVisible"
ref="inputRef"
v-model.trim="inputValue"
style="min-width: 100px"
@keyup.enter.stop.prevent="handleInputConfirm"
@blur.stop.prevent="handleInputConfirm"
/>
<el-button v-else v-bind="config.buttonAttrs" @click="showInput">
{{ config.buttonAttrs.btnText ? config.buttonAttrs.btnText : "+ New Tag" }}
</el-button>
</div>
</el-scrollbar>
</template>
<script setup lang="ts">
import type { InputInstance } from "element-plus";

View File

@@ -1,6 +1,4 @@
import { createApp } from "vue";
// TODO::不引入el-input-tag样式会丢失
import "element-plus/es/components/input-tag/style/css";
import App from "./App.vue";
import setupPlugins from "@/plugins";

View File

@@ -19,6 +19,7 @@ declare module "vue" {
ElBreadcrumbItem: (typeof import("element-plus/es"))["ElBreadcrumbItem"];
ElButton: (typeof import("element-plus/es"))["ElButton"];
ElCard: (typeof import("element-plus/es"))["ElCard"];
ElCascader: (typeof import("element-plus/es"))["ElCascader"];
ElCheckbox: (typeof import("element-plus/es"))["ElCheckbox"];
ElCheckboxGroup: (typeof import("element-plus/es"))["ElCheckboxGroup"];
ElCol: (typeof import("element-plus/es"))["ElCol"];
@@ -56,6 +57,7 @@ declare module "vue" {
ElTableColumn: (typeof import("element-plus/es"))["ElTableColumn"];
ElTag: (typeof import("element-plus/es"))["ElTag"];
ElText: (typeof import("element-plus/es"))["ElText"];
ElTimeSelect: (typeof import("element-plus/es"))["ElTimeSelect"];
ElTooltip: (typeof import("element-plus/es"))["ElTooltip"];
ElTree: (typeof import("element-plus/es"))["ElTree"];
ElTreeSelect: (typeof import("element-plus/es"))["ElTreeSelect"];

View File

@@ -1,14 +1,8 @@
import DeptAPI from "@/api/system/dept.api";
import type { ISearchConfig } from "@/components/CURD/types";
const selectOptions = reactive([
{ label: "启用", value: 1 },
{ label: "禁用", value: 0 },
]);
const searchConfig: ISearchConfig = {
pageName: "sys:user",
colon: false,
formItems: [
{
tips: "支持模糊搜索",
@@ -20,13 +14,6 @@ const searchConfig: ISearchConfig = {
clearable: true,
style: { width: "200px" },
},
events: {
change: (e) => {
console.log("输入框的值: ", e);
// 级联操作示例需要使用reactive提前定义数组
// selectOptions.push({ label: e, value: e });
},
},
},
{
type: "tree-select",
@@ -48,7 +35,6 @@ const searchConfig: ISearchConfig = {
},
},
{
tips: { effect: "light", placement: "top", content: "自定义文字提示" },
type: "select",
label: "状态",
prop: "status",
@@ -57,17 +43,15 @@ const searchConfig: ISearchConfig = {
clearable: true,
style: { width: "200px" },
},
options: selectOptions,
events: {
change: function (e) {
console.log("选中的值: ", e);
},
},
options: [
{ label: "启用", value: 1 },
{ label: "禁用", value: 0 },
],
},
{
type: "date-picker",
label: "创建时间",
prop: "createAt",
prop: "createTime",
attrs: {
type: "daterange",
"range-separator": "~",
@@ -77,61 +61,6 @@ const searchConfig: ISearchConfig = {
style: { width: "200px" },
},
},
{
type: "date-picker",
label: "日期选择器",
prop: "testDataPicker",
attrs: {
type: "date",
placeholder: "选择日期",
style: { width: "200px" },
},
},
{
type: "input-number",
label: "数字输入框",
prop: "testInputNumber",
attrs: {
controls: false,
placeholder: "请输入数字",
style: { width: "200px" },
},
},
{
type: "input-tag",
label: "标签选择器",
prop: "testInputTags",
attrs: {
clearable: true,
placeholder: "请输入",
},
},
{
type: "custom-tag",
label: "标签选择器",
prop: "testCustomTags",
attrs: {
buttonAttrs: { btnText: "+ New Tag" },
inputAttrs: {},
tagAttrs: {},
},
},
{
type: "time-picker",
label: "时间选择器",
prop: "testTimePicker",
attrs: {
style: { width: "200px" },
},
},
{
type: "time-select",
label: "时间选择",
prop: "testTimeSelect",
attrs: {
style: { width: "200px" },
},
},
],
};

View File

@@ -0,0 +1,158 @@
import DeptAPI from "@/api/system/dept.api";
import type { ISearchConfig } from "@/components/CURD/types";
const selectOptions = reactive([
{ label: "启用", value: 1 },
{ label: "禁用", value: 0 },
]);
const searchConfig: ISearchConfig = {
grid: "right",
colon: true,
showNumber: 3,
form: { labelPosition: "right", labelWidth: "90px" },
cardAttrs: { shadow: "hover", style: { "margin-bottom": "12px" } },
formItems: [
{
tips: { effect: "light", placement: "top", content: "自定义文字提示" },
type: "input",
label: "输入框",
prop: "testInput",
attrs: { placeholder: "请输入", clearable: true },
events: {
change: (e) => {
console.log("输入框的值: ", e);
// 级联操作示例需要使用reactive提前定义数组
// selectOptions.push({ label: e, value: e });
},
},
},
{
type: "input-number",
label: "数字输入框",
prop: "testInputNumber",
attrs: { placeholder: "请输入", controls: false },
},
{
type: "select",
label: "下拉选择框",
prop: "testSelect",
attrs: { placeholder: "全部", clearable: true },
options: selectOptions,
events: {
change: function (e) {
console.log("选中的值: ", e);
},
},
},
{
type: "tree-select",
label: "树形选择框",
prop: "testTreeSelect",
attrs: {
placeholder: "请选择",
data: [],
filterable: true,
"check-strictly": true,
"render-after-expand": false,
clearable: true,
},
async initFn(formItem) {
formItem.attrs.data = await DeptAPI.getOptions();
// 注意:如果initFn函数不是箭头函数,this会指向此配置项对象,那么也就可以用this来替代形参formItem
// this.attrs!.data = await DeptAPI.getOptions();
},
},
{
type: "cascader",
label: "级联选择器",
prop: "testCascader",
attrs: {
placeholder: "请选择",
clearable: true,
props: {
expandTrigger: "hover",
label: "label",
value: "value",
children: "children",
},
options: [
{
value: "guide",
label: "Guide",
children: [
{
value: "disciplines",
label: "Disciplines",
children: [
{
value: "consistency",
label: "Consistency",
},
],
},
{
value: "navigation",
label: "Navigation",
children: [
{
value: "side nav",
label: "Side Navigation",
},
],
},
],
},
],
},
},
{
type: "date-picker",
label: "范围选择器",
prop: "createAt",
attrs: {
type: "daterange",
"range-separator": "~",
"start-placeholder": "开始时间",
"end-placeholder": "截止时间",
"value-format": "YYYY-MM-DD",
},
},
{
type: "date-picker",
label: "日期选择器",
prop: "testDataPicker",
attrs: { placeholder: "请选择", type: "date" },
},
{
type: "time-picker",
label: "时间选择器",
prop: "testTimePicker",
attrs: { placeholder: "请选择", clearable: true },
},
{
type: "time-select",
label: "时间选择",
prop: "testTimeSelect",
attrs: { placeholder: "请选择", clearable: true },
},
{
type: "input-tag",
label: "标签选择器",
prop: "testInputTags",
attrs: { placeholder: "请选择", clearable: true },
},
{
type: "custom-tag",
label: "标签选择器",
prop: "testCustomTags",
attrs: {
buttonAttrs: { btnText: "+ New Tag" },
inputAttrs: {},
tagAttrs: {},
},
},
],
};
export default searchConfig;

View File

@@ -74,6 +74,7 @@
</page-modal>
</template>
<template v-else>
<page-search ref="searchRef" :search-config="searchConfig2" @reset-click="handleResetClick" />
<page-content
ref="contentRef"
:content-config="contentConfig2"
@@ -100,6 +101,7 @@ import contentConfig from "./config/content";
import contentConfig2 from "./config/content2";
import editModalConfig from "./config/edit";
import searchConfig from "./config/search";
import searchConfig2 from "./config/search2";
const {
searchRef,

View File

@@ -121,6 +121,7 @@ export default defineConfig(({ mode }: ConfigEnv) => {
"element-plus/es/components/breadcrumb/style/index",
"element-plus/es/components/button/style/index",
"element-plus/es/components/card/style/index",
"element-plus/es/components/cascader/style/index",
"element-plus/es/components/checkbox-group/style/index",
"element-plus/es/components/checkbox/style/index",
"element-plus/es/components/col/style/index",
@@ -174,6 +175,7 @@ export default defineConfig(({ mode }: ConfigEnv) => {
"element-plus/es/components/tag/style/index",
"element-plus/es/components/text/style/index",
"element-plus/es/components/time-picker/style/index",
"element-plus/es/components/time-select/style/index",
"element-plus/es/components/timeline-item/style/index",
"element-plus/es/components/timeline/style/index",
"element-plus/es/components/tooltip/style/index",