-
Notifications
You must be signed in to change notification settings - Fork 6
/
sort-button.component.ts
72 lines (62 loc) · 1.69 KB
/
sort-button.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
export interface SortProp {
key: string;
label: string;
}
/**
* a component with a list of properties to sort a list by one of them.
* It can be used together with the DspSortBy pipe.
*/
@Component({
selector: 'app-sort-button',
templateUrl: './sort-button.component.html',
styleUrls: ['./sort-button.component.scss'],
})
export class SortButtonComponent implements OnInit {
/**
* @param sortProps[]
* An array of SortProp objects for the selection menu:
* SortProp: { key: string, label: string }
*/
@Input() sortProps: SortProp[];
/**
* @param position string
* Optional position of the sort menu: right or left
* e.g. [position='left']
*/
@Input() position: 'left' | 'right' = 'left';
/**
* @param icon
* Default icon is "sort" from material design.
* But you can replace it with another one
* e.g. sort_by_alpha
*/
@Input() icon = 'sort';
/**
* @param activeKey
* Optional parameter: selected sort property key
* By default it takes the first key from sortProps
*/
@Input() activeKey?: string;
/**
* @emits {string} sortKeyChange
*
* EventEmitter when a user selected a sort property;
* This is the selected key
*/
@Output() sortKeyChange: EventEmitter<string> = new EventEmitter<string>();
menuXPos: 'before' | 'after' = 'after';
ngOnInit() {
if (this.position === 'right') {
this.menuXPos = 'before';
}
this.sortBy(this.activeKey);
}
/**
* @param key a string to sort by
*/
sortBy(key: string) {
this.activeKey = key || this.sortProps[0].key;
this.sortKeyChange.emit(this.activeKey);
}
}