danielo515 / obsidian-modal-form

Define forms for filling data that you will be able to open from anywhere you can run JS
https://danielorodriguez.com/obsidian-modal-form/
MIT License
187 stars 15 forks source link

[Question] Is it possible to create `select` dynamically via API? #211

Closed theotheo closed 6 months ago

theotheo commented 7 months ago

Hello, again!

As I understand it, the plugin has added the ability to create forms from the API. Please tell me if it is possible to set values for select.

I mean something like this

await modalForm.openForm('form', {
    values: {
        options: [
            { value: "pizza", label: "πŸ• Pizza" },
            { value: "pasta", label: "🍝 Pasta" },
            { value: "burger", label: "πŸ” Burger" },
        ]
    }
})
danielo515 commented 7 months ago

Interestingly, I was thinking about this in the very same morning you opened the issue. However, in the same morning I thought: can't this be done with the dataview source? But I guess you have some different use-case in mind? You can provide a full definition instead of a form name, but that is a overkill if you just want to override one single field of an existing form.

Please let me know what you want to achieve and I will try to add an easy way to do that. Something like this maybe?

await modalForm.openForm('form', {
    overrides: {
       fieldA: {
          options: [
            { value: "pizza", label: "πŸ• Pizza" },
            { value: "pasta", label: "🍝 Pasta" },
            { value: "burger", label: "πŸ” Burger" },
        ]
    }
  }
})
theotheo commented 7 months ago

In my workflow, when completing a task, I enter some data. I'm filling out several select. Moreover, one of the options is a list of blocks on the page, which I receive dynamically. This list varies accordingly from page to page. If this can be done using dataview, then you still need to pass the page as a parameter.

I have now implemented via quickAddApi.suggestor. But QuickAdd doesn't allow you to combine multiple input elements into one form and is generally not that pretty. Having a successful experience using your plugin, I wanted to try making one form.

You can provide a full definition instead of a form name

I think this option will suit me too! But how to do it?

danielo515 commented 7 months ago

In my workflow, when completing a task, I enter some data. I'm filling out several select. Moreover, one of the options is a list of blocks on the page, which I receive dynamically. This list varies accordingly from page to page. If this can be done using dataview, then you still need to pass the page as a parameter.

I don't know where that data is coming from yet, but I will help you anyway πŸ˜„

I have now implemented via quickAddApi.suggestor. But QuickAdd doesn't allow you to combine multiple input elements into one form and is generally not that pretty. Having a successful experience using your plugin, I wanted to try making one form.

Yeah, that was exactly my motivation to create this plugin, so you are in the right place

You can provide a full definition instead of a form name

I think this option will suit me too! But how to do it?

The open form method takes either a form name, or a form definition. If you build a form definition on the fly and then pass it as first argument, it will show you that form. The easiest way will be to create a simple form, with a single input, copy it as json to your clipboard and then use that as a template.

await modalForm.openForm({
  "title": "Example form",
  "name": "example",
  "fields": [
    {
            name: "favorite_meal",
            label: "Favorite meal",
            input: {
                type: "select",
                source: "fixed",
                options: [
                    { value: "pizza", label: "πŸ• Pizza" },
                    { value: "pasta", label: "🍝 Pasta" },
                    { value: "burger", label: "πŸ” Burger" },
                ],
            },
      isRequired: false
    }
  ],
  "version": "1"
})
danielo515 commented 6 months ago

@theotheo can this be considered done?

theotheo commented 6 months ago

Yes, this issue can be closed. Thank you very much for your advice. Your code helped me to handle my daily tasks more conveniently.

I would also like to mention that eventually, I went further and switched to using dataview-field. This is because the default select is not configurable.

"fields": [
    {
        name: "block",
        description: "blocks",
        input: {
            type: "dataview",
            query: JSON.stringify(contents)
        }
    },
    ...
danielo515 commented 6 months ago

Glad it is working for you. The select not being able to have a default value is a known bug, I want to fix it

El dom, 4 feb 2024, 16:04, theotheo @.***> escribiΓ³:

Closed #211 https://github.com/danielo515/obsidian-modal-form/issues/211 as completed.

β€” Reply to this email directly, view it on GitHub https://github.com/danielo515/obsidian-modal-form/issues/211#event-11695722683, or unsubscribe https://github.com/notifications/unsubscribe-auth/AARKJWLFYTNF5K5BAIVYTKLYR6PQNAVCNFSM6AAAAABB554TLSVHI2DSMVQWIX3LMV45UABCJFZXG5LFIV3GK3TUJZXXI2LGNFRWC5DJN5XDWMJRGY4TKNZSGI3DQMY . You are receiving this because you were assigned.Message ID: @.*** com>