-
Notifications
You must be signed in to change notification settings - Fork 795
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(renderer): fix conditional rendering issue (#5365)
* fix(renderer): fix conditional rendering issue fix conditional rendering issue by applying to find actual DOM element(s) when about the relocate element is slot ref fixes one of the issues raised in #5335 * fix(renderer): add check to prevent regression for conditional rendering bugfix * test(slot): add missing e2e tests for conditional slot rendering
- Loading branch information
Showing
5 changed files
with
99 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Component, h, Host, State } from '@stencil/core'; | ||
|
||
@Component({ | ||
tag: 'slot-conditional-rendering', | ||
shadow: false, | ||
scoped: true, | ||
}) | ||
export class SlotConditionalRendering { | ||
@State() headerVisible = true; | ||
@State() contentVisible = true; | ||
|
||
render() { | ||
return ( | ||
<Host> | ||
{this.headerVisible ? <slot name="header" /> : null} | ||
{this.contentVisible ? <slot /> : null} | ||
|
||
<button id="header-visibility-toggle" onClick={() => (this.headerVisible = !this.headerVisible)}> | ||
Toggle header visibility (to {this.headerVisible ? 'hidden' : 'visible'}) | ||
</button> | ||
<button id="content-visibility-toggle" onClick={() => (this.contentVisible = !this.contentVisible)}> | ||
Toggle content visibility (to {this.contentVisible ? 'hidden' : 'visible'}) | ||
</button> | ||
</Host> | ||
); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<!DOCTYPE html> | ||
<meta charset="utf8"> | ||
<script src="./build/testapp.esm.js" type="module"></script> | ||
<script src="./build/testapp.js" nomodule></script> | ||
|
||
<slot-conditional-rendering> | ||
<span slot="header" id="slotted-header-element-id">Hello</span> | ||
<span id="slotted-content-element-id">World!</span> | ||
</slot-conditional-rendering> |
39 changes: 39 additions & 0 deletions
39
test/karma/test-app/slot-conditional-rendering/karma.spec.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { setupDomTests, waitForChanges } from '../util'; | ||
|
||
describe('slot-conditional-rendering', function () { | ||
const { setupDom, tearDownDom } = setupDomTests(document); | ||
let app: HTMLElement; | ||
const getHeaderVisibilityToggle = () => app.querySelector<HTMLButtonElement>('#header-visibility-toggle'); | ||
const getContentVisibilityToggle = () => app.querySelector<HTMLButtonElement>('#content-visibility-toggle'); | ||
const getHeaderElementInLightDOM = () => app.querySelector('#slotted-header-element-id'); | ||
const getContentElementInLightDOM = () => app.querySelector('#slotted-content-element-id'); | ||
|
||
beforeEach(async () => { | ||
app = await setupDom('/slot-conditional-rendering/index.html'); | ||
}); | ||
afterEach(tearDownDom); | ||
|
||
it('slots are not hidden', async () => { | ||
await waitForChanges(); | ||
expect(getHeaderElementInLightDOM().getAttribute('hidden')).toBeNull(); | ||
expect(getContentElementInLightDOM().getAttribute('hidden')).toBeNull(); | ||
}); | ||
|
||
it('header slot becomes hidden after hit the toggle button', async () => { | ||
await waitForChanges(); | ||
expect(getHeaderElementInLightDOM().getAttribute('hidden')).toBeNull(); | ||
|
||
getHeaderVisibilityToggle()?.click(); | ||
await waitForChanges(); | ||
expect(getHeaderElementInLightDOM().getAttribute('hidden')).not.toBeNull(); | ||
}); | ||
|
||
it('content slot becomes hidden after hit the toggle button', async () => { | ||
await waitForChanges(); | ||
expect(getContentElementInLightDOM().getAttribute('hidden')).toBeNull(); | ||
|
||
getContentVisibilityToggle()?.click(); | ||
await waitForChanges(); | ||
expect(getContentElementInLightDOM().getAttribute('hidden')).not.toBeNull(); | ||
}); | ||
}); |