1
- import { computed , defineComponent , ref , Ref } from '@vue/composition-api'
1
+ import {
2
+ computed ,
3
+ defineComponent ,
4
+ ref ,
5
+ Ref ,
6
+ provide ,
7
+ inject ,
8
+ } from '@vue/composition-api'
2
9
import {
3
10
GeneralField ,
4
11
IVoidFieldFactoryProps ,
@@ -13,7 +20,7 @@ import {
13
20
Fragment ,
14
21
} from '@formily/vue'
15
22
import { observer } from '@formily/reactive-vue'
16
- import { isArr , isBool } from '@formily/shared'
23
+ import { isArr , isBool , isFn } from '@formily/shared'
17
24
import { ArrayBase } from '../array-base'
18
25
import { stylePrefix } from '../__builtins__/configs'
19
26
import { composeExport } from '../__builtins__/shared'
@@ -65,6 +72,14 @@ type ColumnProps = ElColumnProps & {
65
72
} ) => VNode
66
73
}
67
74
75
+ interface PaginationAction {
76
+ totalPage ?: number
77
+ pageSize ?: number
78
+ changePage ?: ( page : number ) => void
79
+ }
80
+
81
+ const PaginationSymbol = Symbol ( 'pagination' )
82
+
68
83
const isColumnComponent = ( schema : Schema ) => {
69
84
return schema [ 'x-component' ] ?. indexOf ( 'Column' ) > - 1
70
85
}
@@ -320,6 +335,10 @@ const StatusSelect = observer(
320
335
}
321
336
)
322
337
338
+ const usePagination = ( ) => {
339
+ return inject < Ref < PaginationAction > > ( PaginationSymbol , ref ( { } ) )
340
+ }
341
+
323
342
const ArrayTablePagination = defineComponent < IArrayTablePaginationProps > ( {
324
343
inheritAttrs : false ,
325
344
props : [ 'pageSize' , 'dataSource' ] ,
@@ -396,6 +415,15 @@ const ArrayTablePagination = defineComponent<IArrayTablePaginationProps>({
396
415
)
397
416
}
398
417
418
+ const paginationContext = computed < PaginationAction > ( ( ) => {
419
+ return {
420
+ totalPage : totalPage . value ,
421
+ pageSize : pageSize . value ,
422
+ changePage : ( page : number ) => ( current . value = page ) ,
423
+ }
424
+ } )
425
+ provide ( PaginationSymbol , paginationContext )
426
+
399
427
return ( ) => {
400
428
return h (
401
429
Fragment ,
@@ -556,11 +584,41 @@ const ArrayTableColumn: Component = {
556
584
} ,
557
585
}
558
586
587
+ const ArrayAddition = defineComponent ( {
588
+ name : 'ArrayAddition' ,
589
+ setup ( props , { attrs, listeners, slots } ) {
590
+ const array = ArrayBase . useArray ( )
591
+ const paginationRef = usePagination ( )
592
+
593
+ const onClick = listeners [ 'click' ]
594
+ listeners [ 'click' ] = ( e ) => {
595
+ const { totalPage = 0 , pageSize = 10 , changePage } = paginationRef . value
596
+ // 如果添加数据后超过当前页,则自动切换到下一页
597
+ const total = array ?. field ?. value ?. value . length || 0
598
+ if ( total === ( totalPage - 1 ) * pageSize + 1 && isFn ( changePage ) ) {
599
+ changePage ( totalPage )
600
+ }
601
+ if ( onClick ) onClick ( e )
602
+ }
603
+ return ( ) => {
604
+ return h (
605
+ ArrayBase . Addition ,
606
+ {
607
+ props,
608
+ attrs,
609
+ on : listeners ,
610
+ } ,
611
+ slots
612
+ )
613
+ }
614
+ } ,
615
+ } )
616
+
559
617
export const ArrayTable = composeExport ( ArrayTableInner , {
560
618
Column : ArrayTableColumn ,
561
619
Index : ArrayBase . Index ,
562
620
SortHandle : ArrayBase . SortHandle ,
563
- Addition : ArrayBase . Addition ,
621
+ Addition : ArrayAddition ,
564
622
Remove : ArrayBase . Remove ,
565
623
MoveDown : ArrayBase . MoveDown ,
566
624
MoveUp : ArrayBase . MoveUp ,
0 commit comments