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

feat(ui5-table): introduce Single and Multi selection modes #2848

Merged
merged 21 commits into from
Apr 5, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
47 changes: 47 additions & 0 deletions packages/base/src/types/TableMode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import DataType from "./DataType.js";

/**
* @lends sap.ui.webcomponents.main.types.TableMode.prototype
* @public
*/
const TableModes = {
/**
* Default mode (no selection).
* @public
* @type {None}
*/
None: "None",

/**
* Single selection mode (only one table row can be selected).
* @public
* @type {SingleSelect}
*/
SingleSelect: "SingleSelect",

/**
* Multi selection mode (more than one table row can be selected).
* @public
* @type {MultiSelect}
*/
MultiSelect: "MultiSelect",
};

/**
* @class
* Defines the type of <code>ui5-table</code>.
* @constructor
* @author SAP SE
* @alias sap.ui.webcomponents.main.types.TableMode
* @public
* @enum {string}
*/
class TableMode extends DataType {
static isValid(value) {
return !!TableModes[value];
}
}

TableMode.generateTypeAccessors(TableModes);

export default TableMode;
9 changes: 9 additions & 0 deletions packages/main/src/Table.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
<table border="0" cellspacing="0" cellpadding="0" @keydown="{{_onkeydown}}" role="table">
<thead>
<tr id="{{_columnHeader.id}}" role="row" class="ui5-table-header-row" tabindex="{{_columnHeader._tabIndex}}" style="height: 48px" @click="{{_onColumnHeaderClick}}">
{{#if isMultiSelect}}
<th class="ui5-table-select-all-column">
<ui5-checkbox
@change="{{_selectAll}}"
ilhan007 marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

@ilhan007 ilhan007 Mar 31, 2021

Choose a reason for hiding this comment

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

change -> ui5-change (non-conflict event names are recommended)

>
</ui5-checkbox>
</th>
{{/if}}

{{#each visibleColumns}}
<slot name="{{this._individualSlot}}"></slot>
{{/each}}
Expand Down
100 changes: 99 additions & 1 deletion packages/main/src/Table.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { fetchI18nBundle, getI18nBundle } from "@ui5/webcomponents-base/dist/i18
import debounce from "@ui5/webcomponents-base/dist/util/debounce.js";
import TableGrowingMode from "./types/TableGrowingMode.js";
import BusyIndicator from "./BusyIndicator.js";
import TableMode from "@ui5/webcomponents-base/dist/types/TableMode.js";

// Texts
import { TABLE_LOAD_MORE_TEXT } from "./generated/i18n/i18n-defaults.js";
Expand Down Expand Up @@ -190,6 +191,17 @@ const metadata = {
type: Boolean,
},

/**
ilhan007 marked this conversation as resolved.
Show resolved Hide resolved
* Defines the mode of the component (None, SingleSelect, MultiSelect).
Copy link
Member

@ilhan007 ilhan007 Mar 31, 2021

Choose a reason for hiding this comment

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

We will revisit all the API of all the components but this can be improved on the spot, let's use the bullets representation of enum options

        * Defines the mode of the <code>ui5-table</code>.
        * <br><br>
   	 * Available options are:
   	 * <ul>
   	 * <li><code>MultiSelect</code></li>
   	 * <li><code>SingleSelect</code></li>
   	 * <li><code>None</code></li>
   	 * <ul>

* @type {TableMode}
* @defaultvalue "None"
* @public
*/
mode: {
type: TableMode,
defaultValue: TableMode.None,
},

_hiddenColumns: {
type: Object,
multiple: true,
Expand Down Expand Up @@ -261,6 +273,22 @@ const metadata = {
* @since 1.0.0-rc.11
*/
"load-more": {},

/**
* Fired when selection is changed by user interaction
* in <code>SingleSelect</code> and <code>MultiSelect</code> modes.
ilhan007 marked this conversation as resolved.
Show resolved Hide resolved
*
* @event sap.ui.webcomponents.main.Table#selection-change
* @param {Array} selectedRows An array of the selected rows.
* @param {Array} previouslySelectedRows An array of the previously selected rows.
* @public
*/
"selection-change": {
detail: {
selectedRows: { type: Array },
previouslySelectedRows: { type: Array },
},
},
},
};

Expand Down Expand Up @@ -344,6 +372,8 @@ class Table extends UI5Element {
this.i18nBundle = getI18nBundle("@ui5/webcomponents");

this.tableEndObserved = false;
this.addEventListener("selection-requested", this._handleMultiSelect.bind(this));
this.addEventListener("row-click", this._handleSingleSelect.bind(this));
}

onBeforeRendering() {
Expand All @@ -359,6 +389,7 @@ class Table extends UI5Element {
row._busy = this.busy;
row.removeEventListener("ui5-_focused", this.fnOnRowFocused);
row.addEventListener("ui5-_focused", this.fnOnRowFocused);
row.mode = this.mode;
});

this.visibleColumns = this.columns.filter((column, index) => {
Expand Down Expand Up @@ -445,6 +476,67 @@ class Table extends UI5Element {
this.fireEvent("load-more");
}

_handleSingleSelect(event) {
const row = this.getRowParent(event.target);
if (this.mode === "SingleSelect" && !row.selected) {
const previouslySelectedRows = this.rows.filter(item => item.selected);
this.rows.forEach(item => {
if (item.selected) {
item.selected = false;
}
});
row.selected = true;
this.fireEvent("selection-change", {
selectedRows: [row],
previouslySelectedRows,
});
}
}

_handleMultiSelect(event) {
const row = this.getRowParent(event.target);
const previouslySelectedRows = this.rows.filter(item => item.selected);

row.selected = !row.selected;

const selectedRows = this.rows.filter(item => item.selected);
Copy link
Member

Choose a reason for hiding this comment

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

this.rows.filter(item => item.selected) is something you need in several places, add a getter for that:

get selectedRows() {
    return this.rows.filter(item => item.selected)
}


this.fireEvent("selection-change", {
selectedRows,
previouslySelectedRows,
});
}

_selectAll(event) {
const bAllSelected = event.target.checked;
const previouslySelectedRows = this.rows.filter(row => row.selected);

this.rows.forEach(row => {
row.selected = bAllSelected;
});

const selectedRows = bAllSelected ? this.rows : [];

this.fireEvent("selection-change", {
selectedRows,
previouslySelectedRows,
});
}

getRowParent(child) {
const parent = child.parentElement;

if (child.hasAttribute("ui5-table-row")) {
return child;
}

if (parent && parent.hasAttribute("ui5-table-row")) {
return parent;
}

this.getRowParent(parent);
}

getColumnHeader() {
return this.getDomRef() && this.getDomRef().querySelector(`#${this._id}-columnHeader`);
}
Expand Down Expand Up @@ -478,7 +570,9 @@ class Table extends UI5Element {
});

if (visibleColumnsIndexes.length) {
this.columns[visibleColumnsIndexes[0]].first = true;
if (!this.isMultiSelect) {
this.columns[visibleColumnsIndexes[0]].first = true;
}
this.columns[visibleColumnsIndexes[visibleColumnsIndexes.length - 1]].last = true;
}

Expand Down Expand Up @@ -578,6 +672,10 @@ class Table extends UI5Element {
&& rect.right <= (window.innerWidth || document.documentElement.clientWidth)
);
}

get isMultiSelect() {
return this.mode === "MultiSelect";
}
}

Table.define();
Expand Down
8 changes: 0 additions & 8 deletions packages/main/src/TableCell.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@ const metadata = {
},
},
properties: /** @lends sap.ui.webcomponents.main.TableCell.prototype */ {

/**
* @private
*/
firstInRow: {
Copy link
Member

Choose a reason for hiding this comment

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

There is also related code with that, you can remove it :

     // 155:TableRow.js
	cell.firstInRow = (index === 0);

type: Boolean,
},

/**
* @private
*/
Expand Down
11 changes: 11 additions & 0 deletions packages/main/src/TableRow.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@
part="row"
role="row"
>

{{#if isMultiSelect}}
<td class="ui5-table-multi-select-cell">
<ui5-checkbox
@change="{{_handleMultiSelection}}"
?checked="{{this.selected}}"
>
</ui5-checkbox>
</td>
{{/if}}

{{#if shouldPopin}}
{{#each visibleCells}}
<slot name="{{this._individualSlot}}"></slot>
Expand Down
37 changes: 36 additions & 1 deletion packages/main/src/TableRow.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import UI5Element from "@ui5/webcomponents-base/dist/UI5Element.js";
import TableMode from "@ui5/webcomponents-base/dist/types/TableMode.js";
import litRender from "@ui5/webcomponents-base/dist/renderer/LitRenderer.js";
import TableRowTemplate from "./generated/templates/TableRowTemplate.lit.js";

Expand Down Expand Up @@ -28,6 +29,26 @@ const metadata = {
},
},
properties: /** @lends sap.ui.webcomponents.main.TableRow.prototype */ {
/**
* Defines the mode of the row (None, SingleSelect, MultiSelect).
* @type {TableMode}
* @defaultvalue "None"
* @private
*/
mode: {
type: TableMode,
defaultValue: TableMode.None,
},
/**
* Defines the row's selected state.
*
* @type {boolean}
* @defaultvalue false
* @private
ilhan007 marked this conversation as resolved.
Show resolved Hide resolved
*/
selected: {
type: Boolean,
},
_columnsInfo: {
type: Object,
multiple: true,
Expand All @@ -43,6 +64,13 @@ const metadata = {
events: /** @lends sap.ui.webcomponents.main.TableRow.prototype */ {
"row-click": {},
Copy link
Member

Choose a reason for hiding this comment

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

Add JS doc when the row-click is fired after this change

_focused: {},
/**
* Fired on selection change of a row in MultiSelect mode.
*
* @event sap.ui.webcomponents.main.TableRow#selection-requested
* @private
Copy link
Member

Choose a reason for hiding this comment

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

since

*/
"selection-requested": {},
},
};

Expand Down Expand Up @@ -97,6 +125,10 @@ class TableRow extends UI5Element {
this.fireEvent("row-click", { row: this });
}

_handleMultiSelection() {
this.fireEvent("selection-requested", { row: this });
}

_getActiveElementTagName() {
return document.activeElement.localName.toLocaleLowerCase();
}
Expand Down Expand Up @@ -133,7 +165,6 @@ class TableRow extends UI5Element {

if (info.visible) {
this.visibleCells.push(cell);
cell.firstInRow = (index === 0);
cell.popined = false;
} else if (info.demandPopin) {
const popinHeaderClass = this.popinCells.length === 0 ? "popin-header" : "";
Expand Down Expand Up @@ -168,6 +199,10 @@ class TableRow extends UI5Element {
}).join(" ");
}

get isMultiSelect() {
return this.mode === "MultiSelect";
}

getCellText(cell) {
return this.getNormilzedTextContent(cell.textContent);
}
Expand Down
9 changes: 9 additions & 0 deletions packages/main/src/themes/Table.css
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,13 @@ tr {

:host([busy]) .ui5-table-load-more-row {
opacity: 0.72;
}

.ui5-table-select-all-column {
width: var(--ui5_table_multiselect_column_width);
text-align: center;
}

th {
background: var(--sapList_HeaderBackground);
}
12 changes: 6 additions & 6 deletions packages/main/src/themes/TableCell.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@
}

td {
padding: .5rem .25rem;
padding: inherit;
box-sizing: border-box;
word-break: break-word;
vertical-align: middle;
}

:host([first-in-row]) td,
.ui5-table-popin-row td {
padding-left: 1rem;

}

:host([first-in-row]) td {
ilhan007 marked this conversation as resolved.
Show resolved Hide resolved
padding-left: 1rem;
}

:host([popined]) td {
padding-left: 0;
}

::slotted(*) {
color: inherit;
}
Loading