A conditional section in one line - #91
Conversation
|
Docs preview: https://advanced-forms-4zlmbh1dh-leancode.vercel.app Built from 1cf2ddf; the landing page is at |
|
IDK, as a user I think I would feel lost in this, when to use removeSubform, when to make it conditional... What about a separate method for that, leaving |
5d12d5c to
1a7cf08
Compare
The closure is evaluated on attach and after every value change in the form; its result switches the section's validation on and off. Replaces the addRelation + setValidationEnabled + manual seed pattern, and a resetAll() brings the section back in step by itself. Piotr Denert's idea. Example wizard, docs and skill updated. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Kamil's call: a section whose condition is false leaves the form, so the payload built from the form has none of its fields. The form still owns it, so its values survive and come back when the condition flips. Example wizard, docs and skill follow. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
bf8109b to
cf91d25
Compare
The last call by hand wins: removeSubform detaches and drops the condition, a plain addSubform attaches for good. In the Dart docs, the patterns page and the skill. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
|
@mateusz-pietras After reconsideration I agree that's API doubling, but I think it's in good faith. Adding subform with "enabled by" hook, then removing it with "removeSubform" will simply deactivate that hook. To make some order of this "chaos" I expanded Dart docs for this "enabledBy" parameter, explaining that it's convenient shortcut for most simple use cases Let me know what you think:) |
Piotruś's four comments on #91: the condition is a typedef of a record and the loop destructures the entry; the docs and the skill say the closure is any boolean expression over the fields' values instead of listing examples. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Problem
A section that applies only sometimes, like invoice details behind an "I need an invoice" switch, takes a relation and two calls in every form:
The seeding line is easy to forget, and the whole thing says nothing about what it is for. Piotr suggested that
addSubformshould just take the condition and do the attaching and detaching itself.Solution
One optional parameter on a method everyone already uses. The form evaluates the closure when the section is added and again after every value change anywhere in its tree. While it is true the section is attached. When it turns false the section is detached, exactly as
removeSubformwould do: its fields leavevalidate(),canSubmit,getFieldValues()andallFields, so a payload built from the form has no invoice in it. The form owns the section either way, so its values stay in its fields and are back when the switch flips, and it is disposed with the form. A reset of the checkbox takes the section with it on its own.removeSubformdrops the condition.Cost: one closure call and a bool comparison per conditional section on each value change, next to the pass over all fields the form already does for
wasModified.Limit, written into the docs: a condition on something outside the form is not re-evaluated for that change. Attaching by hand still covers it.
In this PR
enabledonaddSubform, re-evaluated from the existing value-changed handler; attach and detach share the code withaddSubformandremoveSubform.validate()while detached, parent mode reaches a late attach,resetAll()re-syncs,removeSubformdrops the condition, re-attach and replace cases, outside field not followed.resetAlland friends must keep reaching a hidden section.flutter analyzeclean in the package and the example, all 273 tests pass.🤖 Generated with Claude Code