Code Monkey home page Code Monkey logo

Comments (9)

justin-schroeder avatar justin-schroeder commented on May 19, 2024 1

I think this is working as intended (it would apply to non-grouped fields too) actually, but you're right that it could be confusing. The issue is that at a library level we don't want to assume the model’s value should be equal to an available option since that takes power away from implementers. But I could be convinced this behavior should be changed for select or box inputs since the library will always control those internals.

A pretty easy way to solve this edge case in user-land right now is to just perform a check on the options to make sure the current value is selected. Here's a forked codepen:

https://codepen.io/justin-schroeder/pen/BajBWWm

from vue-formulate.

justin-schroeder avatar justin-schroeder commented on May 19, 2024

Hey πŸ‘‹ Are your questions specifically in the context of form generation?

from vue-formulate.

justin-schroeder avatar justin-schroeder commented on May 19, 2024

Hmm actually I think I’m following. This is a good question....thanks for the detailed example. Let me think on this....

from vue-formulate.

Mosnar avatar Mosnar commented on May 19, 2024

Hey, thanks for the great work on this project and the quick response! I actually just figured out a way to do it. But to answer your question, no it's not a generated form. I'm working on an order form where a user can add some products. They start with a product category, then they're shown a product model in that category.

I realized you can actually get the information from the context and do what I need to do, but it's a little verbose/janky. Here's what I ended up with:

<FormulateInput
        type="group"
        name="products"
        :repeatable="true"
        label="Products"
        add-label="+ Add Product"
        validation="required"
        :value="[{}]"
        #default="{ name, index }"
>
    <FormulateInput
            name="productCategory"
            :options="productCategories"
            type="select"
            label="Product Category"
            validation="required"
    />
    <FormulateInput
            v-if="model[name][index] && model[name][index].productCategory"
            name="productStyle"
            :options="getProductStyles(model[name][index])"
            type="select"
            label="Product Style"
            validation="required"
    />
</FormulateInput>

I need to have a default value in my group field, even if it's just a single empty object, otherwise it throws an undefined index error.

from vue-formulate.

justin-schroeder avatar justin-schroeder commented on May 19, 2024

Hey @Mosnar. You're pretty much right on here. Because it's repeatable you have to use functions instead of computed props to calculate the values of the internals unless you abstract the contents of default group slot into it's own component. I just worked up a little example this conditional repeatable functionality as an example of how I would solve this:

https://codepen.io/justin-schroeder/pen/NWGQbKV

I think the only feature-addition we could potentially add to make this a tad easier is have something like a groupValues exposed on the context object of the default slot. That makes it a bit easier, because you don't have to access the data by the index, but it ultimately requires passing the data into a method to compute the correct values. Open to suggestions on that one.

I'll leave this open for now to help anyone else stumbling on it.

from vue-formulate.

Mosnar avatar Mosnar commented on May 19, 2024

Thanks for the example! There's one other caveat with this approach I ran into that can be a little weird that I wanted to document here for others. When you update the source for a dependent field (ie, Select "Coffee" and then "Drip") then change it to "Tea", you'll notice the model will still have the invalid "Drip" value. For me, this caused issues because certain fields were supposed to be hidden and I was only checking to see if a value was set on the dependent property, it would show the dropdown without any options. To get around this, I had to introduce an additional check into my v-if to ensure there were options available.

I expanded on your example to show the issue. Try selecting "Coffee" -> "Drip" then change it from "Coffee" to "Tea" and you'll notice the "Leave me room for cream" field still shows up due to the malformed model:
https://codepen.io/mosnar/pen/oNjKMMv

The hacky solution is of course to simply check up the chain of dependencies, but this can get pretty messy pretty quickly if you have a lot of fields and you still have an invalid model.

from vue-formulate.

Mosnar avatar Mosnar commented on May 19, 2024

I agree that this is working as intended (but perhaps not as expected.) A side-effect of that is some weird behavior where the input model and the form model can fall out of sync. In your CodePen, try this:

  1. Select Coffee
  2. Select Drip
  3. Change "Coffee" to "Tea"
  4. Change "Tea" back to "Coffee"
    See that "Drip" is already pre-selected but the model doesn't have a "variant" property.

I get the feeling that these types of issues are fundamentally related to the core problem in my initial post where additional conditions and logical complexity are needed to ensure the form is in a valid state. I don't have any suggestions at this point, but I think there's a good solution out there that would solve all/most of these quirks at once.

P.S. I see you're from Charlottesville! Greetings from Blacksburg!

from vue-formulate.

justin-schroeder avatar justin-schroeder commented on May 19, 2024

:) Howdy neighbor πŸ‘

Yeah, in that codepen I made the variety option go away intentionally by setting the value to undefined you could choose to set it to an actual value, or the first option, if that better suits the needs of the author.

And I totally agree that the default behavior of leaving behind an un-available option is definitely not an expected behavior. What’s your opinion on what the expected behavior is? Sometimes that's tricky to figure out.

from vue-formulate.

davidebigpicture avatar davidebigpicture commented on May 19, 2024

And, I actually will use dependent fields in a generated fashion, where a DB is storing hundreds of different forms for different clients.

from vue-formulate.

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.