Add x-else and x-else-if Directives for Conditional Rendering#4353
Add x-else and x-else-if Directives for Conditional Rendering#4353mvaliolahi wants to merge 1 commit intoalpinejs:mainfrom
Conversation
|
There's some cases not covered in the code and tests Made a PR to add these tests. Specifically:
This is a great feature to have, so kudos for stepping up to do it! I don't think this isn't a great way to go about it though. Maybe have the initial |
|
@ekwoka I think your concerns are valid I will change the code to support this case. |
|
Any updates for this pr? This would be very useful @ekwoka, thanks in advance |
|
I'm not the maintainer, so I can't approve or reject PRs. |
PR Review: #4353 — Add x-else and x-else-if Directives for Conditional RenderingType: Feature What's happening (plain English)This PR adds two new directives — <template x-if="value === 1"><h1>One</h1></template>
<template x-else-if="value === 2"><h2>Two</h2></template>
<template x-else><h3>Other</h3></template>Currently each directive is its own independent Alpine directive with its own Architectural problems1. In if (prevConditionsSatisfied) {
hide()
// _x_elseIfSatisfied is never set here!
} else if (value) {
show()
el._x_elseIfSatisfied = true // only set in this branch
} else {
hide()
el._x_elseIfSatisfied = false
}This means in a chain like The existing tests don't catch this because they don't test a chain of 3+ 2.
<template x-if="a">...</template>
<template x-else>...</template>
<template x-if="b">...</template>
<template x-else>...</template> <!-- BUG: also evaluates condition "a" -->The second 3.
4. Independent effects rely on execution order. Each directive in the chain has its own Other approaches considered
Changes MadeNo code changes made — the issues are architectural and need a different approach. Test ResultsAll existing tests pass:
However, the tests don't cover the failure cases identified above (3+ element chains where first condition is true, multiple independent chains in the same parent). Code ReviewStyle issues (minor, would fix if merging):
The SecurityNo security concerns identified. The directives use the standard VerdictNeeds discussion. The feature itself is worthwhile — The right path forward is approach #1: have Reviewed by Claude |
Fix else-if propagation and x-else chain boundary Address PR alpinejs#4353 review: else-if propagation, else chain boundary, tests
581b04a to
857a957
Compare
|
Addressed the review: |
This pull request adds two new directives, x-else and x-else-if, to Alpine.js.
How It Works:
x-else-if: Looks for a preceding x-if or x-else-if and only shows its content if none of those conditions are true.
x-else: Displays its content only if all previous conditions have failed.
Basic Usage:
Inside a Loop:
Tests:
I've added tests to ensure these new directives work as expected, including cases with multiple conditions and inside x-for loops.
Why This Matters:
This enhancement provides more flexibility for conditional logic, allowing you to handle complex scenarios directly within your templates.