-
Notifications
You must be signed in to change notification settings - Fork 23
/
AdditionalPropertiesSchemaField.tsx
284 lines (266 loc) · 8.46 KB
/
AdditionalPropertiesSchemaField.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import {
FieldProps,
FormContextType,
RJSFSchema,
StrictRJSFSchema,
} from '@rjsf/utils'
import { isEqual } from 'lodash-es'
import React, { useEffect, useState } from 'react'
import FullWidthAlert from '../../FullWidthAlert/FullWidthAlert'
import { convertToArray } from '../AnnotationEditorUtils'
import { Grid, InputLabel } from '@mui/material'
import { AdditionalPropertyContextProvider } from '../template/AdditionalPropertyContext'
// Matches ####-##-##T##:##:##.###Z, e.g. 1970-01-01T12:00:000Z
const ISO_TIMESTAMP_REGEX = /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z/
// Types that correspond to the different input fields that the annotation editor supports
const propertyTypeArray = [
'String',
'Integer',
'Float',
'Boolean',
'Datetime',
] as const
export type PropertyType = (typeof propertyTypeArray)[number]
export function guessPropertyType(list: Array<unknown>): PropertyType {
if (list.length === 0) {
// The field was just added, so default to string
return 'String'
} else if (
list.every(
item => typeof item === 'number' || item === 'NaN', // "NaN" is technically a float value
)
) {
if (list.every(item => Number.isInteger(item))) {
return 'Integer'
} else {
return 'Float'
}
} else if (list.every(item => typeof item === 'boolean')) {
return 'Boolean'
} else if (
list.every(item => typeof item === 'string') &&
(list as string[]).every((item: string) => !!ISO_TIMESTAMP_REGEX.exec(item))
) {
return 'Datetime'
}
// otherwise, default type is 'string'
return 'String'
}
export function transformDataFromPropertyType(
list: Array<any>,
propertyType: PropertyType,
) {
switch (propertyType) {
case 'Integer':
return list.map(item =>
Number.isNaN(Number(item)) ? 0 : Math.floor(Number(item)),
)
case 'Float':
return list.map(item => {
const asFloat = parseFloat(item as string)
if (Number.isNaN(asFloat)) {
return 'NaN'
} else if (Number.isInteger(asFloat)) {
return asFloat.toFixed(1)
} else {
return asFloat
}
})
case 'Datetime':
return list.map(item => {
if (typeof item === 'string' && ISO_TIMESTAMP_REGEX.exec(item)) {
return item
} else {
return new Date().toISOString()
}
})
case 'Boolean':
return list.map(item => !!item)
case 'String':
default:
return list.map(item => String(item))
}
}
/**
* Maps a Synapse Annotation PropertyType to a JSON Schema that captures the type and format.
* @param propertyType
*/
export function getSchemaForPropertyType(propertyType: PropertyType) {
switch (propertyType) {
case 'Datetime':
return { type: 'string', format: 'datetime' }
case 'Boolean':
return { type: 'boolean' }
case 'Float':
return { type: 'number' }
case 'Integer':
return { type: 'integer' }
case 'String':
default:
return { type: 'string' }
}
}
/**
* react-jsonschema-form SchemaField override for "additionalProperties" only. In Synapse these are "custom annotations".
* Modifies the data to provide full compatibility with Synapse annotations features.
*
* This component provides these enhancements to the SchemaField:
* - Supports selecting a type, and changing the input widget appropriately
* - Identifying the type on mount
* - Treat all field values as arrays
* - When the last array value is removed, remove the entire key from the form.
* @param props
* @returns
*/
export function AdditionalPropertiesSchemaField<
T extends Array<unknown> = Array<unknown>,
S extends StrictRJSFSchema = RJSFSchema,
F extends FormContextType = any,
>(
props: FieldProps<T, S, F> & {
onDropPropertyClick: (key: string) => (event: any) => void
},
) {
const {
formData,
onChange,
registry,
schema,
name,
onDropPropertyClick,
idSchema,
} = props
const id = idSchema.$id
const { ArrayField } = registry.fields
const { SelectWidget } = registry.widgets
// The type determines which widget we show.
const [propertyType, setPropertyType] = useState(
guessPropertyType(convertToArray(formData)),
)
// If the property type is updated, store it in a new variable where we'll show a warning if data may be lost on coersion
const [nextPropertyType, setNextPropertyType] = useState(propertyType)
/**
* This effect is invoked whenever the user attempts to change the datatype of a custom annotation.
*/
useEffect(() => {
function onNextPropertyTypeUpdate() {
if (Array.isArray(formData)) {
const dataIsEmpty =
formData.length === 0 ||
formData.every(item => item == null || item == '')
const coercedList = transformDataFromPropertyType(
formData,
nextPropertyType,
)
// if the data is empty or identical after conversion, then just update the property type
if (dataIsEmpty || nextPropertyType !== propertyType) {
if (isEqual(formData, coercedList)) {
setPropertyType(nextPropertyType)
}
}
}
}
onNextPropertyTypeUpdate()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [nextPropertyType])
/**
* This effect is invoked whenever the propertyType changes.
*/
useEffect(() => {
function coerceDataAndUpdateWidget() {
if (Array.isArray(formData)) {
if (formData.every(item => item == null)) {
onDropPropertyClick(name)(new CustomEvent('dropEmptyProperty'))
} else {
const coercedList = transformDataFromPropertyType(
formData,
nextPropertyType,
) as unknown as T
// Data conversion is non-destructive or has been confirmed by the user
setPropertyType(nextPropertyType)
// Coerce the data to match the new type
onChange(coercedList)
}
}
}
coerceDataAndUpdateWidget()
// Don't add other properties to dependency array because we don't want to automatically coerce input
// i.e. Only coerce data when the type changes, which should only be on mount or when the user explicitly chooses a new type.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [propertyType])
const itemsSchema = getSchemaForPropertyType(propertyType)
// If the additional property has not yet been coerced to an array, trigger an update to coerce and render nothing.
// We must do this because RJSF does not currently batch these kinds of updates
// See https://github.com/rjsf-team/react-jsonschema-form/issues/3367
if (!Array.isArray(formData)) {
onChange(convertToArray(formData) as unknown as T)
return <></>
}
return (
<AdditionalPropertyContextProvider
value={{
dropProperty: e => {
onDropPropertyClick(name)(e)
},
}}
>
<Grid item xs={2}>
<InputLabel htmlFor={`${id}-type`}>Type</InputLabel>
<SelectWidget
name={'Type'}
id={`${id}-type`}
schema={{} as S}
options={{
enumOptions: propertyTypeArray.map(type => ({
label: type,
value: type,
})),
}}
value={propertyType}
onChange={value => {
setNextPropertyType(value as PropertyType)
}}
disabled={props.disabled}
readOnly={props.readonly}
required={true}
isClearable={false}
onBlur={() => {}}
onFocus={() => {}}
registry={registry}
label={'Type'}
/>
</Grid>
<Grid item xs={7}>
<ArrayField
{...props}
schema={{
...schema,
items: {
...itemsSchema,
},
}}
/>
</Grid>
{propertyType !== nextPropertyType && (
<FullWidthAlert
variant="warning"
title="Data may be lost when converting types"
description={`Are you sure you want to convert ${name} from ${propertyType} to ${nextPropertyType}? Current values may be lost on conversion.`}
primaryButtonConfig={{
text: 'Convert',
onClick: () => {
setPropertyType(nextPropertyType)
},
}}
secondaryButtonConfig={{
text: 'Cancel',
onClick: () => {
setNextPropertyType(propertyType)
},
}}
isGlobal={false}
/>
)}
</AdditionalPropertyContextProvider>
)
}