Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(chat): add scrollBottom chat option #1001

Merged
merged 1 commit into from
Nov 20, 2018
Merged
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
35 changes: 28 additions & 7 deletions src/framework/theme/components/chat/chat.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import {
HostBinding,
ViewChild,
ElementRef,
AfterViewChecked,
ContentChildren,
QueryList, AfterViewInit,
} from '@angular/core';
import { convertToBoolProperty } from '../helpers';
import { NbChatMessageComponent } from './chat-message.component';

/**
Expand Down Expand Up @@ -143,7 +143,7 @@ import { NbChatMessageComponent } from './chat-message.component';
</div>
`,
})
export class NbChatComponent implements AfterViewChecked, AfterViewInit {
export class NbChatComponent implements AfterViewInit {

static readonly SIZE_XXSMALL = 'xxsmall';
static readonly SIZE_XSMALL = 'xsmall';
Expand All @@ -164,6 +164,7 @@ export class NbChatComponent implements AfterViewChecked, AfterViewInit {
size: string;
status: string;
accent: string;
scrollBottom: boolean = true;

@Input() title: string;

Expand Down Expand Up @@ -262,15 +263,35 @@ export class NbChatComponent implements AfterViewChecked, AfterViewInit {
this.status = val;
}

/**
* Scroll chat to the bottom of the list when a new message arrives
* @param {boolean} val
*/
@Input('scrollBottom')
private set setScrollBottom(val: boolean) {
this.scrollBottom = convertToBoolProperty(val);
}

@ViewChild('scrollable') scrollable: ElementRef;
@ContentChildren(NbChatMessageComponent) messages: QueryList<NbChatMessageComponent>;

ngAfterViewChecked() {
this.scrollable.nativeElement.scrollTop = this.scrollable.nativeElement.scrollHeight;
}

ngAfterViewInit() {
this.messages.changes
.subscribe((messages) => this.messages = messages);
.subscribe((messages) => {
this.messages = messages;
this.updateView();
});

this.updateView();
}

updateView() {
if (this.scrollBottom) {
this.scrollListBottom();
}
}

scrollListBottom() {
this.scrollable.nativeElement.scrollTop = this.scrollable.nativeElement.scrollHeight;
}
}