Skip to content

Commit 25fd635

Browse files
committed
feat: schema path, better d&d
1 parent f9d9a58 commit 25fd635

File tree

9 files changed

+926
-154
lines changed

9 files changed

+926
-154
lines changed

lib/components/alternate.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import '@shoelace-style/shoelace/dist/components/option/option.js';
99
// NOTE: Experimental!
1010
export const alternateField = (
1111
schema: JSONSchema7,
12-
data: any,
12+
data: unknown,
1313
path: Path,
14-
uiState: any,
14+
uiState: unknown,
1515
uiSchema: UiSchema,
1616
handleChange: Jsf['_handleChange'],
1717
dig: Jsf['_dig'],
@@ -27,7 +27,7 @@ export const alternateField = (
2727
<sl-select
2828
value=${0}
2929
@sl-change=${(e: Event) => {
30-
handleChange([...path], {});
30+
handleChange([...path], {}, schemaPath);
3131
updateUi(
3232
[...path, 'alternative'],
3333
Number((e.target as HTMLSelectElement)?.value),

lib/components/array.ts

+22-8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import '@shoelace-style/shoelace/dist/components/button-group/button-group.js';
1212
import '@shoelace-style/shoelace/dist/components/tooltip/tooltip.js';
1313

1414
import type { Jsf, Path, UiSchema } from '../json-schema-form.js';
15+
import { debuggerInline } from './utils.js';
1516

1617
export const arrayField = (
1718
schema: JSONSchema7,
@@ -27,20 +28,24 @@ export const arrayField = (
2728

2829
return html` <!-- -->
2930
<fieldset part="array" class="array">
30-
${JSON.stringify(schemaPath)} arr
31+
${debuggerInline({ schemaPath, path })}
3132
<!-- -->
3233
<legend>${schema.title}</legend>
34+
3335
${dataLevel?.map?.((_item, index) => {
3436
if (
3537
typeof schema.items !== 'object' ||
3638
Array.isArray(schema.items) ||
3739
!Array.isArray(dataLevel)
3840
)
3941
return '';
42+
const schemaPathAugmented = [...schemaPath];
43+
schemaPathAugmented.push('items');
44+
4045
return html` <sl-card
4146
@dragover=${(event: DragEvent) => {
4247
event.preventDefault();
43-
// event.stopPropagation();
48+
event.stopPropagation();
4449
const dataTransfer = event.dataTransfer;
4550
if (dataTransfer) dataTransfer.dropEffect = 'move';
4651
@@ -49,29 +54,32 @@ export const arrayField = (
4954
// ?.setAttribute('data-dropzone', '');
5055
}}
5156
@dragenter=${(event: DragEvent) => {
52-
// event.stopPropagation();
57+
event.stopPropagation();
5358
(event.target as HTMLElement)
5459
.closest('sl-card')
5560
?.setAttribute('data-dropzone', '');
5661
}}
5762
@dragleave=${(event: DragEvent) => {
58-
// event.stopPropagation();
63+
event.stopPropagation();
5964
(event.target as HTMLElement)
6065
.closest('sl-card')
6166
?.removeAttribute('data-dropzone');
6267
}}
6368
@drop=${(event: DragEvent) => {
64-
// event.stopPropagation();
69+
event.stopPropagation();
6570
const idx = event.dataTransfer?.getData('integer');
6671
if (!idx) return;
6772
const originIndex = Number.parseInt(idx, 10);
73+
// dataLevel ||= [];
6874
if (!Array.isArray(dataLevel)) return;
6975
const hold = dataLevel[index] as unknown;
7076
// eslint-disable-next-line no-param-reassign
7177
dataLevel[index] = dataLevel[originIndex] as unknown;
7278
// eslint-disable-next-line no-param-reassign
7379
dataLevel[originIndex] = hold;
74-
handleChange([...path], dataLevel, schemaPath);
80+
handleChange([...path], dataLevel, schemaPathAugmented);
81+
82+
console.log({ originIndex, idx });
7583
7684
(event.target as HTMLElement)
7785
.closest('sl-card')
@@ -84,7 +92,7 @@ export const arrayField = (
8492
[...path, index],
8593
uiState,
8694
uiSchema,
87-
schemaPath,
95+
schemaPathAugmented,
8896
)}
8997
9098
<div slot="header" class="array-card-header">
@@ -184,7 +192,13 @@ export const arrayField = (
184192
dataLevel.push(schema.items?.default || []);
185193
}
186194
187-
handleChange([...path], dataLevel, schemaPath);
195+
// const schemaPathAugmented = [...schemaPath];
196+
// schemaPathAugmented.push('items');
197+
handleChange(
198+
[...path, dataLevel.length - 1],
199+
dataLevel[dataLevel.length - 1],
200+
schemaPath,
201+
);
188202
}}
189203
size="large"
190204
>

0 commit comments

Comments
 (0)