feat: 完善search组件demo

This commit is contained in:
kailong321200875 2023-06-25 13:49:11 +08:00
parent a7f3702144
commit cdf44a43a0
8 changed files with 112 additions and 18 deletions

View File

@ -340,7 +340,7 @@ export default defineComponent({
<ElForm <ElForm
ref={elFormRef} ref={elFormRef}
{...getFormBindValue()} {...getFormBindValue()}
model={props.isCustom ? props.model : formModel} model={unref(getProps).isCustom ? unref(getProps).model : formModel}
class={prefixCls} class={prefixCls}
> >
{{ {{

View File

@ -37,7 +37,9 @@ const props = defineProps({
model: { model: {
type: Object as PropType<Recordable>, type: Object as PropType<Recordable>,
default: () => ({}) default: () => ({})
} },
searchLoading: propTypes.bool.def(false),
resetLoading: propTypes.bool.def(false)
}) })
const emit = defineEmits(['search', 'reset', 'register']) const emit = defineEmits(['search', 'reset', 'register'])
@ -48,9 +50,10 @@ const visible = ref(true)
const formModel = ref<Recordable>({}) const formModel = ref<Recordable>({})
const newSchema = computed(() => { const newSchema = computed(() => {
let schema: FormSchema[] = cloneDeep(unref(getProps).schema) const propsComputed = unref(getProps)
if (unref(getProps).showExpand && unref(getProps).expandField && !unref(visible)) { let schema: FormSchema[] = cloneDeep(propsComputed.schema)
const index = findIndex(schema, (v: FormSchema) => v.field === unref(getProps).expandField) if (propsComputed.showExpand && propsComputed.expandField && !unref(visible)) {
const index = findIndex(schema, (v: FormSchema) => v.field === propsComputed.expandField)
schema.map((v, i) => { schema.map((v, i) => {
if (i >= index) { if (i >= index) {
v.hidden = true v.hidden = true
@ -60,7 +63,7 @@ const newSchema = computed(() => {
return v return v
}) })
} }
if (unref(getProps).layout === 'inline') { if (propsComputed.layout === 'inline') {
schema = schema.concat([ schema = schema.concat([
{ {
field: 'action', field: 'action',
@ -71,9 +74,11 @@ const newSchema = computed(() => {
return ( return (
<div> <div>
<ActionButton <ActionButton
showSearch={unref(getProps).showSearch} showSearch={propsComputed.showSearch}
showReset={unref(getProps).showReset} showReset={propsComputed.showReset}
showExpand={unref(getProps).showExpand} showExpand={propsComputed.showExpand}
searchLoading={propsComputed.searchLoading}
resetLoading={propsComputed.resetLoading}
visible={visible.value} visible={visible.value}
onExpand={setVisible} onExpand={setVisible}
onReset={reset} onReset={reset}
@ -91,7 +96,7 @@ const newSchema = computed(() => {
}) })
const { formRegister, formMethods } = useForm() const { formRegister, formMethods } = useForm()
const { getElFormExpose, getFormData } = formMethods const { getElFormExpose, getFormData, getFormExpose } = formMethods
// useSearchprops // useSearchprops
const outsideProps = ref<SearchProps>({}) const outsideProps = ref<SearchProps>({})
@ -171,8 +176,10 @@ const setSchema = (schemaProps: FormSetProps[]) => {
} }
// //
const setValues = (data: Recordable = {}) => { const setValues = async (data: Recordable = {}) => {
formModel.value = Object.assign(unref(formModel), data) formModel.value = Object.assign(unref(formModel), data)
const formExpose = await getFormExpose()
formExpose?.setValues(data)
} }
const delSchema = (field: string) => { const delSchema = (field: string) => {
@ -227,6 +234,8 @@ defineExpose(defaultExpose)
:show-reset="getProps.showReset" :show-reset="getProps.showReset"
:show-search="getProps.showSearch" :show-search="getProps.showSearch"
:show-expand="getProps.showExpand" :show-expand="getProps.showExpand"
:search-loading="getProps.searchLoading"
:reset-loading="getProps.resetLoading"
@expand="setVisible" @expand="setVisible"
@reset="reset" @reset="reset"
@search="search" @search="search"

View File

@ -12,7 +12,9 @@ defineProps({
showSearch: propTypes.bool.def(true), showSearch: propTypes.bool.def(true),
showReset: propTypes.bool.def(true), showReset: propTypes.bool.def(true),
showExpand: propTypes.bool.def(false), showExpand: propTypes.bool.def(false),
visible: propTypes.bool.def(true) visible: propTypes.bool.def(true),
searchLoading: propTypes.bool.def(false),
resetLoading: propTypes.bool.def(false)
}) })
const onSearch = () => { const onSearch = () => {
@ -32,12 +34,18 @@ const onExpand = () => {
<ElButton <ElButton
v-if="showSearch" v-if="showSearch"
type="primary" type="primary"
:loading="searchLoading"
:icon="useIcon({ icon: 'ep:search' })" :icon="useIcon({ icon: 'ep:search' })"
@click="onSearch" @click="onSearch"
> >
{{ t('common.query') }} {{ t('common.query') }}
</ElButton> </ElButton>
<ElButton v-if="showReset" :icon="useIcon({ icon: 'ep:refresh-right' })" @click="onReset"> <ElButton
v-if="showReset"
:loading="resetLoading"
:icon="useIcon({ icon: 'ep:refresh-right' })"
@click="onReset"
>
{{ t('common.reset') }} {{ t('common.reset') }}
</ElButton> </ElButton>
<ElButton <ElButton

View File

@ -115,6 +115,11 @@ export const useForm = () => {
getElFormExpose: async () => { getElFormExpose: async () => {
await getForm() await getForm()
return unref(elFormRef) return unref(elFormRef)
},
getFormExpose: async () => {
await getForm()
return unref(formRef)
} }
} }

View File

@ -371,7 +371,13 @@ export default {
left: 'left', left: 'left',
center: 'center', center: 'center',
right: 'right', right: 'right',
dynamicOptions: 'Dynamic options' dynamicOptions: 'Dynamic options',
// 删除单选框
deleteRadio: 'Delete radio',
// 还原单选框
restoreRadio: 'Restore radio',
loading: 'Loading',
reset: 'Reset'
}, },
stickyDemo: { stickyDemo: {
sticky: 'Sticky' sticky: 'Sticky'

View File

@ -368,7 +368,13 @@ export default {
left: '左', left: '左',
center: '中', center: '中',
right: '右', right: '右',
dynamicOptions: '动态选项' dynamicOptions: '动态选项',
// 删除单选框
deleteRadio: '删除单选框',
// 还原单选框
restoreRadio: '还原单选框',
loading: '加载中',
reset: '重置'
}, },
stickyDemo: { stickyDemo: {
sticky: '黏性' sticky: '黏性'

View File

@ -306,7 +306,7 @@ const inoutValidation = async () => {
</template> </template>
<style lang="less" scoped> <style lang="less" scoped>
.el-button + .el-button { .el-button {
margin-top: 10px; margin-top: 10px;
} }
</style> </style>

View File

@ -11,7 +11,7 @@ import { useSearch } from '@/hooks/web/useSearch'
const { t } = useI18n() const { t } = useI18n()
const { searchRegister, searchMethods } = useSearch() const { searchRegister, searchMethods } = useSearch()
const { setSchema, setProps } = searchMethods const { setSchema, setProps, setValues } = searchMethods
const schema = reactive<FormSchema[]>([ const schema = reactive<FormSchema[]>([
{ {
@ -166,6 +166,48 @@ const getDictOne = async () => {
const handleSearch = (data: any) => { const handleSearch = (data: any) => {
console.log(data) console.log(data)
} }
const delRadio = () => {
setSchema([
{
field: 'field3',
path: 'remove',
value: true
}
])
}
const restoreRadio = () => {
setSchema([
{
field: 'field3',
path: 'remove',
value: false
}
])
}
const setValue = () => {
setValues({
field1: 'Joy'
})
}
const searchLoading = ref(false)
const changeSearchLoading = () => {
searchLoading.value = true
setTimeout(() => {
searchLoading.value = false
}, 2000)
}
const resetLoading = ref(false)
const changeResetLoading = () => {
resetLoading.value = true
setTimeout(() => {
resetLoading.value = false
}, 2000)
}
</script> </script>
<template> <template>
@ -192,7 +234,17 @@ const handleSearch = (data: any) => {
{{ t('searchDemo.bottom') }} {{ t('searchDemo.position') }}-{{ t('searchDemo.right') }} {{ t('searchDemo.bottom') }} {{ t('searchDemo.position') }}-{{ t('searchDemo.right') }}
</ElButton> </ElButton>
<ElButton @click="getDictOne"> <ElButton @click="getDictOne">
{{ t('searchDemo.dynamicOptions') }} {{ t('formDemo.select') }} {{ t('searchDemo.dynamicOptions') }}
</ElButton>
<ElButton @click="delRadio">{{ t('searchDemo.deleteRadio') }}</ElButton>
<ElButton @click="restoreRadio">{{ t('searchDemo.restoreRadio') }}</ElButton>
<ElButton @click="setValue">{{ t('formDemo.setValue') }}</ElButton>
<ElButton @click="changeSearchLoading">
{{ t('searchDemo.search') }} {{ t('searchDemo.loading') }}
</ElButton>
<ElButton @click="changeResetLoading">
{{ t('searchDemo.reset') }} {{ t('searchDemo.loading') }}
</ElButton> </ElButton>
</ContentWrap> </ContentWrap>
@ -202,6 +254,8 @@ const handleSearch = (data: any) => {
:is-col="isGrid" :is-col="isGrid"
:layout="layout" :layout="layout"
:button-position="buttonPosition" :button-position="buttonPosition"
:search-loading="searchLoading"
:reset-loading="resetLoading"
show-expand show-expand
expand-field="field6" expand-field="field6"
@search="handleSearch" @search="handleSearch"
@ -210,3 +264,9 @@ const handleSearch = (data: any) => {
/> />
</ContentWrap> </ContentWrap>
</template> </template>
<style lang="less" scoped>
.el-button {
margin-top: 10px;
}
</style>