Merge pull request #4085 from Budibase/fix/4064
Allow deleting JSON fields in JSON schema
This commit is contained in:
commit
cdaab9e989
|
@ -9,6 +9,7 @@
|
||||||
Select,
|
Select,
|
||||||
Body,
|
Body,
|
||||||
Layout,
|
Layout,
|
||||||
|
ActionButton,
|
||||||
} from "@budibase/bbui"
|
} from "@budibase/bbui"
|
||||||
import { onMount, createEventDispatcher } from "svelte"
|
import { onMount, createEventDispatcher } from "svelte"
|
||||||
import { FIELDS } from "constants/backend"
|
import { FIELDS } from "constants/backend"
|
||||||
|
@ -20,8 +21,8 @@
|
||||||
let dispatcher = createEventDispatcher()
|
let dispatcher = createEventDispatcher()
|
||||||
let mode = "Form"
|
let mode = "Form"
|
||||||
let fieldCount = 0
|
let fieldCount = 0
|
||||||
let fieldKeys = {},
|
let fieldKeys = [],
|
||||||
fieldTypes = {}
|
fieldTypes = []
|
||||||
let keyValueOptions = [
|
let keyValueOptions = [
|
||||||
{ label: "String", value: FIELDS.STRING.type },
|
{ label: "String", value: FIELDS.STRING.type },
|
||||||
{ label: "Number", value: FIELDS.NUMBER.type },
|
{ label: "Number", value: FIELDS.NUMBER.type },
|
||||||
|
@ -50,27 +51,48 @@
|
||||||
if (!schema) {
|
if (!schema) {
|
||||||
schema = {}
|
schema = {}
|
||||||
}
|
}
|
||||||
let i = 0
|
// find the entries which aren't in the list
|
||||||
for (let [key, value] of Object.entries(schema)) {
|
const schemaEntries = Object.entries(schema).filter(
|
||||||
fieldKeys[i] = key
|
([key]) => !fieldKeys.includes(key)
|
||||||
fieldTypes[i] = value.type
|
)
|
||||||
i++
|
for (let [key, value] of schemaEntries) {
|
||||||
|
fieldKeys.push(key)
|
||||||
|
fieldTypes.push(value.type)
|
||||||
}
|
}
|
||||||
fieldCount = i
|
fieldCount = fieldKeys.length
|
||||||
}
|
}
|
||||||
|
|
||||||
function saveSchema() {
|
function saveSchema() {
|
||||||
for (let i of Object.keys(fieldKeys)) {
|
const newSchema = {}
|
||||||
const key = fieldKeys[i]
|
for (let [index, key] of fieldKeys.entries()) {
|
||||||
// they were added to schema, rather than generated
|
// they were added to schema, rather than generated
|
||||||
if (!schema[key]) {
|
newSchema[key] = {
|
||||||
schema[key] = {
|
...schema[key],
|
||||||
type: fieldTypes[i],
|
type: fieldTypes[index],
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
dispatcher("save", { schema: newSchema, json })
|
||||||
|
schema = newSchema
|
||||||
|
}
|
||||||
|
|
||||||
dispatcher("save", { schema, json })
|
function removeKey(index) {
|
||||||
|
const keyToRemove = fieldKeys[index]
|
||||||
|
if (fieldKeys[index + 1] != null) {
|
||||||
|
fieldKeys[index] = fieldKeys[index + 1]
|
||||||
|
fieldTypes[index] = fieldTypes[index + 1]
|
||||||
|
}
|
||||||
|
fieldKeys.splice(index, 1)
|
||||||
|
fieldTypes.splice(index, 1)
|
||||||
|
fieldCount--
|
||||||
|
if (json) {
|
||||||
|
try {
|
||||||
|
const parsed = JSON.parse(json)
|
||||||
|
delete parsed[keyToRemove]
|
||||||
|
json = JSON.stringify(parsed, null, 2)
|
||||||
|
} catch (err) {
|
||||||
|
// json not valid, ignore
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
|
@ -97,6 +119,7 @@
|
||||||
getOptionValue={field => field.value}
|
getOptionValue={field => field.value}
|
||||||
getOptionLabel={field => field.label}
|
getOptionLabel={field => field.label}
|
||||||
/>
|
/>
|
||||||
|
<ActionButton icon="Close" quiet on:click={() => removeKey(i)} />
|
||||||
</div>
|
</div>
|
||||||
{/each}
|
{/each}
|
||||||
<div class:add-field-btn={fieldCount !== 0}>
|
<div class:add-field-btn={fieldCount !== 0}>
|
||||||
|
@ -118,9 +141,9 @@
|
||||||
<style>
|
<style>
|
||||||
.horizontal {
|
.horizontal {
|
||||||
display: grid;
|
display: grid;
|
||||||
grid-template-columns: 30% 1fr;
|
grid-template-columns: 30% 1fr 40px;
|
||||||
grid-gap: var(--spacing-s);
|
grid-gap: var(--spacing-s);
|
||||||
align-items: center;
|
align-items: end;
|
||||||
}
|
}
|
||||||
|
|
||||||
.add-field-btn {
|
.add-field-btn {
|
||||||
|
|
Loading…
Reference in New Issue