Skip to content

Commit 16f2fd5

Browse files
authored
Merge pull request #29 from webgme/fix-array-anyof-add
Fix array anyOf add
2 parents 1f1c7d4 + 0e40673 commit 16f2fd5

File tree

3 files changed

+31
-20
lines changed

3 files changed

+31
-20
lines changed

src/lib/Control.svelte

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
<script context="module" lang="ts">
2+
function useConst(schema: JSONSchema7 | undefined) {
3+
return (schema != null) && !isBoolean(schema) && ("const" in schema);
4+
}
5+
6+
function useAnyOf(schema: JSONSchema7 | undefined) {
7+
return (schema?.anyOf != null) && (schema?.properties == null);
8+
}
9+
10+
export function getSingleSchemaType(schema: JSONSchema7 | undefined) {
11+
const type = useConst(schema) ? "const"
12+
: useAnyOf(schema) ? "anyOf"
13+
: schema?.type;
14+
return (Array.isArray(type) ? type[0] : type) ?? "object";
15+
}
16+
</script>
17+
118
<script lang="ts">
219
import type { JSONSchema7 } from "json-schema";
320
import type UISchema from "./UISchema";
@@ -13,19 +30,8 @@
1330
1431
$: updateControlType(schema);
1532
16-
function useConst(schema: JSONSchema7 | undefined) {
17-
return (schema != null) && !isBoolean(schema) && ("const" in schema);
18-
}
19-
20-
function useAnyOf(schema: JSONSchema7 | undefined) {
21-
return (schema?.anyOf != null) && (schema?.properties == null);
22-
}
23-
2433
function updateControlType(schema: JSONSchema7 | undefined) {
25-
const type = useConst(schema) ? "const"
26-
: useAnyOf(schema) ? "anyOf"
27-
: schema?.type;
28-
const singleType = (Array.isArray(type) ? type[0] : type) ?? "object";
34+
const singleType = getSingleSchemaType(schema);
2935
const updatedControl = controls[singleType as keyof typeof controls] as any;
3036
if (updatedControl != control) {
3137
control = updatedControl;

src/lib/controls/AnyOfControl.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
7676
function resetSelectedProps() {
7777
const newSelectedProps = isObjSchema() ? Object.keys(selected?.properties ?? {}) : undefined;
78-
if (newSelectedProps !== selectedProps) {
78+
if (!deepEquals(newSelectedProps?.sort(), selectedProps?.sort())) {
7979
selectedProps = newSelectedProps;
8080
}
8181
}
@@ -103,7 +103,7 @@
103103
</Title>
104104
<Content class="jsonschema-form-controls">
105105
{#if selected != null}
106-
{#if !!selectedProps}
106+
{#if isObjSchema()}
107107
<ObjectProps {...selected} bind:data {uischema} />
108108
{:else}
109109
<Control schema={selected} bind:data {uischema} force />

src/lib/controls/ArrayControl.svelte

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
import type { JSONSchema7 } from "json-schema";
33
import UISchema from "$lib/UISchema";
44
import { hasRequired as checkRequired, isBoolean } from "$lib/utilities";
5-
import Accordion, { Panel, Header, Content } from '@smui-extra/accordion';
5+
import Accordion, { Panel, Header } from '@smui-extra/accordion';
66
import IconButton, { Icon } from "@smui/icon-button";
7-
import Control from "../Control.svelte";
7+
import Control, { getSingleSchemaType } from "../Control.svelte";
88
99
export let data: any[] | undefined = undefined;
1010
export let uischema: UISchema = {};
@@ -49,18 +49,19 @@
4949
$: updateOpen(enabled);
5050
$: updateOpen($uiOptions.collapse);
5151
52+
const dataKeys: Symbol[] = [];
53+
5254
function getKey(index: number) {
53-
const value = data![index];
54-
const useIndex = (value == null) || (typeof value !== "object");
55-
return useIndex ? `${index} | ${getType(index) ?? ""}` : value;
55+
return dataKeys[index] ??= Symbol();
5656
}
5757
5858
function getItem(index: number) {
5959
return (prefixed.length > index) ? prefixed[index] : additional;
6060
}
6161
6262
function getType(index: number) {
63-
return getItem(index)?.type;
63+
const item = getItem(index);
64+
return getSingleSchemaType(item);
6465
}
6566
6667
function canRemoveItem(index: number) {
@@ -78,25 +79,29 @@
7879
function addItem() {
7980
if (canAddItem) {
8081
data = [...(data ?? []), undefined];
82+
dataKeys.push(Symbol());
8183
}
8284
}
8385
8486
function removeItem(index: number) {
8587
if (canRemoveItem(index)) {
8688
data?.splice(index, 1);
8789
data = (ignoreEmpty && (data?.length ?? 0) === 0) ? undefined : data;
90+
dataKeys.splice(index, 1);
8891
}
8992
}
9093
9194
function moveItemUp(index: number) {
9295
if (canMoveItemUp(index)) {
9396
[data![index - 1], data![index]] = [data![index], data![index - 1]];
97+
[dataKeys[index - 1], dataKeys[index]] = [dataKeys[index], dataKeys[index - 1]];
9498
}
9599
}
96100
97101
function moveItemDown(index: number) {
98102
if (canMoveItemDown(index)) {
99103
[data![index + 1], data![index]] = [data![index], data![index + 1]];
104+
[dataKeys[index + 1], dataKeys[index]] = [dataKeys[index], dataKeys[index + 1]];
100105
}
101106
}
102107

0 commit comments

Comments
 (0)