DEV: Add owner arg to plugin connector's shouldRender callbacks (#29488)

This commit is contained in:
Jarek Radosz 2024-10-31 01:06:02 +01:00 committed by GitHub
parent ae8d919528
commit dc96b6e953
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 25 additions and 6 deletions

View File

@ -1,6 +1,7 @@
import { cached } from "@glimmer/tracking";
import ClassicComponent from "@ember/component";
import { get } from "@ember/object";
import { getOwner } from "@ember/owner";
import { service } from "@ember/service";
import GlimmerComponentWithDeprecatedParentView from "discourse/components/glimmer-component-with-deprecated-parent-view";
import {
@ -88,7 +89,8 @@ export default class PluginOutletComponent extends GlimmerComponentWithDeprecate
const connectors = renderedConnectorsFor(
this.args.name,
this.outletArgsWithDeprecations,
this.context
this.context,
getOwner(this)
);
if (connectors.length > 1 && hasBlock) {
const message = `Multiple connectors were registered for the ${this.args.name} outlet. Using the first.`;

View File

@ -224,10 +224,12 @@ export function connectorsFor(outletName) {
return _connectorCache[outletName] || [];
}
export function renderedConnectorsFor(outletName, args, context) {
export function renderedConnectorsFor(outletName, args, context, owner) {
return connectorsFor(outletName).filter((con) => {
const shouldRender = con.connectorClass?.shouldRender;
return !shouldRender || shouldRender(args, context);
return (
!con.connectorClass?.shouldRender ||
con.connectorClass?.shouldRender(args, context, owner)
);
});
}

View File

@ -60,8 +60,12 @@ module("Integration | Component | plugin-outlet", function (hooks) {
});
registerTemporaryModule(`${CLASS_PREFIX}/test-name/conditional-render`, {
shouldRender(args, context) {
return args.shouldDisplay || context.siteSettings.always_display;
shouldRender(args, context, owner) {
return (
args.shouldDisplay ||
context.siteSettings.always_display ||
owner.lookup("service:site-settings").alternativeAccess
);
},
});
@ -407,6 +411,17 @@ module("Integration | Component | plugin-outlet", function (hooks) {
assert.dom(".conditional-render").exists("renders conditional outlet");
});
test("shouldRender receives an owner argument", async function (assert) {
await render(hbs`<PluginOutlet @name="test-name" />`);
assert
.dom(".conditional-render")
.doesNotExist("doesn't render conditional outlet");
getOwner(this).lookup("service:site-settings").alternativeAccess = true;
await settled();
assert.dom(".conditional-render").exists("renders conditional outlet");
});
test("Other outlets are not re-rendered", async function (assert) {
this.set("shouldDisplay", false);
await render(hbs`