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] Amélioration PixTable (PIX-16753) #832

Merged
merged 4 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 7 additions & 4 deletions addon/components/pix-table.hbs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="pix-table">
<div class="pix-table" ...attributes>
<table class={{this.tableClass}}>
<caption class="screen-reader-only">{{this.caption}}</caption>
<thead class={{this.headerClass}}>
Expand All @@ -7,9 +7,12 @@
</tr>
</thead>
<tbody>
{{#each @data as |row|}}
<tr>
{{yield row "cell" to="columns"}}
{{#each @data as |row index|}}
<tr
class={{if this.hasOnRowClick "pix-table__clickable-row" ""}}
{{on "click" (fn this.onClick row)}}
>
{{yield row "cell" index to="columns"}}
</tr>
{{/each}}
</tbody>
Expand Down
13 changes: 13 additions & 0 deletions addon/components/pix-table.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { warn } from '@ember/debug';
import { action } from '@ember/object';
import Component from '@glimmer/component';

export default class PixTable extends Component {
Expand Down Expand Up @@ -39,4 +40,16 @@ export default class PixTable extends Component {
get headerClass() {
return `pix-table-header--${this.variant}`;
}

get hasOnRowClick() {
return typeof this.args.onRowClick === 'function';
}

@action
onClick(row, event) {
event.stopPropagation();
if (this.hasOnRowClick) {
this.args.onRowClick(row);
}
}
}
26 changes: 23 additions & 3 deletions addon/styles/_pix-table.scss
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,38 @@

@extend %pix-body-s;

.pix-table__condensed {
&__condensed {
th, td {
padding: var(--pix-spacing-2x) var(--pix-spacing-4x);
}
}

&__clickable-row {
cursor: pointer;

&:hover, &:focus, &:active {
transition: 0.25s ease;
}

&:hover {
background-color: rgba(var(--pix-neutral-100-inline), 0.50);
}

&:focus {
background-color: rgba(var(--pix-neutral-100-inline), 0.40);
}

&:active {
background-color: rgba(var(--pix-neutral-100-inline), 0.75);
}
}

table {
min-width: 100%;
border-collapse: collapse;

tbody > tr:nth-of-type(even) {
background-color: var(--pix-neutral-20);
tbody > tr:nth-of-type(even):not(.pix-table__clickable-row:hover, .pix-table__clickable-row:focus, .pix-table__clickable-row:active) {
background-color: rgba(var(--pix-neutral-20-inline), 0.80);
}

thead.pix-table-header {
Expand Down
6 changes: 6 additions & 0 deletions app/stories/pix-table.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ export default {
'Définition du rendu des différentes colonnes de la table en utilisant `<PixTableColumn>`. Expose les paramètres `row` et `context` (correspondant aux données de la ligne actuelle)',
type: { name: 'block content', required: true },
},
onRowClick: {
name: 'onRowClick',
description:
"Permet d'ajouter un onClick sur le <tr> de chaque ligne, la fonction en paramètre récupérera l'objet au complet.",
type: { name: 'function', required: false },
},
variant: {
name: 'variant',
description: "Afficher le bon variant pour l'application",
Expand Down
3 changes: 3 additions & 0 deletions tests/dummy/app/controllers/table-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ export default class TablePage extends Controller {
}
}

@action
onClick() {}

@action
onNumSort() {
this.resetOrders('num');
Expand Down
7 changes: 6 additions & 1 deletion tests/dummy/app/templates/table-page.hbs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<PixTable @variant={{this.variant}} @data={{this.data}} @caption={{this.caption}}>
<PixTable
@variant={{this.variant}}
@data={{this.data}}
@caption={{this.caption}}
@onRowClick={{this.onClick}}
>
<:columns as |row context|>
<PixTableColumn
@context={{context}}
Expand Down
49 changes: 49 additions & 0 deletions tests/integration/components/pix-table-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,55 @@ module('Integration | Component | table', function (hooks) {
});
});

module('#onRowClick', function () {
test('should call onClick on clicked row', async function (assert) {
this.onClick = sinon.stub();

const screen = await render(
hbs`<PixTable
@caption='Ceci est le caption de notre table'
@data={{this.data}}
@onRowClick={{this.onClick}}
>
<:columns as |row context|>
<PixTableColumn @context={{context}}>
<:header>
Nom
</:header>
<:cell>
{{row.name}}
</:cell>
</PixTableColumn>
<PixTableColumn @context={{context}}>
<:header>
Description
</:header>
<:cell>
{{row.description}}
</:cell>
</PixTableColumn>
<PixTableColumn @context={{context}}>
<:header>
Age
</:header>
<:cell>
il a
{{row.age}}
ans
</:cell>
</PixTableColumn>
</:columns>
</PixTable>`,
);

//when
await click(screen.getByText('jean'));

// then
assert.ok(this.onClick.calledWithExactly(this.data[0]));
});
});

module('#sort', function () {
test('it should call @onSort on click', async function (assert) {
// given
Expand Down