Skip to content

Commit

Permalink
fix: spread not being reactive (#3709)
Browse files Browse the repository at this point in the history
  • Loading branch information
abdulsattar authored Sep 15, 2023
1 parent 6528018 commit 2f85f36
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 27 deletions.
18 changes: 4 additions & 14 deletions packages/@lwc/engine-core/src/framework/modules/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
import { assign, htmlPropertyToAttribute, isNull, isUndefined } from '@lwc/shared';
import { htmlPropertyToAttribute, isNull, isUndefined } from '@lwc/shared';
import { logWarn } from '../../shared/logger';
import { RendererAPI } from '../renderer';
import { EmptyObject } from '../utils';
Expand All @@ -21,33 +21,23 @@ export function patchProps(
vnode: VBaseElement,
renderer: RendererAPI
) {
let { props } = vnode.data;
const { spread } = vnode.data;
const { props } = vnode.data;

if (isUndefined(props) && isUndefined(spread)) {
if (isUndefined(props)) {
return;
}

let oldProps;
if (!isNull(oldVnode)) {
oldProps = oldVnode.data.props;
const oldSpread = oldVnode.data.spread;
// Props may be the same due to the static content optimization, so we can skip diffing
if (oldProps === props && oldSpread === spread) {
if (oldProps === props) {
return;
}

if (isUndefined(oldProps)) {
oldProps = EmptyObject;
}

if (!isUndefined(oldSpread)) {
oldProps = assign({}, oldProps, oldSpread);
}
}

if (!isUndefined(spread)) {
props = assign({}, props, spread);
}

const isFirstPatch = isNull(oldVnode);
Expand Down
1 change: 0 additions & 1 deletion packages/@lwc/engine-core/src/framework/vnodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ export interface VNodeData {
readonly on?: Readonly<Record<string, (event: Event) => any>>;
readonly svg?: boolean;
readonly renderer?: RendererAPI;
readonly spread?: Readonly<Record<string, any>>;
}

export interface VElementData extends VNodeData {
Expand Down
30 changes: 29 additions & 1 deletion packages/@lwc/integration-karma/test/spread/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
import { createElement } from 'lwc';
import Test from 'x/test';
import { getHooks, setHooks } from 'test-utils';

function setSanitizeHtmlContentHookForTest(impl) {
const { sanitizeHtmlContent } = getHooks();

setHooks({
sanitizeHtmlContent: impl,
});

return sanitizeHtmlContent;
}
describe('lwc:spread', () => {
let elm, simpleChild, overriddenChild;
let elm, simpleChild, overriddenChild, trackedChild, innerHTMLChild, originalHook;
beforeEach(() => {
originalHook = setSanitizeHtmlContentHookForTest((x) => x);
elm = createElement('x-test', { is: Test });
document.body.appendChild(elm);
simpleChild = elm.shadowRoot.querySelector('.x-child-simple');
overriddenChild = elm.shadowRoot.querySelector('.x-child-overridden');
trackedChild = elm.shadowRoot.querySelector('.x-child-tracked');
innerHTMLChild = elm.shadowRoot.querySelector('.div-innerhtml');
spyOn(console, 'log');
});
afterEach(() => {
setSanitizeHtmlContentHookForTest(originalHook);
});
it('should render basic test', () => {
expect(simpleChild.shadowRoot.querySelector('span').textContent).toEqual('Name: LWC');
});
it('should override innerHTML from inner-html directive', () => {
expect(innerHTMLChild.innerHTML).toEqual('innerHTML from spread');
});
it('should assign onclick', () => {
simpleChild.click();
// eslint-disable-next-line no-console
Expand Down Expand Up @@ -76,4 +95,13 @@ describe('lwc:spread', () => {
.shadowRoot.querySelector('span').textContent
).toEqual('Name: Dynamic');
});

it('should rerender when tracked props are assigned', async () => {
expect(trackedChild.shadowRoot.querySelector('span').textContent).toEqual('Name: Tracked');
elm.modify(function () {
this.trackedProps.name = 'Altered';
});
await Promise.resolve();
expect(trackedChild.shadowRoot.querySelector('span').textContent).toEqual('Name: Altered');
});
});
2 changes: 2 additions & 0 deletions packages/@lwc/integration-karma/test/spread/x/test/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
<span lwc:spread={spanProps}>Hello</span>
<x-cmp lwc:dynamic={dynamicCtor} lwc:spread={dynamicProps}></x-cmp>
<lwc:component data-id="lwc-component" lwc:is={dynamicCtor} lwc:spread={dynamicProps}></lwc:component>
<x-child class="x-child-tracked" lwc:spread={trackedProps}></x-child>
<div class="div-innerhtml" lwc:dom="manual" lwc:spread={innerHTMLProps} lwc:inner-html={innerHTML}></div>
</template>
5 changes: 4 additions & 1 deletion packages/@lwc/integration-karma/test/spread/x/test/test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LightningElement, api } from 'lwc';
import { api, LightningElement, track } from 'lwc';
import Child from 'x/child';

export default class Test extends LightningElement {
Expand All @@ -7,6 +7,9 @@ export default class Test extends LightningElement {
spanProps = { className: 'spanclass' };
dynamicCtor = Child;
dynamicProps = { name: 'Dynamic' };
@track trackedProps = { name: 'Tracked' };
innerHTMLProps = { innerHTML: 'innerHTML from spread' };
innerHTML = 'innerHTML from directive';

spreadClick() {
// eslint-disable-next-line no-console
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ function tmpl($api, $cmp, $slotset, $ctx) {
const { dc: api_dynamic_component } = $api;
return [
api_dynamic_component($cmp.ctor, {
spread: $cmp.hello,
props: {
...$cmp.hello,
},
key: 0,
}),
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ function tmpl($api, $cmp, $slotset, $ctx) {
$api;
return [
api_deprecated_dynamic_component("x-foo", $cmp.dynamicCtor, {
spread: $cmp.dynamicProps,
props: {
...$cmp.dynamicProps,
},
key: 0,
}),
api_dynamic_component($cmp.dynamicCtor, {
spread: $cmp.dynamicProps,
props: {
...$cmp.dynamicProps,
},
key: 1,
}),
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ function tmpl($api, $cmp, $slotset, $ctx) {
const { h: api_element } = $api;
return [
api_element("a", {
spread: $cmp.hello,
props: {
...$cmp.hello,
},
key: 0,
}),
];
Expand Down
12 changes: 6 additions & 6 deletions packages/@lwc/template-compiler/src/codegen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,12 @@ function transform(codeGen: CodeGen): t.Expression {
data.push(codeGen.genRef(ref));
}

// Properties: lwc:spread directive
if (spread) {
// spread goes last, so it can be used to override any other properties
propsObj.properties.push(t.spreadElement(codeGen.bindExpression(spread.value)));
instrumentation?.incrementCounter(CompilerMetrics.LWCSpreadDirective);
}
if (propsObj.properties.length) {
data.push(t.property(t.identifier('props'), propsObj));
}
Expand All @@ -609,12 +615,6 @@ function transform(codeGen: CodeGen): t.Expression {
data.push(t.property(t.identifier('context'), contextObj));
}

// Spread
if (spread) {
data.push(t.property(t.identifier('spread'), codeGen.bindExpression(spread.value)));
instrumentation?.incrementCounter(CompilerMetrics.LWCSpreadDirective);
}

// Key property on VNode
if (forKey) {
// If element has user-supplied `key` or is in iterator, call `api.k`
Expand Down
7 changes: 7 additions & 0 deletions packages/@lwc/template-compiler/src/shared/estree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,13 @@ export function property(
};
}

export function spreadElement(argument: t.Expression): t.SpreadElement {
return {
type: 'SpreadElement',
argument,
};
}

export function assignmentProperty(
key: t.AssignmentProperty['key'],
value: t.AssignmentProperty['value'],
Expand Down

0 comments on commit 2f85f36

Please sign in to comment.