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

View File

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

View File

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

View File

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

View File

@@ -1,14 +1,8 @@
import DeptAPI from "@/api/system/dept.api"; import DeptAPI from "@/api/system/dept.api";
import type { ISearchConfig } from "@/components/CURD/types"; import type { ISearchConfig } from "@/components/CURD/types";
const selectOptions = reactive([
{ label: "启用", value: 1 },
{ label: "禁用", value: 0 },
]);
const searchConfig: ISearchConfig = { const searchConfig: ISearchConfig = {
pageName: "sys:user", pageName: "sys:user",
colon: false,
formItems: [ formItems: [
{ {
tips: "支持模糊搜索", tips: "支持模糊搜索",
@@ -20,13 +14,6 @@ const searchConfig: ISearchConfig = {
clearable: true, clearable: true,
style: { width: "200px" }, style: { width: "200px" },
}, },
events: {
change: (e) => {
console.log("输入框的值: ", e);
// 级联操作示例需要使用reactive提前定义数组
// selectOptions.push({ label: e, value: e });
},
},
}, },
{ {
type: "tree-select", type: "tree-select",
@@ -48,7 +35,6 @@ const searchConfig: ISearchConfig = {
}, },
}, },
{ {
tips: { effect: "light", placement: "top", content: "自定义文字提示" },
type: "select", type: "select",
label: "状态", label: "状态",
prop: "status", prop: "status",
@@ -57,17 +43,15 @@ const searchConfig: ISearchConfig = {
clearable: true, clearable: true,
style: { width: "200px" }, style: { width: "200px" },
}, },
options: selectOptions, options: [
events: { { label: "启用", value: 1 },
change: function (e) { { label: "禁用", value: 0 },
console.log("选中的值: ", e); ],
},
},
}, },
{ {
type: "date-picker", type: "date-picker",
label: "创建时间", label: "创建时间",
prop: "createAt", prop: "createTime",
attrs: { attrs: {
type: "daterange", type: "daterange",
"range-separator": "~", "range-separator": "~",
@@ -77,61 +61,6 @@ const searchConfig: ISearchConfig = {
style: { width: "200px" }, 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> </page-modal>
</template> </template>
<template v-else> <template v-else>
<page-search ref="searchRef" :search-config="searchConfig2" @reset-click="handleResetClick" />
<page-content <page-content
ref="contentRef" ref="contentRef"
:content-config="contentConfig2" :content-config="contentConfig2"
@@ -100,6 +101,7 @@ import contentConfig from "./config/content";
import contentConfig2 from "./config/content2"; import contentConfig2 from "./config/content2";
import editModalConfig from "./config/edit"; import editModalConfig from "./config/edit";
import searchConfig from "./config/search"; import searchConfig from "./config/search";
import searchConfig2 from "./config/search2";
const { const {
searchRef, searchRef,

View File

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