Skip to content

Commit

Permalink
fix(au-slot): correctly prepare resources for slotted view (#1802)
Browse files Browse the repository at this point in the history
Co-authored-by: Архипов Дмитрий <da@netcitylife.ru>
[skip ci]
  • Loading branch information
bigopon committed Jul 18, 2023
1 parent 15acfee commit bf1ca4c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
38 changes: 37 additions & 1 deletion packages/__tests__/3-runtime-html/au-slot.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { delegateSyntax } from '@aurelia/compat-v1';
import { IContainer, inject } from '@aurelia/kernel';
import { BindingMode, Aurelia, AuSlotsInfo, bindable, customElement, CustomElement, IAuSlotsInfo, IPlatform } from '@aurelia/runtime-html';
import { BindingMode, Aurelia, AuSlotsInfo, bindable, customElement, CustomElement, IAuSlotsInfo, IPlatform, ValueConverter } from '@aurelia/runtime-html';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { assert, createFixture, hJsx, TestContext } from '@aurelia/testing';
import { createSpecFunction, TestExecutionContext, TestFunction } from '../util.js';
Expand Down Expand Up @@ -2140,4 +2140,40 @@ describe('3-runtime-html/au-slot.spec.tsx', function () {
assert.instanceOf(parent, Parent);
assert.strictEqual(parent.id, 1);
});

it('provides right resources for slotted view', function () {
const { assertText } = createFixture(
'<el></el>',
{ },
[
CustomElement.define({
name: 'el',
template: '<el-with-slot>${"hey" | upper}</el-with-slot>',
dependencies: [
CustomElement.define({ name: 'el-with-slot', template: '<au-slot>' }, class ElWithSlot {}),
ValueConverter.define('upper', class { toView = v => v.toUpperCase(); }),
]
}, class El {}),
]
);
assertText('HEY');
});

it('provides right resources for passed through <au-slot>', function () {
const { assertText } = createFixture(
'<el></el>',
{ },
[
CustomElement.define({
name: 'el',
template: '<el-with-slot><au-slot>${"hey" | upper}</au-slot></el-with-slot>',
dependencies: [
CustomElement.define({ name: 'el-with-slot', template: '<au-slot>' }, class ElWithSlot {}),
ValueConverter.define('upper', class { toView = v => v.toUpperCase(); }),
]
}, class El {}),
]
);
assertText('HEY');
});
});
20 changes: 10 additions & 10 deletions packages/kernel/src/di.container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { isNativeFunction } from './functions';
import { type Class, type Constructable, type IDisposable } from './interfaces';
import { emptyArray } from './platform';
import { type IResourceKind, type ResourceDefinition, type ResourceType, getAllResources, hasResources } from './resource';
import { createObject, getOwnMetadata, isFunction, isString } from './utilities';
import { getOwnMetadata, isFunction, isString } from './utilities';
import {
IContainer,
type Key,
Expand Down Expand Up @@ -83,21 +83,21 @@ export class Container implements IContainer {
this._resolvers = new Map();
this._factories = new Map<Constructable, Factory>();

this.res = createObject();
this.res = {};
} else {
this.root = parent.root;

this._resolvers = new Map();
this._factories = parent._factories;
this.res = {};

if (config.inheritParentResources) {
this.res = Object.assign(
createObject(),
parent.res,
this.root.res
);
} else {
this.res = createObject();
// todo: when the simplify resource system work is commenced
// this resource inheritance can just be a Object.create() call
// with parent resources as the prototype of the child resources
for (const key in parent.res) {
this.registerResolver(key, parent.res[key]!);
}
}
}

Expand Down Expand Up @@ -277,7 +277,7 @@ export class Container implements IContainer {
}

public has<K extends Key>(key: K, searchAncestors: boolean = false): boolean {
return this._resolvers.has(key)
return this._resolvers.has(key) || isResourceKey(key) && key in this.res
? true
: searchAncestors && this.parent != null
? this.parent.has(key, true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class AuSlot implements ICustomElementViewModel, IAuSlot {
factory = rendering.getViewFactory(slotInfo.fallback, contextController.container);
this._hasProjection = false;
} else {
container = hdrContext.parent!.controller.container.createChild();
container = hdrContext.parent!.controller.container.createChild({ inheritParentResources: true });
registerResolver(
container,
contextController.definition.Type,
Expand Down

0 comments on commit bf1ca4c

Please sign in to comment.