From 629e1ec36b36d61fe7b5a42f1464e54b40b64968 Mon Sep 17 00:00:00 2001 From: Clement Zhang Date: Mon, 4 Nov 2024 16:44:53 +0100 Subject: [PATCH] change input type --- .../components/inputs/AppMultiSelectV2.svelte | 55 ++++++++++++++----- .../apps/editor/component/components.ts | 7 ++- 2 files changed, 46 insertions(+), 16 deletions(-) diff --git a/frontend/src/lib/components/apps/components/inputs/AppMultiSelectV2.svelte b/frontend/src/lib/components/apps/components/inputs/AppMultiSelectV2.svelte index 261fea0317..356d9cafb7 100644 --- a/frontend/src/lib/components/apps/components/inputs/AppMultiSelectV2.svelte +++ b/frontend/src/lib/components/apps/components/inputs/AppMultiSelectV2.svelte @@ -30,21 +30,23 @@ const { app, worldStore, selectedComponent, componentControl } = getContext('AppViewerContext') - let items: string[] + let items: { label: string; value: string; created?: boolean }[] const resolvedConfig = initConfig( - components['multiselectcomponent'].initialData.configuration, + components['multiselectcomponentv2'].initialData.configuration, configuration ) const outputs = initOutput($worldStore, id, { - result: [] as string[] + result: [] as { label: string; value: string; created?: boolean }[] }) - let value: string[] | undefined = [...new Set(outputs?.result.peak())] as string[] + let value: { label: string; value: string; created?: boolean }[] | undefined = [ + ...new Set(outputs?.result.peak()) + ] as { label: string; value: string; created?: boolean }[] $componentControl[id] = { - setValue(nvalue: string[]) { + setValue(nvalue: { label: string; value: string; created?: boolean }[]) { value = [...new Set(nvalue)] outputs?.result.set([...(value ?? [])]) } @@ -53,9 +55,21 @@ $: resolvedConfig.items && handleItems() function handleItems() { - if (Array.isArray(resolvedConfig.items)) { - items = resolvedConfig.items?.map((label) => { - return typeof label === 'string' ? label : `NOT_STRING` + if (!Array.isArray(resolvedConfig.items)) { + items = [] + } else { + items = resolvedConfig.items?.map((item) => { + if (!item || typeof item !== 'object') { + console.error('Select component items should be an array of objects') + return { + label: 'not object', + value: 'not object' + } + } + return { + label: item?.label ?? 'undefined', + value: item?.value != undefined ? JSON.stringify(item.value) : 'undefined' + } }) } } @@ -63,13 +77,26 @@ $: resolvedConfig.defaultItems && handleDefaultItems() function handleDefaultItems() { - if (Array.isArray(resolvedConfig.defaultItems)) { - const nvalue = resolvedConfig.defaultItems?.map((label) => { - return typeof label === 'string' ? label : `NOT_STRING` + let nvalue: { label: string; value: string; created?: boolean }[] + if (!Array.isArray(resolvedConfig.defaultItems)) { + nvalue = [] + } else { + nvalue = resolvedConfig.defaultItems?.map((item) => { + if (!item || typeof item !== 'object') { + console.error('Select component items should be an array of objects') + return { + label: 'not object', + value: 'not object' + } + } + return { + label: item?.label ?? 'undefined', + value: item?.value != undefined ? JSON.stringify(item.value) : 'undefined' + } }) - value = [...new Set(nvalue)] - outputs?.result.set([...(value ?? [])]) } + value = [...new Set(nvalue)] + outputs?.result.set([...(value ?? [])]) } let css = initCss($app.css?.multiselectcomponent, customCss) @@ -181,7 +208,7 @@ e.target?.['parentElement']?.dispatchEvent(newe) }} > - {option} + {option.label} diff --git a/frontend/src/lib/components/apps/editor/component/components.ts b/frontend/src/lib/components/apps/editor/component/components.ts index 34b6faca78..5fff829890 100644 --- a/frontend/src/lib/components/apps/editor/component/components.ts +++ b/frontend/src/lib/components/apps/editor/component/components.ts @@ -2199,8 +2199,11 @@ This is a paragraph. items: { type: 'static', fieldType: 'array', - subFieldType: 'text', - value: ['Foo', 'Bar'] + subFieldType: 'labeledselect', + value: [ + { value: 'foo', label: 'Foo' }, + { value: 'bar', label: 'Bar' } + ] } as StaticAppInput, defaultItems: { type: 'static',