This repository has been archived by the owner on Jan 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
FlatTreePopup.tsx
118 lines (104 loc) · 4.2 KB
/
FlatTreePopup.tsx
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
/*
* TESLER-UI
* Copyright (C) 2018-2020 Tesler Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import React, { ComponentType } from 'react'
import { useDispatch, useSelector, shallowEqual } from 'react-redux'
import { ListChildComponentProps } from 'react-window'
import { WidgetTableMeta } from '../../../interfaces/widget'
import { FlatTree } from './FlatTree'
import PickListPopup from '../PickListPopup/PickListPopup'
import { Store } from '../../../interfaces/store'
import { $do } from '../../../actions/actions'
import { PopupFooter } from '../../../components/ui/Popup/PopupFooter'
import { useSingleSelect } from './useSingleSelect'
import { useMultipleSelect } from './useMultipleSelect'
/**
* Properties for `FlatTreePopup` widget
*/
export interface FlatTreePopupProps {
/**
* Widget configuration
*/
meta: WidgetTableMeta
/**
* Customization of items renderer
*/
children?: ComponentType<ListChildComponentProps>
}
/**
* Popup widget dislaying tree-like data with items expandable into nested subtrees as
* flat virtualized list of items.
*
* Data must be presorted (every parent is followed by its descendants) for this widget.
*
* @param props Widget props
* @category Widgets
*/
export const FlatTreePopup: React.FC<FlatTreePopupProps> = ({ meta, children }) => {
const { multiple, hierarchyGroupSelection, hierarchyGroupDeselection, hierarchyRadioAll, hierarchyRadio: hierarchyRootRadio } =
meta.options ?? {}
const bcName = meta.bcName
const dispatch = useDispatch()
const { parentBcName, parentCursor } = useSelector((store: Store) => {
const parentName = store.screen.bo.bc[meta.bcName].parentName
const parentBc = store.screen.bo.bc[parentName]
return {
parentBcName: parentName,
parentCursor: parentBc.cursor
}
}, shallowEqual)
const pickListDescriptor = useSelector((store: Store) => {
return store.view.pickMap
}, shallowEqual)
const handleSingleSelect = useSingleSelect(pickListDescriptor, bcName, parentCursor, parentBcName)
const handleMultipleSelect = useMultipleSelect(
bcName,
hierarchyGroupSelection,
hierarchyGroupDeselection,
hierarchyRadioAll,
hierarchyRootRadio
)
const handleSelect = multiple ? handleMultipleSelect : handleSingleSelect
const handleConfirmMultiple = React.useCallback(() => {
dispatch($do.saveAssociations({ bcNames: [bcName] }))
dispatch($do.bcCancelPendingChanges({ bcNames: [bcName] }))
dispatch($do.closeViewPopup(null))
}, [bcName, dispatch])
const handleCancelMultiple = React.useCallback(() => {
dispatch($do.closeViewPopup(null))
dispatch($do.bcRemoveAllFilters({ bcName }))
dispatch($do.bcCancelPendingChanges({ bcNames: [bcName] }))
}, [bcName, dispatch])
const footer = React.useMemo(() => {
return multiple ? <PopupFooter onAccept={handleConfirmMultiple} onCancel={handleCancelMultiple} /> : null
}, [multiple, handleCancelMultiple, handleConfirmMultiple])
const components = React.useMemo(() => {
return {
table: (
<FlatTree meta={meta} multiple={multiple} onSelect={handleSelect}>
{children}
</FlatTree>
),
footer
}
}, [meta, footer, multiple, children, handleSelect])
return <PickListPopup widget={meta} components={components} disableScroll />
}
/**
* @category Widgets
*/
const MemoizedFlatTreePopup = React.memo(FlatTreePopup)
export default MemoizedFlatTreePopup