Skip to content

Commit

Permalink
fix: 修复Search组件无法默认值
Browse files Browse the repository at this point in the history
  • Loading branch information
kailong321200875 committed Nov 21, 2022
1 parent f37cc1b commit 3368fda
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 6 deletions.
10 changes: 8 additions & 2 deletions src/components/Search/src/Search.vue
Expand Up @@ -33,7 +33,11 @@ const props = defineProps({
expand: propTypes.bool.def(false),
// 伸缩的界限字段
expandField: propTypes.string.def(''),
inline: propTypes.bool.def(true)
inline: propTypes.bool.def(true),
model: {
type: Object as PropType<Recordable>,
default: () => ({})
}
})
const emit = defineEmits(['search', 'reset'])
Expand Down Expand Up @@ -62,7 +66,9 @@ const newSchema = computed(() => {
return schema
})
const { register, elFormRef, methods } = useForm()
const { register, elFormRef, methods } = useForm({
model: props.model || {}
})
const search = async () => {
await unref(elFormRef)?.validate(async (isValid) => {
Expand Down
3 changes: 3 additions & 0 deletions src/hooks/web/useForm.ts
Expand Up @@ -41,6 +41,9 @@ export const useForm = (props?: FormProps) => {
setProps: async (props: FormProps = {}) => {
const form = await getForm()
form?.setProps(props)
if (props.model) {
form?.setValues(props.model)
}
},

setValues: async (data: Recordable) => {
Expand Down
6 changes: 5 additions & 1 deletion src/hooks/web/useTable.ts
Expand Up @@ -23,6 +23,8 @@ interface UseTableConfig<T = any> {
list: string
total?: string
}
// 默认传递的参数
defaultParams?: Recordable
props?: TableProps
}

Expand All @@ -47,7 +49,9 @@ export const useTable = <T = any>(config?: UseTableConfig<T>) => {
// 表格数据
tableList: [],
// AxiosConfig 配置
params: {},
params: {
...(config?.defaultParams || {})
},
// 加载中
loading: true,
// 当前行的数据
Expand Down
10 changes: 8 additions & 2 deletions src/views/Components/Table/DefaultTable.vue
Expand Up @@ -31,7 +31,8 @@ const columns: TableColumn[] = [
},
{
field: 'display_time',
label: t('tableDemo.displayTime')
label: t('tableDemo.displayTime'),
sortable: true
},
{
field: 'importance',
Expand Down Expand Up @@ -90,7 +91,12 @@ const actionFn = (data: TableSlotDefault) => {

<template>
<ContentWrap :title="t('tableDemo.table')" :message="t('tableDemo.tableDes')">
<Table :columns="columns" :data="tableDataList" :loading="loading">
<Table
:columns="columns"
:data="tableDataList"
:loading="loading"
:defaultSort="{ prop: 'display_time', order: 'descending' }"
>
<template #action="data">
<ElButton type="primary" @click="actionFn(data as TableSlotDefault)">
{{ t('tableDemo.action') }}
Expand Down
10 changes: 9 additions & 1 deletion src/views/Example/Dialog/ExampleDialog.vue
Expand Up @@ -20,6 +20,9 @@ const { register, tableObject, methods } = useTable<TableData>({
response: {
list: 'list',
total: 'total'
},
defaultParams: {
title: 's'
}
})
Expand Down Expand Up @@ -212,7 +215,12 @@ const save = async () => {

<template>
<ContentWrap>
<Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />
<Search
:model="{ title: 's' }"
:schema="allSchemas.searchSchema"
@search="setSearchParams"
@reset="setSearchParams"
/>

<div class="mb-10px">
<ElButton type="primary" @click="AddAction">{{ t('exampleDemo.add') }}</ElButton>
Expand Down

0 comments on commit 3368fda

Please sign in to comment.