Skip to content

Commit 284ab8f

Browse files
[sitecore-jss-angular] Implement Link-List SXA component (#1898)
* [sitecore-jss] Introduce extra types from Edge schema into models (cherry picked from commit f09a6cd) * mid-way implementation * rework implementation * changelog * make SXA base component generic * cleanup console.log
1 parent d08b137 commit 284ab8f

File tree

7 files changed

+80
-6
lines changed

7 files changed

+80
-6
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Our versioning strategy is as follows:
3333
* Richtext component ([#1864](https://github.com/Sitecore/jss/pull/1864))
3434
* Container component ([#1872](https://github.com/Sitecore/jss/pull/1872))
3535
* Angular SXA layout ([#1873](https://github.com/Sitecore/jss/pull/1873))([#1880](https://github.com/Sitecore/jss/pull/1880))([#1890](https://github.com/Sitecore/jss/pull/1890))
36+
* Link-List ([#1898](https://github.com/Sitecore/jss/pull/1898))
3637
* Column-Splitter ([#1889](https://github.com/Sitecore/jss/pull/1889))
3738

3839
### 🛠 Breaking Change
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<div class="component link-list {{ styles }}" [attr.id]="id">
2+
<div class="component-content">
3+
<ng-container *ngIf="title; else defaultTitle">
4+
<h3 *scText="title"></h3>
5+
</ng-container>
6+
<ul>
7+
<li *ngFor="let fieldLink of fieldLinks;index as i" [ngClass]="getFieldLinkClass(i)" >
8+
<div class="field-link">
9+
<a *scLink="fieldLink"></a>
10+
</div>
11+
</li>
12+
</ul>
13+
</div>
14+
<ng-template #defaultTitle>
15+
<span class="is-empty-hint">Link list</span>
16+
</ng-template>
17+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { Component, OnInit } from '@angular/core';
2+
import { SxaComponent } from '../sxa.component';
3+
import { Field, LinkField, SxaLinkListFields } from '@sitecore-jss/sitecore-jss-angular';
4+
5+
@Component({
6+
selector: 'app-link-list',
7+
templateUrl: './link-list.component.html',
8+
})
9+
export class LinkListComponent extends SxaComponent<SxaLinkListFields> implements OnInit {
10+
title?: Field<string>;
11+
fieldLinks: LinkField[] = [];
12+
13+
getFieldLinkClass(index: number): string {
14+
let className = `item${index}`;
15+
className += (index + 1) % 2 == 0 ? ' even' : ' odd';
16+
if (index === 0) {
17+
className += ' first';
18+
}
19+
if (index + 1 === this.fieldLinks.length) {
20+
className += ' last';
21+
}
22+
return className;
23+
}
24+
25+
ngOnInit() {
26+
super.ngOnInit();
27+
const datasource = this.rendering.fields?.data?.datasource;
28+
if (datasource) {
29+
this.title = datasource.field?.title as Field<string>;
30+
datasource.children.results.forEach(item => {
31+
if (item.field?.link)
32+
this.fieldLinks.push(item.field.link as LinkField);
33+
});
34+
}
35+
}
36+
}

packages/create-sitecore-jss/src/templates/angular-xmcloud/src/app/components/sxa.component.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { OnInit, Input, Directive } from '@angular/core';
2-
import { ComponentRendering } from '@sitecore-jss/sitecore-jss-angular';
2+
import { ComponentFields, ComponentRendering } from '@sitecore-jss/sitecore-jss-angular';
33

44
@Directive()
5-
export abstract class SxaComponent implements OnInit {
6-
@Input() rendering: ComponentRendering;
5+
export abstract class SxaComponent<FieldType = ComponentFields> implements OnInit {
6+
@Input() rendering: ComponentRendering<FieldType>;
77

88
id?: string;
99
styles?: string;

packages/sitecore-jss-angular/src/components/rendering-field.ts

+19
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,25 @@ export interface LinkField extends LinkFieldValue, RenderingField {
4444
editableLastPart?: string;
4545
}
4646

47+
interface LayoutServiceLinkField {
48+
field: {
49+
link: LinkField;
50+
};
51+
}
52+
53+
export interface SxaLinkListFields {
54+
data: {
55+
datasource: {
56+
children: {
57+
results: LayoutServiceLinkField[];
58+
};
59+
field: {
60+
title: TextField;
61+
};
62+
};
63+
};
64+
}
65+
4766
export interface RichTextField extends RenderingField<string> {}
4867

4968
export interface TextField extends RenderingField<string> {}

packages/sitecore-jss-angular/src/public_api.ts

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export {
3030
RenderingField,
3131
RichTextField,
3232
TextField,
33+
SxaLinkListFields,
3334
} from './components/rendering-field';
3435
export { RichTextDirective } from './components/rich-text.directive';
3536
export { TextDirective } from './components/text.directive';

packages/sitecore-jss/src/layout/models.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,12 @@ export interface ComponentParams {
9393
/**
9494
* Definition of a component instance within a placeholder on a route
9595
*/
96-
export interface ComponentRendering {
96+
export interface ComponentRendering<T = ComponentFields> {
9797
componentName: string;
9898
dataSource?: string;
9999
uid?: string;
100100
placeholders?: PlaceholdersData;
101-
fields?: ComponentFields;
101+
fields?: T;
102102
params?: ComponentParams;
103103
}
104104

@@ -137,7 +137,7 @@ export interface FieldMetadata {
137137
}
138138

139139
/**
140-
* Content data returned from Content Service
140+
* Content data returned from Layout Service
141141
*/
142142
export interface Item {
143143
name: string;

0 commit comments

Comments
 (0)