Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/1364 data grid review #1423

Merged
merged 40 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
3297025
removed default value, added tests
Diaan Jul 16, 2024
70718bc
added tests for column-group
Diaan Jul 17, 2024
8f2ec04
fixed column width and made sl-column next to sl-column-group possible
Diaan Jul 19, 2024
4513aed
changed around the odd and even row colours to better match header
Diaan Jul 22, 2024
ab516e9
Merge remote-tracking branch 'origin/main' into feature/1364-data-gri…
Diaan Jul 22, 2024
54d2994
added comments, attempt to improve sorter
Diaan Jul 23, 2024
bbe1c3a
attempt to write tests for sorter + clean up code
Diaan Jul 24, 2024
6362028
changeset added
anna-lach Aug 8, 2024
9772054
filter column tests (in progress)
anna-lach Aug 8, 2024
52f135d
Merge remote-tracking branch 'origin/main' into feature/1364-data-gri…
anna-lach Aug 9, 2024
1b7c063
filter column tests changes, initial test for filter
anna-lach Aug 9, 2024
c4c954b
filter and filter column unit tests changes
anna-lach Aug 12, 2024
fc116a1
filter tests changes
anna-lach Aug 13, 2024
03e4ad5
filter tests updates (not working yet)
anna-lach Aug 14, 2024
9cbd912
Merge remote-tracking branch 'origin/main' into feature/1364-data-gri…
anna-lach Aug 19, 2024
bcf2438
trying to fix filter tests
anna-lach Aug 19, 2024
c62062f
filter column tests update, still cannot check options
anna-lach Aug 20, 2024
1a7f297
Merge remote-tracking branch 'origin/main' into feature/1364-data-gri…
anna-lach Aug 21, 2024
45580d6
filter and filter column tests changes
anna-lach Aug 21, 2024
e62ae80
Merge branch 'main' into feature/1364-data-grid-review
jpzwarte Aug 23, 2024
7b120ab
🚂
jpzwarte Aug 23, 2024
d2d35a7
🛥
jpzwarte Aug 23, 2024
adcc122
Merge remote-tracking branch 'origin/main' into feature/1364-data-gri…
anna-lach Aug 23, 2024
09bec34
Merge remote-tracking branch 'origin/main' into feature/1364-data-gri…
anna-lach Aug 23, 2024
7a4ef6f
filter column tests changes
anna-lach Aug 23, 2024
81ac1db
filter column tests, filter test change
anna-lach Aug 26, 2024
9505b39
Merge remote-tracking branch 'origin/main' into feature/1364-data-gri…
anna-lach Aug 26, 2024
9dbe0bc
Trying to change filter test example
anna-lach Aug 27, 2024
c91994e
Merge remote-tracking branch 'origin/main' into feature/1364-data-gri…
anna-lach Aug 27, 2024
d7eac3a
trying to improve the example in filter tests
anna-lach Aug 27, 2024
c43d6cd
filter test updated
anna-lach Aug 28, 2024
1e2986e
new tests, removed unnecessary elements
anna-lach Aug 28, 2024
9ef8a14
cleanup
anna-lach Aug 29, 2024
114096c
missing export
anna-lach Aug 29, 2024
199d153
Merge remote-tracking branch 'origin/main' into feature/1364-data-gri…
anna-lach Sep 11, 2024
0ae6b24
filter and filter column changes after review
anna-lach Sep 11, 2024
b97476a
review comments
Diaan Sep 12, 2024
b6cc3e3
fixed columns widths in non-grouped columns next to grouped columns
Diaan Sep 16, 2024
4bbe387
fix build
Diaan Sep 16, 2024
e8bf01b
Merge branch 'main' into feature/1364-data-grid-review
Diaan Sep 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/components/grid/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export * from './src/grid.js';
export * from './src/select-column.js';
export * from './src/selection-column.js';
export * from './src/sort-column.js';
export * from './src/sorter.js';
Diaan marked this conversation as resolved.
Show resolved Hide resolved
export * from './src/text-field-column.js';
2 changes: 2 additions & 0 deletions packages/components/grid/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Grid } from './src/grid.js';
import { GridSelectColumn } from './src/select-column.js';
import { GridSelectionColumn } from './src/selection-column.js';
import { GridSortColumn } from './src/sort-column.js';
import { GridSorter } from './src/sorter.js';
import { GridTextFieldColumn } from './src/text-field-column.js';

