avoid ghost options on labeled toggle, tiny cleaning

This commit is contained in:
Clement Zhang
2024-11-06 17:37:10 +01:00
parent d74e406a2d
commit 7d52eecad8
3 changed files with 22 additions and 41 deletions
@@ -27,8 +27,6 @@
// every option is labeled, or no one is.
type Options = ObjectOption[] | string[]
$: resolvedConfig.items && handleItems()
const [floatingRef, floatingContent] = createFloatingActions({
strategy: 'absolute',
middleware: [offset(5), flip(), shift()]
@@ -43,6 +41,21 @@
configuration
)
$: handleItems(resolvedConfig.items)
function handleItems(resolvedConfigItems) {
if (!resolvedConfigItems) {
return
}
items = []
value = []
if (isObjectOptionArray(resolvedConfigItems)) {
items = parseLabeledItems(resolvedConfigItems)
} else if (isStringArray(resolvedConfigItems)) {
items = parseStringItems(resolvedConfigItems)
}
}
const outputs = initOutput($worldStore, id, {
result: isObjectOptionArray(items)
? ([] as ObjectOption[])
@@ -91,12 +104,8 @@
return arr.every((item) => typeof item === 'string')
}
function handleLabeledItems() {
if (!Array.isArray(resolvedConfig.items)) {
items = []
return
}
items = resolvedConfig.items?.map((item) => {
function parseLabeledItems(resolvedConfigItems: ObjectOption[]) {
return resolvedConfigItems?.map((item) => {
if (!item || typeof item !== 'object') {
console.error(
'When labeled, MultiSelect component items should be an array of { label: string, value: string }.'
@@ -114,12 +123,8 @@
})
}
function handleStringItems() {
if (!Array.isArray(resolvedConfig.items)) {
items = []
return
}
items = resolvedConfig.items?.map((item) => {
function parseStringItems(resolvedConfigItems: string[]) {
return resolvedConfigItems?.map((item) => {
if (!item || typeof item !== 'string') {
console.error(
'When not labeled, MultiSelect component items should be an array of strings.'
@@ -130,14 +135,6 @@
})
}
function handleItems() {
if (isObjectOptionArray(resolvedConfig.items)) {
handleLabeledItems()
} else if (isStringArray(resolvedConfig.items)) {
handleStringItems()
}
}
$: resolvedConfig.defaultItems && handleDefaultItems()
// todo
@@ -235,7 +232,6 @@
{/each}
<InitializeComponent {id} />
<AlignWrapper {render} hFull {verticalAlignment}>
<div
class="w-full app-editor-input"
@@ -267,6 +263,7 @@
on:open={multiSelectProps.onOpen}
on:close={multiSelectProps.onClose}
let:option
id="objectoption-multiselect"
>
<!-- needed because portal doesn't work for mouseup event en mobile -->
<!-- svelte-ignore a11y-no-static-element-interactions -->
@@ -297,6 +294,7 @@
on:open={multiSelectProps.onOpen}
on:close={multiSelectProps.onClose}
let:option
id="simplestring-multiselect"
>
<!-- needed because portal doesn't work for mouseup event en mobile -->
<!-- svelte-ignore a11y-no-static-element-interactions -->
@@ -308,7 +306,7 @@
e.target?.['parentElement']?.dispatchEvent(newe)
}}
>
{option.label}
{option}
</div>
</MultiSelect>
{/if}
@@ -293,19 +293,6 @@
</script>
<div class="flex gap-2 flex-col mt-2 w-full">
<button
on:click={() =>
console.log(
'labeled =',
labeled,
'items =',
items,
'componentInput =',
componentInput,
'subFieldType =',
subFieldType
)}>console.log in ArrayStaticInputEditor</button
>
{#if Array.isArray(items) && componentInput.value}
<div class="flex flex-row items-center justify-between">
<div class="text-xs text-tertiary font-semibold">{pluralize(items.length, 'item')}</div>
@@ -16,10 +16,6 @@
$: fakeComponentInput && (value = fakeComponentInput.value)
</script>
<button
on:click={() => console.log('subFieldType =', subFieldType, 'componentInput =', componentInput)}
>console.log in SubtypeEditor</button
>
<StaticInputEditor
{id}
fieldType={subFieldType}