Skip to content

Commit

Permalink
feat: Add property to condense pixTable
Browse files Browse the repository at this point in the history
Co-authored-by: Iris Benoit <[email protected]>
Co-authored-by: Fael Bassetti <[email protected]>
Co-authored-by: Laura Bergoens <[email protected]>
  • Loading branch information
4 people authored and Laura Bergoens committed Jan 13, 2025
1 parent 8c67f09 commit 88bfeee
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 2 deletions.
2 changes: 1 addition & 1 deletion addon/components/pix-table.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="pix-table">
<table>
<table class={{this.tableClass}}>
<caption class="screen-reader-only">{{this.caption}}</caption>
<thead class={{this.headerClass}}>
<tr>
Expand Down
14 changes: 14 additions & 0 deletions addon/components/pix-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ export default class PixTable extends Component {
return this.args.caption;
}

get tableClass() {
warn(
'PixTable: @condensed must be a boolean, default undefined',
[true, false, undefined].includes(this.args.condensed),
{
id: 'pix-ui.pix-table.condensed.not-boolean',
},
);
if (this.args.condensed) {
return 'pix-table--condensed';
}
return null;
}

get headerClass() {
return `pix-table-header--${this.variant}`;
}
Expand Down
6 changes: 6 additions & 0 deletions addon/styles/_pix-table.scss
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@

@extend %pix-body-s;

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

table {
min-width: 100%;
border-collapse: collapse;
Expand Down
16 changes: 15 additions & 1 deletion app/stories/pix-table.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,25 @@ export default {
required: false,
},
},
condensed: {
name: 'condensed',
description: 'Afficher le tableau en mode condensé',
type: {
name: 'boolean',
required: false,
},
},
},
};

const Template = (args) => {
return {
template: hbs`<PixTable @variant={{this.variant}} @data={{this.data}} @caption={{this.caption}}>
template: hbs`<PixTable
@variant={{this.variant}}
@data={{this.data}}
@caption={{this.caption}}
@condensed={{this.condensed}}
>
<:columns as |row context|>
<PixTableColumn @context={{context}} @type='text'>
<:header>
Expand Down Expand Up @@ -81,6 +94,7 @@ const Template = (args) => {
export const Default = Template.bind({});
Default.args = {
caption: 'Description du tableau',
condensed: false,
data: [
{
name: 'jean',
Expand Down
109 changes: 109 additions & 0 deletions tests/integration/components/pix-table-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,96 @@ module('Integration | Component | table', function (hooks) {
});
});

module('#condensed', function () {
test('it should not be condensed by default', async function (assert) {
// when
await render(
hbs`<PixTable @caption='Ceci est le caption de notre table' @data={{this.data}}>
<: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>`,
);

// then
assert.notOk(this.element.querySelector('table').getAttribute('class'));
});

test('it should be condensed', async function (assert) {
// given
this.condensed = true;

// when
await render(
hbs`<PixTable
@caption='Ceci est le caption de notre table'
@data={{this.data}}
@condensed={{this.condensed}}
>
<: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>`,
);

// then
assert.strictEqual(
this.element.querySelector('table').getAttribute('class'),
'pix-table__condensed',
);
});
});

module('#sort', function () {
test('it should call @onSort on click', async function (assert) {
// given
Expand Down Expand Up @@ -318,6 +408,25 @@ module('Integration | Component | table', function (hooks) {
);
});

test('it should warn when @condensed is incorrect', async function (assert) {
// when
this.data = [];
await render(
hbs`<PixTable @data={{this.data}} @condensed={{null}} @caption='On condense ?'>
<:columns as |row context|>
<PixTableColumn @context={{context}} />
</:columns>
</PixTable>`,
);

// then
assert.ok(
warnStub.calledWithExactly(
'WARNING: PixTable: @condensed must be a boolean, default undefined',
),
);
});

[
{
ariaLabelDefaultSort: 'tri',
Expand Down

0 comments on commit 88bfeee

Please sign in to comment.