customElements.define('sl-grid', Grid);
Expand All @@ -16,4 +17,5 @@ customElements.define('sl-grid-filter-column', GridFilterColumn);
customElements.define('sl-grid-select-column', GridSelectColumn);
customElements.define('sl-grid-selection-column', GridSelectionColumn);
customElements.define('sl-grid-sort-column', GridSortColumn);
customElements.define('sl-grid-sorter', GridSorter);
Diaan marked this conversation as resolved.
Show resolved Hide resolved
customElements.define('sl-grid-text-field-column', GridTextFieldColumn);
104 changes: 104 additions & 0 deletions packages/components/grid/src/column-group.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { setupIgnoreWindowResizeObserverLoopErrors } from '@lit-labs/virtualizer/support/resize-observer-errors.js';
import { expect, fixture } from '@open-wc/testing';
import { html } from 'lit';
import '../register.js';
import { type Grid } from './grid.js';

setupIgnoreWindowResizeObserverLoopErrors(beforeEach, afterEach);

describe('sl-column-group', () => {
let el: Grid;

describe('defaults', () => {
beforeEach(async () => {
el = await fixture(html`
<sl-grid>
<sl-grid-column-group header="Name">
<sl-grid-column path="firstName"></sl-grid-column>
<sl-grid-column path="lastName"></sl-grid-column>
</sl-grid-column-group>
<sl-grid-column-group header="Grades">
<sl-grid-column path="grades.biology"></sl-grid-column>
<sl-grid-column path="grades.maths"></sl-grid-column>
<sl-grid-column path="grades.english"></sl-grid-column>
<sl-grid-column path="age"></sl-grid-column>
</sl-grid-column-group>
</sl-grid>
`);
el.items = [{ firstName: 'John', lastName: 'Doe', grades: { biology: 'A', maths: 'B', english: 'B+' } }];
await el.updateComplete;

// Give grid time to render the table structure
await new Promise(resolve => setTimeout(resolve, 100));
await el.updateComplete;
});

it('should render column headers', () => {
const columns = Array.from(el.renderRoot.querySelectorAll('th')).map(col => col.textContent);

expect(columns).to.deep.equal([
'Name',
'Grades',
'First name',
'Last name',
'Biology',
'Maths',
'English',
'Age'
]);
});

it('should have the correct width', () => {
const cells = Array.from(el.renderRoot.querySelectorAll('th'));
expect(cells.map(cell => Math.floor(parseFloat(getComputedStyle(cell).width)))).to.deep.equal([
300, 481, 151, 148, 128, 120, 128, 103
]);
});

// it('should emit an sl-column-update event after clicking the checkbox', async () => {
// const columnUpdateEvent = spy();
// const columnGoup = el.querySelector('sl-grid-column-group:first-of-type') as GridColumnGroup;
// columnGoup?.addEventListener('sl-column-update', columnUpdateEvent);

// const newColumn = document.createElement('sl-grid-column');
// await new Promise(resolve => setTimeout(resolve, 100));

// columnGoup?.appendChild(newColumn);

// // expect(columnUpdateEvent).to.have.been.called;
// expect(columnGoup?.columns).to.equal(3);
// });
});

describe('explicit width', () => {
beforeEach(async () => {
el = await fixture(html`
<sl-grid>
<sl-grid-column-group header="Name">
<sl-grid-column path="firstName"></sl-grid-column>
<sl-grid-column path="lastName"></sl-grid-column>
</sl-grid-column-group>
<sl-grid-column-group header="Grades" width="600">
<sl-grid-column path="grades.biology"></sl-grid-column>
<sl-grid-column path="grades.maths"></sl-grid-column>
<sl-grid-column path="grades.english"></sl-grid-column>
</sl-grid-column-group>
</sl-grid>
`);

el.items = [{ firstName: 'John', lastName: 'Doe', grades: { biology: 'A', maths: 'B', english: 'B+' } }];
await el.updateComplete;

// Give grid time to render the table structure
await new Promise(resolve => setTimeout(resolve, 100));
await el.updateComplete;
});

it('should have the correct width when one is set explicitly', () => {
const cells = Array.from(el.renderRoot.querySelectorAll('th'));
expect(cells.map(cell => Math.floor(parseFloat(getComputedStyle(cell).width)))).to.deep.equal([
209, 600, 177, 175, 155, 147, 155
jpzwarte marked this conversation as resolved.
Show resolved Hide resolved
]);
});
});
});
3 changes: 1 addition & 2 deletions packages/components/grid/src/column-group.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { getNameByPath } from '@sl-design-system/shared';
import { type PropertyValues, type TemplateResult, html } from 'lit';
import { state } from 'lit/decorators.js';
import { GridColumn } from './column.js';
Expand Down Expand Up @@ -35,7 +34,7 @@ export class GridColumnGroup<T = unknown> extends GridColumn<T> {
}

