GithubHelp home page GithubHelp logo

Comments (4)

lucafaggianelli avatar lucafaggianelli commented on June 15, 2024 1

mmm my bad, indeed I finalized my code and it's working with Object.entries:

import type { BindingParams } from 'tweakpane'

export interface ThemeOption {
  default: string | number
  options?: BindingParams
}

export type ThemeOptions = Record<string, ThemeOption>

const initDesigner = (
  container: HTMLElement,
  theme: ThemeOptions,
  onChange: (key: string, value: string | number) => void,
) => {
  const pane = new Pane({
    title: 'Resume Designer',
    expanded: true,
    container,
  })

  const initialData: Record<string, string | number> = {}

  Object.entries(theme).forEach(([key, option]) => {
    initialData[key] = option.default
    pane
      .addBinding(initialData, key, option.options)
      .on('change', (event) => {
        onChange(key, event.value)
      })
  })
 
  pane.addButton({ title: 'Reset' }).on('click', () => {
    // pane.importState({})
  })

  return {...initialData}
}

Thanks for your help!

from tweakpane.

davelsan avatar davelsan commented on June 15, 2024

That error makes sense to me. You are trying to bind theme keys to the data object. But even if you fix that, there is stil the limitation of Object.entries, which does not produce a fully typed object.

Assuming theme is of type ThemeOptions, you could do the following.

const TypedObject = {
  entries: <T extends {}>(obj: T) => {
    return Object.entries(obj) as [keyof T, T[keyof T]][];
  }
}

TypedObject.entries(theme).forEach(([key, option]) => {
  pane.addBinding(theme, key, option.options); // changed `data` for `theme`
})

Edit: Actually, Object.entries will work just fine in this case too, if you use theme instead of data. Maybe I'm missing something about what you are trying to achieve?

from tweakpane.

lucafaggianelli avatar lucafaggianelli commented on June 15, 2024

Closing as this is not a bug

from tweakpane.

davelsan avatar davelsan commented on June 15, 2024

Happy to hear you got it working.

from tweakpane.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.