-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
dataviews.js
151 lines (143 loc) · 3.7 KB
/
dataviews.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* WordPress dependencies
*/
import { __experimentalHStack as HStack } from '@wordpress/components';
import { useMemo, useState, useCallback, useEffect } from '@wordpress/element';
/**
* Internal dependencies
*/
import Pagination from './pagination';
import ViewActions from './view-actions';
import Filters from './filters';
import Search from './search';
import { VIEW_LAYOUTS, LAYOUT_TABLE, LAYOUT_GRID } from './constants';
import BulkActions from './bulk-actions';
import { normalizeFields } from './normalize-fields';
const defaultGetItemId = ( item ) => item.id;
const defaultOnSelectionChange = () => {};
function useSomeItemHasAPossibleBulkAction( actions, data ) {
return useMemo( () => {
return data.some( ( item ) => {
return actions.some( ( action ) => {
return action.supportsBulk && action.isEligible( item );
} );
} );
}, [ actions, data ] );
}
export default function DataViews( {
view,
onChangeView,
fields,
search = true,
searchLabel = undefined,
actions = [],
data,
getItemId = defaultGetItemId,
isLoading = false,
paginationInfo,
supportedLayouts,
onSelectionChange = defaultOnSelectionChange,
onDetailsChange = null,
deferredRendering = false,
} ) {
const [ selection, setSelection ] = useState( [] );
const [ openedFilter, setOpenedFilter ] = useState( null );
useEffect( () => {
if (
selection.length > 0 &&
selection.some(
( id ) => ! data.some( ( item ) => getItemId( item ) === id )
)
) {
const newSelection = selection.filter( ( id ) =>
data.some( ( item ) => getItemId( item ) === id )
);
setSelection( newSelection );
onSelectionChange(
data.filter( ( item ) =>
newSelection.includes( getItemId( item ) )
)
);
}
}, [ selection, data, getItemId, onSelectionChange ] );
const onSetSelection = useCallback(
( items ) => {
setSelection( items.map( ( item ) => getItemId( item ) ) );
onSelectionChange( items );
},
[ setSelection, getItemId, onSelectionChange ]
);
const ViewComponent = VIEW_LAYOUTS.find(
( v ) => v.type === view.type
).component;
const _fields = useMemo( () => normalizeFields( fields ), [ fields ] );
const hasPossibleBulkAction = useSomeItemHasAPossibleBulkAction(
actions,
data
);
return (
<div className="dataviews-wrapper">
<HStack
alignment="top"
justify="start"
className="dataviews-filters__view-actions"
>
<HStack
justify="start"
className="dataviews-filters__container"
wrap
>
{ search && (
<Search
label={ searchLabel }
view={ view }
onChangeView={ onChangeView }
/>
) }
<Filters
fields={ _fields }
view={ view }
onChangeView={ onChangeView }
openedFilter={ openedFilter }
setOpenedFilter={ setOpenedFilter }
/>
</HStack>
{ [ LAYOUT_TABLE, LAYOUT_GRID ].includes( view.type ) &&
hasPossibleBulkAction && (
<BulkActions
actions={ actions }
data={ data }
onSelectionChange={ onSetSelection }
selection={ selection }
getItemId={ getItemId }
/>
) }
<ViewActions
fields={ _fields }
view={ view }
onChangeView={ onChangeView }
supportedLayouts={ supportedLayouts }
/>
</HStack>
<ViewComponent
fields={ _fields }
view={ view }
onChangeView={ onChangeView }
actions={ actions }
data={ data }
getItemId={ getItemId }
isLoading={ isLoading }
onSelectionChange={ onSetSelection }
onDetailsChange={ onDetailsChange }
selection={ selection }
deferredRendering={ deferredRendering }
setOpenedFilter={ setOpenedFilter }
/>
<Pagination
view={ view }
onChangeView={ onChangeView }
paginationInfo={ paginationInfo }
/>
</div>
);
}