Skip to content

Commit

Permalink
feat(Table): support compact density
Browse files Browse the repository at this point in the history
Now supporting two density settings: `comfy` and `compact`

ISSUES CLOSED: #1676
  • Loading branch information
benjamincharity committed Oct 25, 2019
1 parent 83fe61d commit a9ff309
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 13 deletions.
11 changes: 7 additions & 4 deletions demo/app/components/components.component.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import {
Component,
OnDestroy,
OnInit,
VERSION as NG_VERSION,
} from '@angular/core';
import { FormControl } from '@angular/forms';
import { VERSION as MAT_VERSION } from '@angular/material';
import {
NavigationEnd, Route,
NavigationEnd,
Route,
Router,
Routes,
} from '@angular/router';
Expand All @@ -17,11 +19,12 @@ import {
import { TsSelectionListChange } from '@terminus/ui/selection-list';
import { VERSION as UI_VERSION } from '@terminus/ui/utilities';
import {
BehaviorSubject, Observable,
BehaviorSubject,
Observable,
} from 'rxjs';
import { map } from 'rxjs/operators';

import { orderArrayByProperty } from './../utilities/orderArrayByProperty';
import { orderArrayByProperty } from '../utilities/orderArrayByProperty';
import { componentsList } from './components.constant';


Expand All @@ -30,7 +33,7 @@ import { componentsList } from './components.constant';
templateUrl: './components.component.html',
styleUrls: ['./components.component.scss'],
})
export class ComponentsComponent implements OnInit {
export class ComponentsComponent implements OnInit, OnDestroy {
public components = orderArrayByProperty(componentsList, 'path');
public path = '';
public uiVersion = UI_VERSION;
Expand Down
18 changes: 15 additions & 3 deletions demo/app/components/table/table.component.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div fxLayout="row" fxLayoutAlign="space-between center">
<div fxLayout="row" fxLayoutAlign="space-between center" tsVerticalSpacing>
<div style="font-size: 12px;">
<p>
To get around GitHub rate limiting, we cache response data by default.
Expand All @@ -14,6 +14,16 @@

<button (click)="clearCachedData()">Clear cached GitHub data</button>
</div>

<div>
<label>
Table density:
<select name="density" id="density" [(ngModel)]="density">
<option value="comfy">Comfy</option>
<option value="compact">Compact</option>
</select>
</label>
</div>
</div>

<span fxFlex></span>
Expand Down Expand Up @@ -57,7 +67,9 @@


<div class="example-container">
<table ts-table
<table
ts-table
[density]="density"
[dataSource]="dataSource"
[columns]="visibleColumns"
(columnsChange)="columnsChange($event)"
Expand Down Expand Up @@ -85,7 +97,7 @@
</td>
</ng-container>

<ng-container tsColumnDef="comments">
<ng-container tsColumnDef="comments" alignment="right">
<th ts-header-cell *tsHeaderCellDef>
Comments
</th>
Expand Down
3 changes: 2 additions & 1 deletion demo/app/components/table/table.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
TsColumn,
TsTableColumnsChangeEvent,
TsTableComponent,
TsTableDataSource,
TsTableDataSource, TsTableDensity,
} from '@terminus/ui/table';
import {
merge,
Expand Down Expand Up @@ -155,6 +155,7 @@ export class TableComponent implements AfterViewInit {
public exampleDatabase!: ExampleHttpDao;
public dataSource = new TsTableDataSource<GithubIssue>();
public resultsLength = 0;
public density: TsTableDensity = 'comfy';

/**
* Return all visible columns
Expand Down
12 changes: 12 additions & 0 deletions terminus-ui/table/src/table.component.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- [Sticky column](#sticky-column)
- [Sticky column at end](#sticky-column-at-end)
- [Re-orderable columns](#re-orderable-columns)
- [Density](#density)
- [Events](#events)
- [Table](#table)
- [Cell](#cell)
Expand Down Expand Up @@ -472,6 +473,17 @@ export class TableComponent {
```


## Density

The table supports two density settings: `comfy` (default) & `compact`.

```html
<table ts-table density="compact">
...
</table>
```


## Events

### Table
Expand Down
9 changes: 7 additions & 2 deletions terminus-ui/table/src/table.component.scss
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
$primary: color(primary);
--drop-bg: #{desaturate(lighten($primary, 66%), 70%)};
--z-index-base-context: 1;
--cell-padding: #{spacing(default)};
// Must be greater than 40 as that is the length of the generated z-indexes for header cells
--sticky-end-z: 50;
@include reset;
Expand All @@ -34,6 +35,10 @@
max-width: 100%;
overflow: scroll;

&.ts-table--compact {
--cell-padding: #{spacing(small, 2)};
}

// Class added to all sticky-end cells
.ts-table__column--sticky-end {
// For the last sticky cell of a row, hide the overflow so that the resize grabber doesn't create extra space past the final cell.
Expand Down Expand Up @@ -114,7 +119,7 @@
.ts-cell {
background-color: var(--table-bg);
overflow: hidden;
padding: spacing(default);
padding: var(--cell-padding);
text-overflow: ellipsis;
transition: background-color 200ms ease-out;
white-space: nowrap;
Expand All @@ -128,7 +133,7 @@
.ts-header-cell {
background-color: var(--header-bg);
border-color: var(--header-border-color);
padding: spacing(default);
padding: var(--cell-padding);

// Class added when the column is sorted
&.ts-sort-header-sorted {
Expand Down
18 changes: 18 additions & 0 deletions terminus-ui/table/src/table.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,4 +316,22 @@ describe(`TsTableComponent`, function() {

});

describe(`density`, () => {

test(`should be able to change the density of the table spacing`, () => {
const fixture = createComponent(testComponents.TableApp, undefined, [TsTableModule]);
fixture.detectChanges();
const tableElement = fixture.nativeElement.querySelector('.ts-table');
expect(tableElement.classList).toContain('ts-table--comfy');
expect(tableElement.classList).not.toContain('ts-table--compact');

fixture.componentInstance.density = 'compact';
fixture.detectChanges();

expect(tableElement.classList).not.toContain('ts-table--comfy');
expect(tableElement.classList).toContain('ts-table--compact');
});

});

});
20 changes: 19 additions & 1 deletion terminus-ui/table/src/table.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ export interface TsColumn {
[key: string]: any;
}

/**
* The possible table density settings
*/
export type TsTableDensity
= 'comfy'
| 'compact'
;

/**
* Default column width.
*
Expand Down Expand Up @@ -119,7 +127,11 @@ export class TsTableColumnsChangeEvent {
selector: 'ts-table, table[ts-table]',
template: CDK_TABLE_TEMPLATE,
styleUrls: ['./table.component.scss'],
host: { class: 'ts-table' },
host: {
'class': 'ts-table',
'[class.ts-table--comfy]': 'density === "comfy"',
'[class.ts-table--compact]': 'density === "compact"',
},
providers: [{
provide: CdkTable,
useExisting: TsTableComponent,
Expand Down Expand Up @@ -212,6 +224,12 @@ export class TsTableComponent<T = any> extends CdkTable<T> implements OnInit, Af
}
private _columns: TsColumn[] = [];

/**
* Define the density of the cells
*/
@Input()
public density: TsTableDensity = 'comfy';

/**
* Emit when a column is resized
*
Expand Down
5 changes: 3 additions & 2 deletions terminus-ui/table/testing/src/test-components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import {
TsTableColumnsChangeEvent,
TsTableComponent,
TsTableDataSource,
TsTableDataSource, TsTableDensity,
TsTableModule,
} from '@terminus/ui/table';

Expand All @@ -25,6 +25,7 @@ import {
<table
ts-table
[dataSource]="dataSource"
[density]="density"
[columns]="columns"
(columnsChange)="columnsChanged($event)"
#myTable="tsTable"
Expand Down Expand Up @@ -57,7 +58,7 @@ import {
export class TableApp {
@ViewChild(TsTableComponent, { static: true })
public table!: TsTableComponent<TestData>;

public density: TsTableDensity = 'comfy';
public dataSource: FakeDataSource | null = new FakeDataSource();
public columnsToRender = ['column_a', 'column_b', 'column_c'];
public columns = this.columnsToRender.map(c => ({
Expand Down

0 comments on commit a9ff309

Please sign in to comment.