-
Notifications
You must be signed in to change notification settings - Fork 7
/
ForeignKeySelect.tsx
167 lines (153 loc) · 5.57 KB
/
ForeignKeySelect.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Это легаси
/* eslint-disable react/jsx-props-no-spreading */
import React, { useEffect } from 'react'
import type { MenuPlacement } from 'react-select'
import { BoxProps } from '@chakra-ui/react'
import type { Accessor, DetailObject, WidgetProps } from '../../typing'
import { useWidgetInitialization } from '../../common/hooks/useWidgetInitialization'
import { ValidationWrapper } from '../../common/components/ValidationWrapper'
import { AsyncSelectWidget } from '../../common/components/AsyncSelectWidget'
import { WidgetWrapper } from '../../common/components/WidgetWrapper'
import { getAccessor, getCopyHandler, getPayload } from '../utils/dataAccess'
import { ExtendedProps } from '../../common/components/ReactSelectCustomization'
import { useCreateTestId } from '../../django-spa/aspects'
import { Provider } from '../../admin/providers'
export type ForeignKeySelectWidgetProps = WidgetProps &
ExtendedProps & {
optionLabel: Function
optionValue: Function
isClearable?: boolean
isDisabled?: Accessor<boolean>
defaultOptions?: boolean
searchParamName?: string
styles?: Accessor<object>
optionLabelMenu?: (option: unknown, mainObject: DetailObject) => string
optionLabelValue?: (option: unknown, mainObject: DetailObject) => string
placeholder?: string
containerProps?: BoxProps
labelContainerProps?: BoxProps
staleTime?: Accessor<number>
allowAllDefinedValues?: Accessor<boolean>
provider?: Provider
menuPlacement?: MenuPlacement
}
/**
* Render select-input with async loaded options from backend
* onChange - can return full loaded model for option, not just key
*
* Based on AsyncSelectWidget, so have common props.
*
* props.optionLabel - got loaded model and return label for option
* props.optionValue - got loaded model and return label for value
* props.targetPayload - got selected model and return widget payload
* props.styles - react-select styles
* props.optionLabelMenu - got loaded model and return menu label for option
* props.optionLabelValue - got loaded model and return value label for option
*
* @param props - widget props
*/
const ForeignKeySelectWidget = (props: ForeignKeySelectWidgetProps): JSX.Element => {
const {
name,
mainDetailObject,
provider,
helpText,
targetPayload,
optionLabel,
optionValue,
isClearable = false,
defaultOptions = false,
style,
styles,
setInitialValue,
submitChange,
notBlockingValidators = [],
blockingValidators = [],
containerStore,
useClipboard,
copyValue,
searchParamName,
notifier,
cacheTime,
optionLabelMenu,
optionLabelValue,
isDisabled = false,
placeholder,
containerProps,
labelContainerProps,
staleTime,
widgetClassName,
componentsClasses,
allowAllDefinedValues,
menuPlacement,
} = props
const context = containerStore.getState()
const { targetUrl, content, dataResourceUrl, isRequired, widgetDescription } = useWidgetInitialization(
{ ...props, context },
{ allowAllDefinedValues: getAccessor(allowAllDefinedValues) }
)
const effectiveCacheTime = getAccessor(cacheTime, mainDetailObject, context)
const selectStyle = getAccessor(styles, mainDetailObject, context)
const [value, setValue] = React.useState<object | null>(content as object | null)
setInitialValue(value ? getPayload(value, name, targetPayload) : null)
useEffect(() => {
setValue(content as object | null)
}, [content])
const handleChangeValue = (changeValue: React.ChangeEvent<HTMLInputElement>): void => {
setValue(changeValue)
const widgetPayload = getPayload(changeValue, name, targetPayload)
submitChange({ url: targetUrl, payload: widgetPayload })
}
const handleCopyValue = getCopyHandler(value, copyValue, () => optionLabel(value, mainDetailObject))
const { getDataTestId } = useCreateTestId()
return (
<WidgetWrapper
name={name}
style={style}
helpText={helpText}
description={widgetDescription}
useClipboard={useClipboard}
copyValue={handleCopyValue}
notifier={notifier}
required={isRequired}
containerProps={containerProps}
labelContainerProps={labelContainerProps}
{...getDataTestId(props)}
>
<ValidationWrapper
notBlockingValidators={notBlockingValidators}
blockingValidators={blockingValidators}
provider={provider}
detailObject={mainDetailObject}
context={context as Record<string, unknown>}
>
<AsyncSelectWidget
cacheTime={effectiveCacheTime}
dataResourceUrl={dataResourceUrl}
handleChange={handleChangeValue}
value={value}
isClearable={isClearable}
defaultOptions={defaultOptions}
getOptionLabel={(val: object | null) => optionLabel(val, mainDetailObject)}
getOptionValue={optionValue}
getOptionLabelMenu={
optionLabelMenu ? (val: object | null) => optionLabelMenu(val, mainDetailObject) : undefined
}
getOptionLabelValue={
optionLabelValue ? (val: object | null) => optionLabelValue(val, mainDetailObject) : undefined
}
searchParamName={searchParamName}
styles={selectStyle}
isDisabled={getAccessor(isDisabled, mainDetailObject, context)}
placeholder={placeholder}
staleTime={staleTime}
className={widgetClassName}
componentsClasses={componentsClasses}
name={name}
menuPlacement={menuPlacement}
/>
</ValidationWrapper>
</WidgetWrapper>
)
}
export { ForeignKeySelectWidget }