/
MapField.tsx
151 lines (132 loc) · 5.29 KB
/
MapField.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
import React from "react";
import { FieldProps, Properties, Property } from "../../models";
import {
Box,
FormControl,
FormHelperText,
Grid,
InputLabel,
MenuItem,
Paper,
Select
} from "@mui/material";
import { SelectChangeEvent } from "@mui/material/Select";
import { pick } from "../../core/util/objects";
import { FieldDescription, LabelWithIcon } from "../components";
import { useClearRestoreValue } from "../../hooks";
import { buildPropertyField } from "../form_factory";
import { isHidden } from "../../core/utils";
/**
* Field that renders the children property fields
*
* This is one of the internal components that get mapped natively inside forms
* and tables to the specified properties.
* @category Form fields
*/
export function MapField<T extends object>({
name,
value,
showError,
disabled,
property,
setValue,
tableMode,
includeDescription,
underlyingValueHasChanged,
context
}: FieldProps<T>) {
const pickOnlySomeKeys = property.config?.pickOnlySomeKeys || false;
if (!property.properties) {
throw Error(`You need to specify a 'properties' prop (or specify a custom field) in your map property ${name}`);
}
let mapProperties: Record<string, Property>;
if (!pickOnlySomeKeys) {
mapProperties = property.properties as Properties;
} else if (value) {
mapProperties = pick(property.properties as Properties,
...Object.keys(value)
.filter(key => key in property.properties!)
);
} else {
mapProperties = {};
}
useClearRestoreValue({
property,
value,
setValue
});
function buildPickKeysSelect() {
const keys = Object.keys(property.properties!)
.filter((key) => !value || !(key in value));
const handleAddProperty = (event: SelectChangeEvent) => {
setValue({
...value,
[event.target.value as string]: null
});
};
if (!keys.length) return <></>;
return <Box m={1}>
<FormControl fullWidth>
<InputLabel>Add property</InputLabel>
<Select
variant={"standard"}
value={""}
disabled={disabled}
onChange={handleAddProperty}>
{keys.map((key) => (
<MenuItem key={key} value={key}>
{(property.properties as Properties)[key].title || key}
</MenuItem>
))}
</Select>
</FormControl>
</Box>;
}
return (
<FormControl fullWidth error={showError}>
{!tableMode && <FormHelperText filled
required={property.validation?.required}>
<LabelWithIcon property={property}/>
</FormHelperText>}
<Paper elevation={0} variant={"outlined"} sx={(theme) => ({
elevation: 0,
padding: theme.spacing(2),
[theme.breakpoints.up("md")]: {
padding: theme.spacing(2)
}
})}>
<Grid container spacing={2}>
{Object.entries(mapProperties)
.filter(([_, property]) => !isHidden(property))
.map(([entryKey, childProperty], index) => {
return (
<Grid item
sm={12}
xs={12}
key={`map-${name}-${index}`}>
{
buildPropertyField<any, T>({
name: `${name}[${entryKey}]`,
disabled,
property: childProperty,
includeDescription,
underlyingValueHasChanged,
context,
tableMode,
partOfArray: false,
autoFocus: false,
shouldAlwaysRerender: false
})
}
</Grid>
);
}
)}
</Grid>
{pickOnlySomeKeys && buildPickKeysSelect()}
</Paper>
{includeDescription &&
<FieldDescription property={property}/>}
</FormControl>
);
}