feat: add a single option to slotted and children#7652
Open
AKnassa wants to merge 1 commit into
Open
Conversation
Both directives always assigned an array, so a slot holding one element still forced consumers to index into it and guard for emptiness at every use site. Passing `single: true` now assigns the first matching node directly, or null when nothing matches, mirroring how `ref` behaves. The declarative syntax and the template generator round-trip the option. Fixes microsoft#5450
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this does
Lets
slottedandchildrenhand you a single element instead of always handing you an array.Why
refgives you the node directly.slottedandchildrenalways give you an array — even when you have filtered down to the one element you actually want. So every use site ends up writingthis.thing[0]and guarding for emptiness by hand.In the original report, someone building a wrapper with two named slots ended up reaching into
this.$fastController.view.behaviors[0].target.assignedNodes[0]to get at a single node. That was fairly described at the time as "pretty brittle code."What changed
slottedandchildrenacceptsingle: true.nullwhen nothing matches — the same shaperefgives you.childrengets it automatically.How to see it
Run the
fast-elementtest suite.templating/slotted.pw.spec.tsandtemplating/children.pw.spec.tscover the single-node case: the first match is assigned directly,nullwhen the slot is empty or the filter matches nothing, and it updates onslotchange.One behavior worth calling out, because it is genuinely new: observables compare with
!==, so withsinglethe property does not re-notify when the first match happens to be unchanged across a slot change. That is asserted in the tests.Two things for reviewers
Naming.
singlewas chosen overfirstormultiple: falsebecause it reads as a description of the result, not of the traversal. Happy to change it.One extra change, deliberately.
packages/fast-test-harnessreconstructs these directives when generating declarative templates, so it had to learn the new option or the fixtures would silently lose it. While there, it was also fabricating afilter elements()for any filter it could not represent — quietly narrowing what you observe to elements only. It now emits no filter and warns instead, so an unrepresentable filter degrades loudly rather than silently changing behavior.Fixes #5450