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

Converted Body, TBody, Tbody/Row and Tbody/Row/Cell to glimmer #351

Closed
wants to merge 1 commit into from
Closed
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
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# v

#### 🚨 Breaking Changes

- cells no longer support the ability to specify `visible` on the cells themselves. Please set `visible` on the column

#### ✨ Features

- Converted components to user glimmer syntax [#345](https://github.com/miguelcobain/ember-yeti-table/pull/345) [#351](https://github.com/miguelcobain/ember-yeti-table/pull/351)

# v1.7.0

#### ✨ Features
Expand Down
4 changes: 2 additions & 2 deletions addon/components/yeti-table/body.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

{{#each @data as |rowData index|}}
{{yield
(hash row=(component "ember-yeti-table@yeti-table/tbody/row" theme=@theme onClick=this.onRowClick columns=@columns))
(hash row=(component "ember-yeti-table@yeti-table/tbody/row" theme=@theme onClick=@onRowClick columns=@columns))
rowData
index}}
{{/each}}
Expand All @@ -13,7 +13,7 @@
{{#each @data as |rowData|}}
<EmberYetiTable@YetiTable::Tbody::Row
@theme={{@theme}}
@onClick={{if this.onRowClick (fn this.handleRowClick rowData)}} @columns={{@columns}} as |row|>
@onClick={{if @onRowClick (fn this.handleRowClick rowData)}} @columns={{@columns}} as |row|>

{{#each @columns as |column|}}
<row.cell @class={{column.columnClass}}>
Expand Down
17 changes: 5 additions & 12 deletions addon/components/yeti-table/body.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { tagName } from '@ember-decorators/component';
import Component from '@ember/component';
import { action } from '@ember/object';

import Component from '@glimmer/component';

/**
Renders a `<tbody>` element and yields the row component, row data and index.
```hbs
Expand Down Expand Up @@ -31,24 +31,17 @@ import { action } from '@ember/object';
@yield {Object} rowData - one item in the data array
@yield {number} index
*/
@tagName('')
class Body extends Component {
theme;

data;

columns;

parent;
/**
* Adds a click action to each row, called with the clicked row's data as an argument.
* Can be used with both the blockless and block invocations.
*
* @argument {function} onRowClick
*/
onRowClick;

@action
handleRowClick(rowData) {
this.onRowClick?.(rowData);
this.args.onRowClick?.(rowData);
}
}

Expand Down
2 changes: 1 addition & 1 deletion addon/components/yeti-table/tbody.hbs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<tbody class={{@theme.tbody}} ...attributes>
{{yield
(hash row=(component "ember-yeti-table@yeti-table/tbody/row" theme=@theme onClick=this.onRowClick columns=@columns))
(hash row=(component "ember-yeti-table@yeti-table/tbody/row" theme=@theme onClick=@onRowClick columns=@columns))
@data
}}
</tbody>
16 changes: 4 additions & 12 deletions addon/components/yeti-table/tbody.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { tagName } from '@ember-decorators/component';
import Component from '@ember/component';
import Component from '@glimmer/component';

/**
Renders a `<tbody>` element and yields the row component and data. You must iterate each row
Expand Down Expand Up @@ -28,21 +27,14 @@ import Component from '@ember/component';
@yield {Component} body.row - the row component
@yield {Array} data
*/
@tagName('')
// eslint-disable-next-line ember/no-empty-glimmer-component-classes
class TBody extends Component {
theme;

data;

columns;

parent;

/**
* Adds a click action to each row, called with the clicked row's data as an argument.
* Can be used with both the blockless and block invocations.
*
* @argument {fucntion} onRowClick
*/
onRowClick;
}

export default TBody;
43 changes: 17 additions & 26 deletions addon/components/yeti-table/tbody/row.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { tagName } from '@ember-decorators/component';
import { A } from '@ember/array';
import Component from '@ember/component';
import { action } from '@ember/object';

import Component from '@glimmer/component';

/**
Renders a `<tr>` element and yields the cell component.
```hbs
Expand All @@ -18,7 +17,7 @@ import { action } from '@ember/object';
</row.cell>
</body.row>
```
Remember you can cutomize each `<tr>` class or `@onClick` handler based on the row data
Remember you can customize each `<tr>` class or `@onClick` handler based on the row data
because you have access to it from the body component.

```hbs
Expand All @@ -40,44 +39,36 @@ import { action } from '@ember/object';
@yield {object} row
@yield {Component} row.cell - the cell component
*/
@tagName('')
class TBodyRow extends Component {
theme;

columns;

/**
* Adds a click action to the row.
*
* @argument {function} onClick
*/
onClick;

cells = A();
cells = [];

registerCell(cell) {
let columns = this.get('columns');
let prop = cell.get('prop');
let columns = this.args.columns;
let index = this.cells.length;

if (prop) {
let column = columns.findBy('prop', prop);
cell.set('column', column);
} else {
let index = this.get('cells.length');
let column = columns[index];
cell.set('column', column);
}
let column = columns[index];

this.get('cells').addObject(cell);
this.cells.push(cell);

return column;
}

unregisterCell(cell) {
this.get('cells').removeObject(cell);
let cells = this.cells;
let index = cells.indexOf(cell);

cells.splice(index, 1);
}

@action
handleClick() {
if (this.get('onClick')) {
this.get('onClick')(...arguments);
}
this.args.onClick?.(...arguments);
}
}

Expand Down
2 changes: 1 addition & 1 deletion addon/components/yeti-table/tbody/row/cell.hbs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{#if this.visible}}
{{#if this.column.visible}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it seems like a change here, as the cell itself can't have its own visible flag anymore.
This is probably fine (I don't see why a column would be visible, but some of the cells wouldn't), but that's a breaking change to me.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically the visible flag is passed from the column, although the user could override it, it leads to problematic HTML, which is why I didnt write it in such a way as to provide the ability to override it. I dont feel it should be documented as a user defined field for just this reason. Just because it was available, doesnt mean you should :)

I believe this is going to be a major bump anyway.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

100% agree :)

<td class="{{@class}} {{this.column.columnClass}} {{@theme.tbodyCell}}" ...attributes>
{{yield}}
</td>
Expand Down
34 changes: 9 additions & 25 deletions addon/components/yeti-table/tbody/row/cell.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { tagName } from '@ember-decorators/component';
import Component from '@ember/component';
import { reads } from '@ember/object/computed';
import Component from '@glimmer/component';

/**
Renders a `<td>` element (if its corresponding column definition has `@visible={{true}}`).
Expand All @@ -10,32 +8,18 @@ import { reads } from '@ember/object/computed';
</row.cell>
```
*/
@tagName('')
class TBodyCell extends Component {
theme;
// Assigned when the cell is registered
column = undefined;

parent;

/**
* Controls the visibility of the current cell. Keep in mind that this property
* won't just hide the column using css. The DOM for the column will be removed.
* Defaults to the `visible` argument of the corresponding column.
*/
@reads('column.visible')
visible;

init() {
super.init(...arguments);
if (this.get('parent')) {
this.get('parent').registerCell(this);
}
constructor() {
super(...arguments);
this.column = this.args.parent?.registerCell(this);
}

willDestroyElement() {
super.willDestroyElement(...arguments);
if (this.get('parent')) {
this.get('parent').unregisterCell(this);
}
willDestroy() {
super.willDestroy(...arguments);
this.args.parent?.unregisterCell(this);
}
}

Expand Down