Skip to content

Commit

Permalink
[sitecore-jss-angular] Implement Link-List SXA component (#1898)
Browse files Browse the repository at this point in the history
* [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
  • Loading branch information
art-alexeyenko authored Aug 22, 2024
1 parent d08b137 commit 284ab8f
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Our versioning strategy is as follows:
* Richtext component ([#1864](https://github.com/Sitecore/jss/pull/1864))
* Container component ([#1872](https://github.com/Sitecore/jss/pull/1872))
* 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))
* Link-List ([#1898](https://github.com/Sitecore/jss/pull/1898))
* Column-Splitter ([#1889](https://github.com/Sitecore/jss/pull/1889))

### 🛠 Breaking Change
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<div class="component link-list {{ styles }}" [attr.id]="id">
<div class="component-content">
<ng-container *ngIf="title; else defaultTitle">
<h3 *scText="title"></h3>
</ng-container>
<ul>
<li *ngFor="let fieldLink of fieldLinks;index as i" [ngClass]="getFieldLinkClass(i)" >
<div class="field-link">
<a *scLink="fieldLink"></a>
</div>
</li>
</ul>
</div>
<ng-template #defaultTitle>
<span class="is-empty-hint">Link list</span>
</ng-template>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Component, OnInit } from '@angular/core';
import { SxaComponent } from '../sxa.component';
import { Field, LinkField, SxaLinkListFields } from '@sitecore-jss/sitecore-jss-angular';

@Component({
selector: 'app-link-list',
templateUrl: './link-list.component.html',
})
export class LinkListComponent extends SxaComponent<SxaLinkListFields> implements OnInit {
title?: Field<string>;
fieldLinks: LinkField[] = [];

getFieldLinkClass(index: number): string {
let className = `item${index}`;
className += (index + 1) % 2 == 0 ? ' even' : ' odd';
if (index === 0) {
className += ' first';
}
if (index + 1 === this.fieldLinks.length) {
className += ' last';
}
return className;
}

ngOnInit() {
super.ngOnInit();
const datasource = this.rendering.fields?.data?.datasource;
if (datasource) {
this.title = datasource.field?.title as Field<string>;
datasource.children.results.forEach(item => {
if (item.field?.link)
this.fieldLinks.push(item.field.link as LinkField);
});
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { OnInit, Input, Directive } from '@angular/core';
import { ComponentRendering } from '@sitecore-jss/sitecore-jss-angular';
import { ComponentFields, ComponentRendering } from '@sitecore-jss/sitecore-jss-angular';

@Directive()
export abstract class SxaComponent implements OnInit {
@Input() rendering: ComponentRendering;
export abstract class SxaComponent<FieldType = ComponentFields> implements OnInit {
@Input() rendering: ComponentRendering<FieldType>;

id?: string;
styles?: string;
Expand Down
19 changes: 19 additions & 0 deletions packages/sitecore-jss-angular/src/components/rendering-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,25 @@ export interface LinkField extends LinkFieldValue, RenderingField {
editableLastPart?: string;
}

interface LayoutServiceLinkField {
field: {
link: LinkField;
};
}

export interface SxaLinkListFields {
data: {
datasource: {
children: {
results: LayoutServiceLinkField[];
};
field: {
title: TextField;
};
};
};
}

export interface RichTextField extends RenderingField<string> {}

export interface TextField extends RenderingField<string> {}
1 change: 1 addition & 0 deletions packages/sitecore-jss-angular/src/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export {
RenderingField,
RichTextField,
TextField,
SxaLinkListFields,
} from './components/rendering-field';
export { RichTextDirective } from './components/rich-text.directive';
export { TextDirective } from './components/text.directive';
Expand Down
6 changes: 3 additions & 3 deletions packages/sitecore-jss/src/layout/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ export interface ComponentParams {
/**
* Definition of a component instance within a placeholder on a route
*/
export interface ComponentRendering {
export interface ComponentRendering<T = ComponentFields> {
componentName: string;
dataSource?: string;
uid?: string;
placeholders?: PlaceholdersData;
fields?: ComponentFields;
fields?: T;
params?: ComponentParams;
}

Expand Down Expand Up @@ -137,7 +137,7 @@ export interface FieldMetadata {
}

/**
* Content data returned from Content Service
* Content data returned from Layout Service
*/
export interface Item {
name: string;
Expand Down

0 comments on commit 284ab8f

Please sign in to comment.