Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(runtime): prevent ref callbacks from being called too early (#5614)
Sort attribute names before calling `setAccessor` with their changed values in order to ensure that the `ref` attribute is handled by `setAccessor` after all other attributes have been handled. This prevents the execution order for `ref` callbacks from being dependency on the order in which attributes are declared in the JSX. STENCIL-737 fixes #4074
- Loading branch information
1 parent
363c07b
commit 81fa375
Showing
3 changed files
with
65 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { h } from '@stencil/core'; | ||
import { render } from '@wdio/browser-runner/stencil'; | ||
|
||
describe('ref-attr-order', () => { | ||
beforeEach(() => { | ||
render({ | ||
template: () => <ref-attr-order></ref-attr-order>, | ||
}); | ||
}); | ||
|
||
it('should call the `ref` callback after handling other attrs', async () => { | ||
const cmp = await $('ref-attr-order'); | ||
await cmp.waitForStable(); | ||
await expect(cmp).toHaveText('my tabIndex: 0'); | ||
}); | ||
}); |
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,28 @@ | ||
import { Component, h, State } from '@stencil/core'; | ||
|
||
@Component({ | ||
tag: 'ref-attr-order', | ||
shadow: true, | ||
}) | ||
export class RefAttrOrder { | ||
@State() index: number = -1; | ||
|
||
// order matters for the attributes in the test below! | ||
// | ||
// this is testing that even though the `ref` attribute is declared first in | ||
// the JSX for the `div` the `ref` callback will nonetheless be called after | ||
// the `tabIndex` attribute is applied to the element. | ||
// See https://github.com/ionic-team/stencil/issues/4074 | ||
render() { | ||
return ( | ||
<div | ||
ref={(el) => { | ||
this.index = el.tabIndex; | ||
}} | ||
tabIndex={0} | ||
> | ||
my tabIndex: {this.index} | ||
</div> | ||
); | ||
} | ||
} |