perf: 优化Search组件
This commit is contained in:
parent
c76f8bc494
commit
e548668cce
|
@ -1,3 +1,8 @@
|
||||||
import Search from './src/Search.vue'
|
import Search from './src/Search.vue'
|
||||||
|
import { ElForm } from 'element-plus'
|
||||||
|
|
||||||
|
export interface SearchExpose {
|
||||||
|
getElFormExpose: () => Promise<ComponentRef<typeof ElForm>>
|
||||||
|
}
|
||||||
|
|
||||||
export { Search }
|
export { Search }
|
||||||
|
|
|
@ -2,17 +2,14 @@
|
||||||
import { Form, FormSchema } from '@/components/Form'
|
import { Form, FormSchema } from '@/components/Form'
|
||||||
import { PropType, computed, unref, ref, watch, onMounted } from 'vue'
|
import { PropType, computed, unref, ref, watch, onMounted } from 'vue'
|
||||||
import { propTypes } from '@/utils/propTypes'
|
import { propTypes } from '@/utils/propTypes'
|
||||||
import { ElButton } from 'element-plus'
|
|
||||||
import { useI18n } from '@/hooks/web/useI18n'
|
|
||||||
import { useForm } from '@/hooks/web/useForm'
|
import { useForm } from '@/hooks/web/useForm'
|
||||||
import { useIcon } from '@/hooks/web/useIcon'
|
|
||||||
import { findIndex } from '@/utils'
|
import { findIndex } from '@/utils'
|
||||||
import { cloneDeep } from 'lodash-es'
|
import { cloneDeep } from 'lodash-es'
|
||||||
import { initModel } from '@/components/Form/src/helper'
|
import { initModel } from '@/components/Form/src/helper'
|
||||||
|
import ActionButton from './components/ActiconButton.vue'
|
||||||
const { t } = useI18n()
|
|
||||||
|
|
||||||
const formExpose = ref<ComponentRef<typeof Form>>()
|
const formExpose = ref<ComponentRef<typeof Form>>()
|
||||||
|
const searchRef = ref()
|
||||||
|
|
||||||
const props = defineProps({
|
const props = defineProps({
|
||||||
// 生成Form的布局结构数组
|
// 生成Form的布局结构数组
|
||||||
|
@ -33,7 +30,7 @@ const props = defineProps({
|
||||||
showSearch: propTypes.bool.def(true),
|
showSearch: propTypes.bool.def(true),
|
||||||
showReset: propTypes.bool.def(true),
|
showReset: propTypes.bool.def(true),
|
||||||
// 是否显示伸缩
|
// 是否显示伸缩
|
||||||
expand: propTypes.bool.def(false),
|
showExpand: propTypes.bool.def(false),
|
||||||
// 伸缩的界限字段
|
// 伸缩的界限字段
|
||||||
expandField: propTypes.string.def(''),
|
expandField: propTypes.string.def(''),
|
||||||
inline: propTypes.bool.def(true),
|
inline: propTypes.bool.def(true),
|
||||||
|
@ -54,7 +51,7 @@ const formModel = ref<Recordable>({})
|
||||||
|
|
||||||
const newSchema = computed(() => {
|
const newSchema = computed(() => {
|
||||||
let schema: FormSchema[] = cloneDeep(props.schema)
|
let schema: FormSchema[] = cloneDeep(props.schema)
|
||||||
if (props.expand && props.expandField && !unref(visible)) {
|
if (props.showExpand && props.expandField && !unref(visible)) {
|
||||||
const index = findIndex(schema, (v: FormSchema) => v.field === props.expandField)
|
const index = findIndex(schema, (v: FormSchema) => v.field === props.expandField)
|
||||||
schema.map((v, i) => {
|
schema.map((v, i) => {
|
||||||
if (i >= index) {
|
if (i >= index) {
|
||||||
|
@ -75,27 +72,15 @@ const newSchema = computed(() => {
|
||||||
default: () => {
|
default: () => {
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
{props.showSearch ? (
|
<ActionButton
|
||||||
<ElButton type="primary" onClick={search} icon={useIcon({ icon: 'ep:search' })}>
|
showSearch={props.showSearch}
|
||||||
{t('common.query')}
|
showReset={props.showReset}
|
||||||
</ElButton>
|
showExpand={props.showExpand}
|
||||||
) : null}
|
visible={visible.value}
|
||||||
{props.showReset ? (
|
onExpand={setVisible}
|
||||||
<ElButton onClick={reset} icon={useIcon({ icon: 'ep:refresh-right' })}>
|
onReset={reset}
|
||||||
{t('common.reset')}
|
onSearch={search}
|
||||||
</ElButton>
|
/>
|
||||||
) : null}
|
|
||||||
{props.expand ? (
|
|
||||||
<ElButton
|
|
||||||
text
|
|
||||||
onClick={setVisible}
|
|
||||||
icon={useIcon({
|
|
||||||
icon: visible.value ? 'ant-design:up-outlined' : 'ant-design:down-outlined'
|
|
||||||
})}
|
|
||||||
>
|
|
||||||
{t(visible.value ? 'common.shrink' : 'common.expand')}
|
|
||||||
</ElButton>
|
|
||||||
) : null}
|
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -164,9 +149,14 @@ onMounted(async () => {
|
||||||
const elFormExpose = await getElFormExpose()
|
const elFormExpose = await getElFormExpose()
|
||||||
emit('register', formExpose, elFormExpose)
|
emit('register', formExpose, elFormExpose)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
defineExpose({
|
||||||
|
getElFormExpose
|
||||||
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
|
<div ref="searchRef">
|
||||||
<Form
|
<Form
|
||||||
ref="formExpose"
|
ref="formExpose"
|
||||||
:model="formModel"
|
:model="formModel"
|
||||||
|
@ -181,25 +171,15 @@ onMounted(async () => {
|
||||||
|
|
||||||
<template v-if="layout === 'bottom'">
|
<template v-if="layout === 'bottom'">
|
||||||
<div :style="bottomButtonStyle">
|
<div :style="bottomButtonStyle">
|
||||||
<ElButton
|
<ActionButton
|
||||||
v-if="showSearch"
|
:show-reset="showReset"
|
||||||
type="primary"
|
:show-search="showSearch"
|
||||||
:icon="useIcon({ icon: 'ep:search' })"
|
:show-expand="showExpand"
|
||||||
@click="search"
|
@expand="setVisible"
|
||||||
>
|
@reset="reset"
|
||||||
{{ t('common.query') }}
|
@search="search"
|
||||||
</ElButton>
|
/>
|
||||||
<ElButton v-if="showReset" :icon="useIcon({ icon: 'ep:refresh-right' })" @click="reset">
|
|
||||||
{{ t('common.reset') }}
|
|
||||||
</ElButton>
|
|
||||||
<ElButton
|
|
||||||
v-if="expand"
|
|
||||||
:icon="useIcon({ icon: visible ? 'ant-design:up-outlined' : 'ant-design:down-outlined' })"
|
|
||||||
text
|
|
||||||
@click="setVisible"
|
|
||||||
>
|
|
||||||
{{ t(visible ? 'common.shrink' : 'common.expand') }}
|
|
||||||
</ElButton>
|
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
|
@ -0,0 +1,52 @@
|
||||||
|
<script setup lang="ts">
|
||||||
|
import { ElButton } from 'element-plus'
|
||||||
|
import { useIcon } from '@/hooks/web/useIcon'
|
||||||
|
import { propTypes } from '@/utils/propTypes'
|
||||||
|
import { useI18n } from '@/hooks/web/useI18n'
|
||||||
|
|
||||||
|
const emit = defineEmits(['search', 'reset', 'expand'])
|
||||||
|
|
||||||
|
const { t } = useI18n()
|
||||||
|
|
||||||
|
defineProps({
|
||||||
|
showSearch: propTypes.bool.def(true),
|
||||||
|
showReset: propTypes.bool.def(true),
|
||||||
|
// 是否显示伸缩
|
||||||
|
showExpand: propTypes.bool.def(false),
|
||||||
|
visible: propTypes.bool.def(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
const onSearch = () => {
|
||||||
|
emit('search')
|
||||||
|
}
|
||||||
|
|
||||||
|
const onReset = () => {
|
||||||
|
emit('reset')
|
||||||
|
}
|
||||||
|
|
||||||
|
const onExpand = () => {
|
||||||
|
emit('expand')
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<ElButton
|
||||||
|
v-if="showSearch"
|
||||||
|
type="primary"
|
||||||
|
:icon="useIcon({ icon: 'ep:search' })"
|
||||||
|
@click="onSearch"
|
||||||
|
>
|
||||||
|
{{ t('common.query') }}
|
||||||
|
</ElButton>
|
||||||
|
<ElButton v-if="showReset" :icon="useIcon({ icon: 'ep:refresh-right' })" @click="onReset">
|
||||||
|
{{ t('common.reset') }}
|
||||||
|
</ElButton>
|
||||||
|
<ElButton
|
||||||
|
v-if="showExpand"
|
||||||
|
:icon="useIcon({ icon: visible ? 'ant-design:up-outlined' : 'ant-design:down-outlined' })"
|
||||||
|
text
|
||||||
|
@click="onExpand"
|
||||||
|
>
|
||||||
|
{{ t(visible ? 'common.shrink' : 'common.expand') }}
|
||||||
|
</ElButton>
|
||||||
|
</template>
|
|
@ -199,7 +199,7 @@ const handleSearch = (data: any) => {
|
||||||
:is-col="isGrid"
|
:is-col="isGrid"
|
||||||
:layout="layout"
|
:layout="layout"
|
||||||
:button-position="buttonPosition"
|
:button-position="buttonPosition"
|
||||||
expand
|
show-expand
|
||||||
expand-field="field6"
|
expand-field="field6"
|
||||||
@search="handleSearch"
|
@search="handleSearch"
|
||||||
@reset="handleSearch"
|
@reset="handleSearch"
|
||||||
|
|
Loading…
Reference in New Issue