override renderHeader(): TemplateResult {
return html`<th colspan=${this.columns.length}>${this.header ?? getNameByPath(this.path)}</th>`;
jpzwarte marked this conversation as resolved.
Show resolved Hide resolved
return html`<th colspan=${Math.max(this.columns.length, 1)}>${this.header}</th>`;
}

#onSlotchange(event: Event & { target: HTMLSlotElement }): void {
Expand Down
100 changes: 100 additions & 0 deletions packages/components/grid/src/column.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { setupIgnoreWindowResizeObserverLoopErrors } from '@lit-labs/virtualizer/support/resize-observer-errors.js';
import { expect, fixture } from '@open-wc/testing';
import { Avatar } from '@sl-design-system/avatar';
import { html } from 'lit';
import { Person } from 'tools/example-data/index.js';
import '../register.js';
import { GridColumnDataRenderer } from './column.js';
import { type Grid } from './grid.js';

setupIgnoreWindowResizeObserverLoopErrors(beforeEach, afterEach);

describe('sl-column', () => {
let el: Grid;
let cells: HTMLElement[];

describe('defaults', () => {
beforeEach(async () => {
el = await fixture(html`
<sl-grid>
<sl-grid-column path="firstName"></sl-grid-column>
<sl-grid-column path="lastName"></sl-grid-column>
<sl-grid-column path="age" align="end" grow="3"></sl-grid-column>
</sl-grid>
`);
el.items = [
{ firstName: 'John', lastName: 'Doe', age: 20 },
{ firstName: 'Jane', lastName: 'Smith', age: 40 }
];
await el.updateComplete;

// Give grid time to render the table structure
await new Promise(resolve => setTimeout(resolve, 100));
await el.updateComplete;

cells = Array.from(el.renderRoot.querySelectorAll('tbody tr:first-of-type td'));
});

it('should render column headers', () => {
const columns = Array.from(el.renderRoot.querySelectorAll('th')).map(col => col.textContent);

expect(columns).to.deep.equal(['First name', 'Last name', 'Age']);
Diaan marked this conversation as resolved.
Show resolved Hide resolved
});

it('should have the right justify-content value', () => {
expect(cells.map(cell => getComputedStyle(cell).justifyContent)).to.deep.equal(['start', 'start', 'end']);
});

it('should have the right grow value', () => {
expect(cells.map(cell => getComputedStyle(cell).flexGrow)).to.deep.equal(['1', '1', '3']);
});

it('should have the right parts', () => {
expect(cells.map(cell => cell.getAttribute('part'))).to.deep.equal([
'data first-name',
'data last-name',
'data age'
]);
});
});

describe('custom renderer', () => {
beforeEach(async () => {
const avatarRenderer: GridColumnDataRenderer<Person> = ({ firstName, lastName }) => {
return html`<sl-avatar .displayName=${[firstName, lastName].join(' ')} size="sm"></sl-avatar>`;
};

el = await fixture(html`
<sl-grid>
<sl-grid-column
header="Person"
.renderer=${avatarRenderer}
.scopedElements=${{
'sl-avatar': Avatar
}}
></sl-grid-column>
<sl-grid-column path="age" parts="number"></sl-grid-column>
</sl-grid>
`);
el.items = [
{ firstName: 'John', lastName: 'Doe', age: 20 },
{ firstName: 'Jane', lastName: 'Smith', age: 40 }
];
await el.updateComplete;

// Give grid time to render the table structure
await new Promise(resolve => setTimeout(resolve, 100));
await el.updateComplete;

cells = Array.from(el.renderRoot.querySelectorAll('tbody tr:first-of-type td'));
});

it('should render the elements set with the custom renderer', () => {
expect(cells[0]).to.contain('sl-avatar');
Diaan marked this conversation as resolved.
Show resolved Hide resolved
});

it('should have the right parts, including one set on the column', () => {
expect(cells.map(cell => cell.getAttribute('part'))).to.deep.equal(['data', 'data number age']);
});
});
});
3 changes: 2 additions & 1 deletion packages/components/grid/src/column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class GridColumn<T = any> extends LitElement {
#width?: number;

/** The alignment of the content within the column. */
@property() align: GridColumnAlignment = 'start';
@property() align?: GridColumnAlignment;

/**
* Automatically sets the width of the column based on the column contents when this is set to `true`.
Expand Down Expand Up @@ -160,6 +160,7 @@ export class GridColumn<T = any> extends LitElement {
if (typeof this.parts === 'string') {
parts = this.parts.split(' ');
} else if (typeof this.parts === 'function' && item) {
// TODO: what does this do? How can parts ever be a function? According to the typing this should not be possible.
Diaan marked this conversation as resolved.
Show resolved Hide resolved
parts = this.parts(item)?.split(' ') ?? [];
}

Expand Down
Loading