-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AutoObject should display arrays #20
Comments
Hi mingfang, thanks for using the library and for this suggestion. See #3 for a somewhat-related discussion on what to do with arrays, though I see that your suggestion is different. It's worth noting that vanilla Tweakpane ignores arrays, for example this results in an empty pane: import { Pane } from 'https://cdn.jsdelivr.net/npm/[email protected]/dist/tweakpane.min.js'
const params = {
array: ['a', 'b', 'c'],
}
const pane = new Pane({})
pane.addBinding(params, 'array') There's also some confusing precedent from the rest of Svelte Tweakpane UI, where certain controls can accept tuple values. So I'm not sure at the moment of the "least surprising" way to handle this case. |
The code that I have works locally so this is not urgent. |
Another option is for {#each Object.keys(object) as key (key)}
{#if object[key].constructor === Object}
<slot name="object" {object} {key}>
<Folder title={prettify(key)}>
<AutoObject bind:object={object[key]}/>
</Folder>
</slot>
{:else if typeof object[key] === 'string'}
<slot name="string" {object} {key}>
<Text bind:value={object[key]} label={prettify(key)}/>
</slot>
{:else if Array.isArray(object[key])}
<slot name="array" {object} {key}>
<Folder title={prettify(key)}>
{#each object[key].map((value, i) => ({[i]: value})) as value}
<AutoObject bind:object={value}/>
{/each}
</Folder>
</slot>
{:else}
<slot {object} {key}>
<Binding bind:object {key} label={prettify(key)}/>
</slot>
{/if}
{/each} Then the user can override, say <AutoObject object={object}>
<svelte:fragment slot="array" let:object let:key>
<Text value={object[key].toString()} label={key}/>
</svelte:fragment>
</AutoObjec> The only downside is you can't |
Thanks! The slot approach is very interesting — though losing the let binding is tough. I'll leave this issue open as I'd like to implement something when I have a moment. |
Given an object like with an array field, e.g.
AutoObject
prints this errorIdeally
AutoObject
can display the array like thisThis is the possible code change in
AutoObject
svelte-tweakpane-ui/src/lib/extra/AutoObject.svelte
Line 174 in 1b17dcb
The text was updated successfully, but these errors were encountered: