Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion __test__/test-website/src/app/en/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ import {
Component2A,
Component3,
Component3C,
Component6,
Component7,
ct1,
ct2,
ct3,
ct6,
ct7,
dt1,
dt2,
dt3,
Expand All @@ -28,7 +32,7 @@ import {
} from '@optimizely/cms-sdk';
import { initReactComponentRegistry } from '@optimizely/cms-sdk/react/server';

initContentTypeRegistry([ct1, ct2, ct3]);
initContentTypeRegistry([ct1, ct2, ct3, ct6, ct7]);
initDisplayTemplateRegistry([dt1, dt2, dt3, dt4, dt5, dt6]);
initReactComponentRegistry({
resolver: {
Expand Down Expand Up @@ -57,6 +61,15 @@ initReactComponentRegistry({
},
_Column: Column,
'_Column:tagA': ColumnA,

// Content type "test_ct6" only has a component with tag
'test_c6:tagA': Component6,
test_c7: {
// default: Component7,
tags: {
tagA: Component7,
},
},
},
});

Expand Down
21 changes: 21 additions & 0 deletions __test__/test-website/src/components/with-display-templates.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ export const ct3 = contentType({
baseType: '_experience',
});

export const ct6 = contentType({
key: 'test_c6',
baseType: '_component',
compositionBehaviors: ['elementEnabled', 'sectionEnabled'],
});

export const ct7 = contentType({
key: 'test_c7',
baseType: '_component',
compositionBehaviors: ['elementEnabled', 'sectionEnabled'],
});

export const dt1 = displayTemplate({
key: 'test_dt1',
baseType: '_component',
Expand Down Expand Up @@ -90,6 +102,8 @@ export const dt6 = displayTemplate({
type Props1 = { opti: Infer<typeof ct1> };
type Props2 = { opti: Infer<typeof ct2> };
type Props3 = { opti: Infer<typeof ct3> };
type Props6 = { opti: Infer<typeof ct6> };
type Props7 = { opti: Infer<typeof ct7> };

type BlankSectionProps = {
opti: Infer<typeof BlankSectionContentType>;
Expand Down Expand Up @@ -124,6 +138,13 @@ export function Component3({ opti }: Props3) {
);
}

export function Component6({}: Props6) {
return <div>This is Component6</div>;
}

export function Component7({}: Props7) {
return <div>This is Component7</div>;
}
export function Component3C({ opti }: Props3) {
return (
<div>
Expand Down
20 changes: 10 additions & 10 deletions packages/optimizely-cms-sdk/src/render/componentRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
type ComponentWithVariants<C> = {
/** Default component */
default: C;
default?: C;

/**
* Tagged variants, where the keys are tag names and the values are the corresponding components.
Expand Down Expand Up @@ -47,13 +47,11 @@ type ComponentMap<C> = Record<string, ComponentEntry<C>>;

/** Returns true if `obj` is type {@linkcode ComponentWithVariants} */
function hasVariants(obj: unknown): obj is ComponentWithVariants<unknown> {
return (
typeof obj === 'object' && obj !== null && 'default' in obj && 'tags' in obj
);
return typeof obj === 'object' && obj !== null && 'tags' in obj;
}

/** Returns the default component in an {@linkcode ComponentEntry} */
function getDefaultComponent<C>(entry: ComponentEntry<C>): C {
function getDefaultComponent<C>(entry: ComponentEntry<C>): C | undefined {
if (hasVariants(entry)) {
return entry.default;
} else {
Expand Down Expand Up @@ -100,21 +98,23 @@ export class ComponentRegistry<T> {

const entry = this.resolver[contentType];

if (!entry) {
return undefined;
}

if (!options.tag) {
if (!entry) {
return undefined;
}
return getDefaultComponent(entry);
}

// Search for the component `${contentType}:${tag}`
const taggedEntry = this.resolver[`${contentType}:${options.tag}`];

if (taggedEntry) {
return getDefaultComponent(taggedEntry);
}

if (!entry) {
return undefined;
}

return (
// Search for the component with the tag in the component definition
getTagComponent(entry, options.tag) ??
Expand Down