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
/
MultivalueField.tsx
132 lines (123 loc) · 4.49 KB
/
MultivalueField.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import React, { FunctionComponent } from 'react'
import { connect, useSelector } from 'react-redux'
import { Dispatch } from 'redux'
import { Store } from '../../interfaces/store'
import { $do } from '../../actions/actions'
import { MultivalueSingleValue } from '../../interfaces/data'
import MultivalueTag from '../ui/Multivalue/MultivalueTag'
import { MultivalueFieldMeta } from '../../interfaces/widget'
import { WidgetTypes } from '@tesler-ui/schema'
export interface MultivalueFieldOwnProps {
widgetName: string // TODO: for future pagination support
defaultValue: MultivalueSingleValue[]
widgetFieldMeta: MultivalueFieldMeta
bcName: string
disabled?: boolean
metaError?: string
placeholder?: string
}
export interface MultivalueFieldProps extends MultivalueFieldOwnProps {
cursor: string
page: number
popupBcName: string
/**
* @deprecated TODO: Remove in 2.0.0; initially existed to prevent race condition
* when opening popup that still fetches row meta, but after introducing lazy load
* for popup lost its relevance
*/
popupRowMetaDone?: boolean
fieldKey: string
onRemove: (
bcName: string,
popupBcName: string,
cursor: string,
associateFieldKey: string,
newValue: MultivalueSingleValue[],
removedItem: MultivalueSingleValue
) => void
onMultivalueAssocOpen: (bcName: string, widgetFieldMeta: MultivalueFieldMeta, page: number, widgetName?: string) => void
}
/**
*
* @param props
* @category Components
*/
const MultivalueField: FunctionComponent<MultivalueFieldProps> = props => {
// TODO 2.0.0: assocWidget should be found by widgetName
const assocWidget = useSelector((store: Store) =>
store.view.widgets.find(widget => widget.bcName === props.popupBcName && widget.type === WidgetTypes.AssocListPopup)
)
const onRemove = (newValue: MultivalueSingleValue[], removedItem: MultivalueSingleValue) => {
props.onRemove(props.bcName, props.popupBcName, props.cursor, props.fieldKey, newValue, removedItem)
}
return (
<MultivalueTag
widgetName={assocWidget?.name}
onPopupOpen={props.onMultivalueAssocOpen}
onChange={onRemove}
value={props.defaultValue}
disabled={props.disabled}
widgetFieldMeta={props.widgetFieldMeta}
page={props.page}
bcName={props.bcName}
metaError={props.metaError}
placeholder={props.placeholder}
/>
)
}
function mapStateToProps(store: Store, ownProps: MultivalueFieldOwnProps) {
// TODO: for future pagination support
// const widget = store.view.widgets.find(widget => widget.name === ownProps.widgetName)
// const bc = store.screen.bo.bc[widget.bcName]
const popupBcName = ownProps.widgetFieldMeta.popupBcName
return {
cursor: store.screen.bo.bc[ownProps.bcName]?.cursor,
page: 0,
popupBcName: popupBcName,
assocValueKey: ownProps.widgetFieldMeta.assocValueKey,
fieldKey: ownProps.widgetFieldMeta.key
}
}
function mapDispatchToProps(dispatch: Dispatch) {
return {
onMultivalueAssocOpen: (bcName: string, widgetFieldMeta: MultivalueFieldMeta, page: number, widgetName?: string) => {
// TODO: for future pagination support
// if (page !== 1) {
// dispatch($do.componentChangePage({page: 1, bcName: popupBcName, isPopup: true}))
// }
dispatch(
$do.showViewPopup({
assocValueKey: widgetFieldMeta.assocValueKey,
bcName: widgetFieldMeta.popupBcName,
calleeBCName: bcName,
associateFieldKey: widgetFieldMeta.key,
widgetName
})
)
},
onRemove: (
bcName: string,
popupBcName: string,
cursor: string,
associateFieldKey: string,
dataItem: MultivalueSingleValue[],
removedItem: MultivalueSingleValue
) => {
dispatch(
$do.removeMultivalueTag({
bcName,
popupBcName,
cursor,
associateFieldKey,
dataItem,
removedItem
})
)
}
}
}
/**
* @category Components
*/
const ConnectedMultivalueField = connect(mapStateToProps, mapDispatchToProps)(MultivalueField)
export default ConnectedMultivalueField