Skip to content

Commit

Permalink
feat: add default order on bntable #823 (#824)
Browse files Browse the repository at this point in the history
  • Loading branch information
iurynogueira authored Sep 13, 2023
1 parent e8f2eff commit 3169263
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
18 changes: 18 additions & 0 deletions projects/ion/src/core/bn-table/bn-table.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,24 @@ describe('BnTable', () => {
});
});

it('should add order on payload in first load when column have a order by default', async () => {
const config: IBnTable<MockItemData> = {
service: new MockEmptyService(),
tableConfig: {
columns: [
{ label: 'Name', key: 'name', desc: true },
{ label: 'Url', key: 'url' },
],
actions: [{ label: 'Remove', icon: 'trash' }],
},
};
const bnTableWithDefaultOrder: BnTable<MockItemData> =
new BnTable<MockItemData>(config);

expect(bnTableWithDefaultOrder.payload.order).toBe('name');
expect(bnTableWithDefaultOrder.payload.sort).toBe('desc');
});

it('should return empty array when dont have data or dados', async () => {
const config: IBnTable<MockItemData> = {
service: new MockEmptyService(),
Expand Down
16 changes: 16 additions & 0 deletions projects/ion/src/core/bn-table/bn-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export default class BnTable<DataType> {

private service: BnService<DataType>;
private formatData: (data: DataType[]) => DataType[];
private firstLoad = true;

constructor(config: IBnTable<DataType>) {
this.service = config.service;
Expand All @@ -71,6 +72,21 @@ export default class BnTable<DataType> {
smartData(): void {
this.configTable.loading = true;

if (this.firstLoad) {
const firstOrderedColumn = this.configTable.columns.filter(
(column) => column.desc !== undefined
)[0];

if (firstOrderedColumn) {
this.handleSort({
column: firstOrderedColumn.key,
desc: firstOrderedColumn.desc,
});
}

this.firstLoad = false;
}

const totalRequest$ = this.payload.total
? this.service.list(this.payload).pipe(take(1))
: of(null);
Expand Down
5 changes: 4 additions & 1 deletion projects/ion/src/lib/use-table/use-table.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ export class IonUseTableComponent extends BnTable<User> {
super({
service,
tableConfig: {
columns: [{ label: 'Nome', sort: true, key: 'nome' }],
columns: [
{ label: 'Nome', sort: true, key: 'nome', desc: true },
{ label: 'Url', sort: true, key: 'url' },
],
actions: [
{
icon: 'trash',
Expand Down

0 comments on commit 3169263

Please sign in to comment.