JSON: fix array support

This commit is contained in:
Sidney Alcantara
2021-10-22 16:14:54 +11:00
parent e4c4f0c7ea
commit cbcbc2e2f5
2 changed files with 23 additions and 17 deletions

View File

@@ -1,21 +1,25 @@
import { Switch, FormControlLabel } from "@mui/material";
import { Checkbox, FormControlLabel, FormHelperText } from "@mui/material";
const Settings = ({ config, handleChange }) => {
return (
<>
<FormControlLabel
control={
<Switch
<Checkbox
checked={config.isArray}
onChange={() => handleChange("isArray")(!Boolean(config.isArray))}
name="isArray"
/>
}
label="Set as array"
sx={{
alignItems: "center",
"& .MuiFormControlLabel-label": { mt: 0 },
}}
label={
<>
Default as array
<FormHelperText>
You can still set individual field values as a JSON object or
array using the code editor
</FormHelperText>
</>
}
/>
</>
);

View File

@@ -46,9 +46,17 @@ export default function Json({
control={control}
name={column.key}
render={({ field: { onChange, value } }) => {
const formattedJson = value
? jsonFormat(value, { type: "space", char: " ", size: 2 })
: "";
const sanitizedValue =
value !== undefined && isValidJson(value)
? value
: column.config?.isArray
? []
: {};
const formattedJson = jsonFormat(sanitizedValue, {
type: "space",
char: " ",
size: 2,
});
if (disabled)
return (
@@ -93,13 +101,7 @@ export default function Json({
style={{ overflowX: "auto", ...theme.typography.caption }}
>
<ReactJson
src={
value !== undefined && isValidJson(value)
? value
: column.config?.isArray
? []
: {}
}
src={sanitizedValue}
onEdit={handleEdit}
onAdd={handleEdit}
onDelete={handleEdit}