Browse Source

feat(BasicTable/Form): register 改为 props 传递

BasicTable register 改为 props 以便获得更好的 TS 泛型执行
main
刘凯 11 months ago
parent
commit
f8454f0e30
  1. 7
      src/components/Form/src/BasicForm.vue
  2. 3
      src/components/Form/src/props.ts
  3. 9
      src/components/Table/src/BasicTable.vue
  4. 10
      src/components/Table/src/hooks/useTable.ts
  5. 4
      src/components/Table/src/props.ts

7
src/components/Form/src/BasicForm.vue

@ -254,7 +254,12 @@ const formActionType: FormActionType = {
onMounted(() => {
initDefault()
emit('register', formActionType)
if (props.register) {
props.register(formActionType)
}
else {
emit('register', formActionType)
}
})
const getFormActionBindProps = computed(() => ({ ...getProps.value, ...advanceState }) as InstanceType<typeof FormAction>['$props'])

3
src/components/Form/src/props.ts

@ -1,12 +1,13 @@
import type { CSSProperties, PropType } from 'vue'
import type { ButtonProps } from 'ant-design-vue/es/button/buttonTypes'
import type { RowProps } from 'ant-design-vue/lib/grid/Row'
import type { FieldMapToTime, FormSchema } from './types/form'
import type { FieldMapToTime, FormActionType, FormSchema } from './types/form'
import type { ColEx } from './types'
import type { TableActionType } from '@/components/Table'
import { propTypes } from '@/utils/propTypes'
export const basicProps = {
register: Function as PropType<(instance: FormActionType) => void>,
model: {
type: Object as PropType<Recordable>,
default: () => ({}),

9
src/components/Table/src/BasicTable.vue

@ -286,7 +286,12 @@ createTableContext({ ...tableAction, wrapRef, getBindValues })
defineExpose(tableAction)
emit('register', tableAction, formActions)
if (props.register) {
props.register(tableAction, formActions)
}
else {
emit('register', tableAction, formActions)
}
</script>
<template>
@ -297,7 +302,7 @@ emit('register', tableAction, formActions)
submit-on-reset
v-bind="getFormProps"
:table-action="tableAction"
@register="registerForm"
:register="registerForm"
@submit="handleSearchInfoChange"
@advanced-change="redoHeight"
>

10
src/components/Table/src/hooks/useTable.ts

@ -11,23 +11,19 @@ import { error } from '@/utils/log'
type Props<T> = Partial<DynamicProps<BasicTableProps<T>>>
type UseTableMethod = TableActionType & {
getForm: () => FormActionType
}
export function useTable<T>(tableProps?: Props<T>): [
(instance: TableActionType<T>, formInstance: UseTableMethod) => void,
(instance: TableActionType<T>, formInstance: FormActionType) => void,
TableActionType<T> & {
getForm: () => FormActionType
},
] {
const tableRef = ref<Nullable<TableActionType<T>>>(null)
const loadedRef = ref<Nullable<boolean>>(false)
const formRef = ref<Nullable<UseTableMethod>>(null)
const formRef = ref<Nullable<FormActionType>>(null)
let stopWatch: WatchStopHandle
function register(instance: TableActionType<T>, formInstance: UseTableMethod) {
function register(instance: TableActionType<T>, formInstance: FormActionType) {
isProdMode()
&& onUnmounted(() => {
tableRef.value = null

4
src/components/Table/src/props.ts

@ -5,18 +5,20 @@ import type {
FetchSetting,
SizeType,
SorterResult,
TableActionType,
TableCustomRecord,
TableRowSelection,
TableSetting,
} from './types/table'
import { DEFAULT_FILTER_FN, DEFAULT_SIZE, DEFAULT_SORT_FN, FETCH_SETTING } from './const'
import type { EditRecordRow } from './components/editable'
import type { FormProps } from '@/components/Form'
import type { FormActionType, FormProps } from '@/components/Form'
import { propTypes } from '@/utils/propTypes'
export function defineTableProps<T>() {
return {
register: Function as PropType<(instance: TableActionType<T>, formInstance: FormActionType) => void>,
clickToRowSelect: { type: Boolean, default: true },
isTreeTable: Boolean,
tableSetting: propTypes.shape<TableSetting>({}),

Loading…
Cancel
Save