Skip to content

Commit ac753da

Browse files
committed
trying new approach
1 parent 12031e0 commit ac753da

File tree

7 files changed

+65
-17
lines changed

7 files changed

+65
-17
lines changed

packages/components/src/components/hds/table/index.hbs

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<thead class="hds-table__thead">
1414
{{#if @columns}}
1515
<Hds::Table::Tr
16+
@sortOrder={{if (eq this.sortBy @selectedItemKey) this.sortOrder}}
1617
@selectionScope="col"
1718
@isSelectable={{@isSelectable}}
1819
@onSelectionChange={{this.onSelectionAllChange}}
@@ -78,6 +79,7 @@
7879
"hds/table/tr"
7980
selectionScope="row"
8081
isSelectable=@isSelectable
82+
onClickSort=(fn this.setSortBy @selectedItemKey)
8183
onSelectionChange=this.onSelectionRowChange
8284
didInsert=this.didInsertRowCheckbox
8385
willDestroy=this.willDestroyRowCheckbox

packages/components/src/components/hds/table/index.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import type {
2323
HdsTableSortingFunction,
2424
HdsTableThSortOrder,
2525
HdsTableVerticalAlignment,
26+
HdsTableModel,
2627
} from './types';
2728
import type { HdsFormCheckboxBaseSignature } from '../form/checkbox/base';
2829
import type { HdsTableTdArgs } from './td.ts';
@@ -50,7 +51,8 @@ export interface HdsTableArgs {
5051
isFixedLayout?: boolean;
5152
isSelectable?: boolean;
5253
isStriped?: boolean;
53-
model: Array<Record<string, unknown>>;
54+
model: HdsTableModel;
55+
selectedItemKey?: string;
5456
onSelectionChange?: (selection: HdsTableOnSelectionChangeArgs) => void;
5557
onSort?: (sortBy: string, sortOrder: HdsTableThSortOrder) => void;
5658
selectionAriaLabelSuffix?: string;
@@ -190,7 +192,11 @@ export default class HdsTable extends Component<HdsTableArgs> {
190192
}
191193

192194
@action
193-
setSortBy(column: string): void {
195+
setSortBy(column?: string): void {
196+
if (column === undefined) {
197+
return;
198+
}
199+
194200
if (this.sortBy === column) {
195201
// check to see if the column is already sorted and invert the sort order if so
196202
this.sortOrder =

packages/components/src/components/hds/table/th-selectable.hbs

+14-9
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@
44
}}
55

66
<Hds::Table::Th class="hds-table__th--is-selectable" @scope={{@selectionScope}} ...attributes>
7-
<Hds::Form::Checkbox::Base
8-
id={{this.checkboxId}}
9-
class="hds-table__checkbox"
10-
checked={{@isSelected}}
11-
aria-label={{this.ariaLabel}}
12-
{{did-insert this.didInsert}}
13-
{{will-destroy this.willDestroyNode}}
14-
{{on "change" this.onSelectionChange}}
15-
/>
7+
<div class="hds-table__th--is-selectable__inner-wrapper">
8+
<Hds::Form::Checkbox::Base
9+
id={{this.checkboxId}}
10+
class="hds-table__checkbox"
11+
checked={{@isSelected}}
12+
aria-label={{this.ariaLabel}}
13+
{{did-insert this.didInsert}}
14+
{{will-destroy this.willDestroyNode}}
15+
{{on "change" this.onSelectionChange}}
16+
/>
17+
{{#if @isHeaderRow}}
18+
<Hds::Table::ThButtonSort @sortOrder={{@sortOrder}} @onClick={{@onClickSort}} />
19+
{{/if}}
20+
</div>
1621
</Hds::Table::Th>

packages/components/src/components/hds/table/th-selectable.ts

+5
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,14 @@ import { tracked } from '@glimmer/tracking';
1010
import type { HdsFormCheckboxBaseSignature } from '../form/checkbox/base';
1111
import type { HdsTableScope } from './types';
1212
import type { HdsTableThArgs } from './th';
13+
import type { HdsTableTrArgs } from './tr';
1314

1415
export interface HdsTableThSelectableArgs {
1516
Args: {
17+
isHeaderRow?: boolean;
18+
sortOrder?: HdsTableTrArgs['Args']['sortOrder'];
19+
onClickSort: HdsTableTrArgs['Args']['onClickSort'];
20+
// new above here
1621
didInsert: (
1722
checkbox: HdsFormCheckboxBaseSignature['Element'],
1823
selectionKey?: string

packages/components/src/components/hds/table/tr.hbs

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
<tr class="hds-table__tr" ...attributes>
77
{{#if @isSelectable}}
88
<Hds::Table::ThSelectable
9+
@isHeaderRow={{this.isHeaderRow}}
10+
@sortOrder={{@sortOrder}}
11+
@onClickSort={{@onClickSort}}
12+
{{!-- new above here --}}
913
@isSelected={{@isSelected}}
1014
@selectionScope={{@selectionScope}}
1115
@selectionKey={{this.selectionKey}}

packages/components/src/components/hds/table/tr.ts

+30-6
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@
66
import Component from '@glimmer/component';
77
import { assert } from '@ember/debug';
88
import { HdsTableScopeValues } from './types.ts';
9-
import type { HdsTableScope } from './types.ts';
9+
import type { HdsTableScope, HdsTableThSortOrder } from './types.ts';
1010
import type { HdsFormCheckboxBaseSignature } from '../form/checkbox/base';
11+
import { action } from '@ember/object';
12+
import { tracked } from '@glimmer/tracking';
1113

1214
export interface BaseHdsTableTrArgs {
1315
Args: {
16+
sortOrder?: HdsTableThSortOrder;
17+
onClickSort?: (sortBy?: string) => void;
18+
// NEW ABOVE
1419
isSelectable?: boolean;
1520
isSelected?: false;
1621
selectionAriaLabelSuffix?: string;
@@ -44,12 +49,24 @@ export interface SelectableHdsTableTrArgs extends BaseHdsTableTrArgs {
4449
// Union type to combine both possible states
4550
export type HdsTableTrArgs = BaseHdsTableTrArgs | SelectableHdsTableTrArgs;
4651

52+
function getRowContainerElement(
53+
element: HTMLTableRowElement
54+
): HTMLElement | null {
55+
let parent = element.parentElement;
56+
57+
while (parent && parent.tagName !== 'TABLE') {
58+
if (['TBODY', 'THEAD'].includes(parent.tagName)) {
59+
return parent;
60+
}
61+
62+
parent = parent.parentElement;
63+
}
64+
65+
return parent;
66+
}
4767
export default class HdsTableTr extends Component<HdsTableTrArgs> {
48-
/**
49-
* @param selectionKey
50-
* @type {string}
51-
* @default undefined
52-
*/
68+
@tracked isHeaderRow = false;
69+
5370
get selectionKey(): string | undefined {
5471
if (this.args.isSelectable && this.args.selectionScope === 'row') {
5572
assert(
@@ -60,4 +77,11 @@ export default class HdsTableTr extends Component<HdsTableTrArgs> {
6077
}
6178
return undefined;
6279
}
80+
81+
@action
82+
didInsert(element: HdsTableTrArgs['Element']) {
83+
const rowContainer = getRowContainerElement(element);
84+
85+
this.isHeaderRow = rowContainer?.tagName === 'THEAD';
86+
}
6387
}

packages/components/src/components/hds/table/types.ts

+2
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,5 @@ export interface HdsTableOnSelectionChangeArgs {
9191
isSelected?: boolean;
9292
}[];
9393
}
94+
95+
export type HdsTableModel = Array<Record<string, unknown>>;

0 commit comments

Comments
 (0)