-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata-table.ts
More file actions
180 lines (164 loc) · 5.14 KB
/
Copy pathdata-table.ts
File metadata and controls
180 lines (164 loc) · 5.14 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { Component, ChangeDetectionStrategy, input, output, computed } from '@angular/core';
import { AppIconComponent } from '../icon/app-icon';
import { SkeletonComponent } from '../skeleton/skeleton';
import { EmptyStateComponent } from '../empty-state/empty-state';
import {
TableAction,
TableBadgeConfig,
TableColumn,
} from '../../core/models/list-table/list-table.model';
import { BadgeVariant, BadgeComponent } from '../badge/badge';
export interface ColumnDef<T> {
key: string;
header: string;
width?: string;
sortable?: boolean;
align?: 'left' | 'center' | 'right';
cell?: (row: T) => string | number | boolean;
}
@Component({
selector: 'app-data-table',
standalone: true,
imports: [AppIconComponent, SkeletonComponent, EmptyStateComponent, BadgeComponent],
changeDetection: ChangeDetectionStrategy.OnPush,
templateUrl: './data-table.html',
styleUrl: './data-table.css',
})
export class DataTableComponent<T extends Record<string, unknown>> {
readonly columns = input.required<TableColumn<T>[]>();
readonly data = input.required<T[]>();
readonly loading = input<boolean>(false);
readonly actions = input<TableAction<T>[]>([]);
readonly hasActions = input<boolean>(true);
readonly roundedBottom = input<boolean>(true);
readonly emptyTitle = input<string>('Nenhum dado encontrado');
readonly emptyDescription = input<string>('Não existem registros cadastrados para exibição.');
readonly containerClasses = computed(() => {
const base =
'w-full bg-white dark:bg-slate-800 border border-gray-200/80 dark:border-slate-700/80 shadow-2xs overflow-hidden';
return this.roundedBottom() ? `${base} rounded-[12px]` : `${base} rounded-t-[12px] border-b-0`;
});
readonly rowClick = output<T>();
readonly actionClick = output<{ actionId: string; row: T }>();
readonly editRow = output<T>();
readonly deleteRow = output<T>();
resolveValue(row: T, col: TableColumn<T>): unknown {
if (col.valueGetter) {
return col.valueGetter(row);
}
const key = col.key;
if (key.includes('.')) {
const parts = key.split('.');
let current: unknown = row;
for (const p of parts) {
if (current && typeof current === 'object' && p in (current as Record<string, unknown>)) {
current = (current as Record<string, unknown>)[p];
} else {
return undefined;
}
}
return current;
}
return row[key];
}
getFormattedValue(row: T, col: TableColumn<T>): string {
if (col.cellFormatter) {
const res = col.cellFormatter(row);
return res !== null && res !== undefined ? String(res) : '';
}
const val = this.resolveValue(row, col);
if (col.type === 'currency' && typeof val === 'number') {
return new Intl.NumberFormat('pt-BR', {
style: 'currency',
currency: 'BRL',
}).format(val);
}
if (col.type === 'date' && typeof val === 'string' && val) {
const d = new Date(val);
if (!isNaN(d.getTime())) {
return d.toLocaleDateString('pt-BR');
}
}
if (val === null || val === undefined) {
return '-';
}
return String(val);
}
getBadgeConfig(row: T, col: TableColumn<T>): TableBadgeConfig {
if (col.badgeConfig) {
return col.badgeConfig(row);
}
const val = this.resolveValue(row, col);
if (typeof val === 'boolean') {
return {
text: val ? 'Ativo' : 'Inativo',
variant: val ? 'success' : 'neutral',
};
}
return {
text: String(val ?? 'N/A'),
variant: 'neutral',
};
}
mapVariant(v?: string): BadgeVariant {
switch (v) {
case 'success':
return 'success';
case 'warning':
return 'warning';
case 'danger':
case 'error':
return 'error';
case 'info':
return 'info';
case 'primary':
return 'primary';
default:
return 'neutral';
}
}
getVisibleActions(row: T): TableAction<T>[] {
const list = this.actions();
if (list.length > 0) {
return list.filter((act) => (act.visible ? act.visible(row) : true));
}
// Default fallback actions if no explicit actions array passed
if (this.hasActions()) {
return [
{
id: 'edit',
label: 'Editar',
icon: 'edit',
colorClass: 'text-gray-400 hover:text-[#4F8A6B]',
title: 'Editar',
handler: (r) => this.editRow.emit(r),
},
{
id: 'delete',
label: 'Excluir',
icon: 'trash-2',
colorClass: 'text-gray-400 hover:text-[#D66A6A]',
title: 'Excluir',
handler: (r) => this.deleteRow.emit(r),
},
];
}
return [];
}
onRowClick(row: T): void {
this.rowClick.emit(row);
}
onActionClick(action: TableAction<T>, row: T, event: MouseEvent): void {
event.stopPropagation();
if (action.id) {
action.handler(row);
}
console.log('[ON ACTION CLICK]', action, action.id);
this.actionClick.emit({ actionId: action.id, row });
// if (action.id === 'edit') {
// this.editRow.emit(row);
// } else if (action.id === 'delete') {
// this.deleteRow.emit(row);
// }
}
}