Skip to content

Commit

Permalink
feat: select row when clicking row
Browse files Browse the repository at this point in the history
  • Loading branch information
Westbrook Johnson authored and najikahalsema committed Aug 26, 2022
1 parent 6091842 commit 294523c
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/table/src/TableCheckboxCell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ export class TableCheckboxCell extends SpectrumElement {
@property({ type: Boolean, reflect: true, attribute: 'selects-single' })
public selectsSingle = false;

public override click(): void {
this.checkbox.click();
}

protected override render(): TemplateResult {
return html`
<sp-checkbox
Expand Down
39 changes: 38 additions & 1 deletion packages/table/src/TableRow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ export class TableRow extends SpectrumElement {
@property({ reflect: true })
public role = 'row';

@property({ type: Boolean })
public selectable = false;

@property({ type: Boolean, reflect: true })
public selected = false;

Expand All @@ -60,21 +63,55 @@ export class TableRow extends SpectrumElement {
}
}

protected handleSlotchange({
target,
}: Event & { target: HTMLSlotElement }): void {
const assignedElements = target.assignedElements();
this.selectable = !!assignedElements.find(
(el) => el.localName === 'sp-table-checkbox-cell'
);
}

protected manageSelected(): void {
const [checkboxCell] = this.checkboxCells;
if (!checkboxCell) return;
checkboxCell.checked = this.selected;
}

protected handleClick(event: Event): void {
if (
event
.composedPath()
.find(
(node) => (node as HTMLElement).localName === 'sp-checkbox'
)
) {
return;
}
const [checkboxCell] = this.checkboxCells;
if (!checkboxCell) return;
checkboxCell.click();
}

protected override render(): TemplateResult {
return html`
<slot @change=${this.handleChange}></slot>
<slot
@change=${this.handleChange}
@slotchange=${this.handleSlotchange}
></slot>
`;
}

protected override willUpdate(changed: PropertyValues<this>): void {
if (changed.has('selected')) {
this.manageSelected();
}
if (changed.has('selectable')) {
if (this.selectable) {
this.addEventListener('click', this.handleClick);
} else {
this.removeEventListener('click', this.handleClick);
}
}
}
}
6 changes: 6 additions & 0 deletions packages/table/test/table-selects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ describe('Table Selects', () => {
expect(rowThreeCheckbox.checkbox.checked).to.be.true;
expect(rowTwoCheckbox.checkbox.checked).to.be.false;
expect(el.selected.length).to.equal(1);

rowTwo.click();
await elementUpdated(el);

expect(rowTwoCheckbox.checked).to.be.true;
expect(el.selected).to.deep.equal(['row2']);
});

it('surfaces [selects="multiple"] selection', async () => {
Expand Down
5 changes: 5 additions & 0 deletions packages/table/test/virtualized-table-selects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ describe('Virtualized Table Selects', () => {
await elementUpdated(el);

expect(el.selected).to.deep.equal(['2']);

rowTwo.click();
await elementUpdated(el);

expect(el.selected).to.deep.equal(['1']);
});

it('surfaces [selects="multiple"] selection on Virtualized Table', async () => {
Expand Down

0 comments on commit 294523c

Please sign in to comment.