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-segmentedbutton): initial implementation #1164

Merged
merged 14 commits into from
Feb 10, 2020
Merged
1 change: 1 addition & 0 deletions packages/main/bundle.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import Link from "./dist/Link.js";
import Popover from "./dist/Popover.js";
import Panel from "./dist/Panel.js";
import RadioButton from "./dist/RadioButton.js";
import SegmentedButton from "./dist/SegmentedButton.js";
import Select from "./dist/Select.js";
import Option from "./dist/Option.js";
import Switch from "./dist/Switch.js";
Expand Down
8 changes: 8 additions & 0 deletions packages/main/src/SegmentedButton.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div
@click="{{_onclick}}"
class="ui5-segmentedbutton-root"
>
{{#each buttons}}
<slot name="{{this._individualSlot}}"></slot>
Copy link
Member

Choose a reason for hiding this comment

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

I am wondering if we need the individual slot assignment as we do not wrap the buttons in spans or anything else?

<div
	@click="{{_onclick}}"
	class="ui5-segmentedbutton-root"
>
    <slot></slot>
</div>

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

{{/each}}
</div>
158 changes: 158 additions & 0 deletions packages/main/src/SegmentedButton.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
import UI5Element from "@ui5/webcomponents-base/dist/UI5Element.js";
ilhan007 marked this conversation as resolved.
Show resolved Hide resolved
import litRender from "@ui5/webcomponents-base/dist/renderer/LitRenderer.js";
import ResizeHandler from "@ui5/webcomponents-base/dist/delegate/ResizeHandler.js";

// Template
import SegmentedButtonTemplate from "./generated/templates/SegmentedButtonTemplate.lit.js";

// Styles
import SegmentedButtonCss from "./generated/themes/SegmentedButton.css.js";

/**
* @public
*/
const metadata = {
tag: "ui5-segmentedbutton",
properties: /** @lends sap.ui.webcomponents.main.SegmentedButton.prototype */ {},
slots: /** @lends sap.ui.webcomponents.main.SegmentedButton.prototype */ {

/**
* Defines the buttons of <code>ui5-segmentedbutton</code>.
* <br><br>
* <b>Note:</b> Multiple buttons are allowed.
* <br><br>
* <b>Note:</b> Use the <code>ui5-togglebutton</code> for the intended design.
* @type {HTMLElement[]}
* @slot
* @public
*/
"default": {
propertyName: "buttons",
type: HTMLElement,
individualSlots: true,
},
},
events: /** @lends sap.ui.webcomponents.main.SegmentedButton.prototype */ {

/**
* Fired when the pressed button changes.
*
* @event
* @param {HTMLElement} pressedButton the pressed button.
* @public
*/
pressChange: {
Copy link
Contributor

Choose a reason for hiding this comment

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

Was this name agreed upon? Press change sounds ok for the toggle button, but for segmented button it's more like "selectionChange". How is it in OpenUI5?

detail: {
pressedButton: { type: HTMLElement },
},
},
},
};

/**
* @class
*
* <h3 class="comment-api-title">Overview</h3>
*
* The <code>SegmentedButton</code> shows a group of buttons. When the user clicks or taps
ilhan007 marked this conversation as resolved.
Show resolved Hide resolved
* one of the buttons, it stays in a pressed state. It automatically resizes the buttons
* to fit proportionally within the control. When no width is set, the control uses the available width.
Copy link
Member

Choose a reason for hiding this comment

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

use "component", instead of "control"

*
* <h3>ES6 Module Import</h3>
*
* <code>import "@ui5/webcomponents/dist/SegmentedButton";</code>
*
* @constructor
* @author SAP SE
* @alias sap.ui.webcomponents.main.SegmentedButton
* @extends sap.ui.webcomponents.base.UI5Element
* @tagname ui5-segmentedbutton
* @public
*/
class SegmentedButton extends UI5Element {
static get metadata() {
return metadata;
}

static get render() {
return litRender;
}

static get template() {
return SegmentedButtonTemplate;
}

static get styles() {
return SegmentedButtonCss;
}

onEnterDOM() {
this._handleResizeBound = this._handleResize.bind(this);

ResizeHandler.register(document.body, this._handleResizeBound);
}

onExitDOM() {
ResizeHandler.deregister(document.body, this._handleResizeBound);
}

onBeforeRendering() {
this.syncSelection();
}

async onAfterRendering() {
await Promise.all(this.buttons.map(button => button._waitForDomRef));
this.widths = this.buttons.map(button => button.offsetWidth);
}

syncSelection() {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not really synchronization. It's deselecting, if there are many selected. Think of a better name.

this._pressedButton = this.buttons.filter(button => button.pressed).pop();

if (this._pressedButton) {
this.buttons.forEach(button => {
button.pressed = false;
});
this._pressedButton.pressed = true;
Copy link
Contributor

Choose a reason for hiding this comment

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

What does this line do? If we set pressed to false to all buttons in the loop, isn't this unnecessary?

Copy link
Member Author

Choose a reason for hiding this comment

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

We need this in order to leave the last pressed button, after we deselect all buttons.

}
}

_onclick(event) {
return this.toggle(event);
}

toggle(event) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This function seems to be only called from onclick. Just move the code there

if (event.target !== this.pressedButton) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is confusing a bit. It's the same as ._pressedButton. Do we need "pressedButton"? Is this the public API? Even if so, just check for ._pressedButton here

if (this._pressedButton) {
this._pressedButton.pressed = false;
}
this._pressedButton = event.target;
this.fireEvent("pressChange", {
pressedButton: this._pressedButton,
});
}
event.target.pressed = true;
Copy link
Contributor

Choose a reason for hiding this comment

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

Better use ._pressedButton here.


return this;
}

get pressedButton() {
return this._pressedButton;
}

_handleResize() {
const documentWidth = document.body.clientWidth;
Copy link
Member

Choose a reason for hiding this comment

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

I found one issue:

  • First, open the test page in large size
  • Then, shrink the page to the point the component gets width: 100%;
  • Then, expand the page back - the segmented button remains with width: 100%,
    but should not.


this.style.width = `${Math.max(...this.widths) * this.buttons.length}px`;
this.buttons.forEach(button => {
button.style.width = "100%";
});

if (documentWidth <= this.offsetWidth) {
this.style.width = "100%";
}
}
}

SegmentedButton.define();

export default SegmentedButton;
13 changes: 10 additions & 3 deletions packages/main/src/themes/Button.css
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
color: inherit;
text-shadow: inherit;
font: inherit;
white-space: inherit;
overflow: inherit;
text-overflow: inherit;
}

:host(:not([active]):hover) {
Expand Down Expand Up @@ -83,6 +86,9 @@
.ui5-button-text {
outline: none;
position: relative;
white-space: inherit;
overflow: inherit;
text-overflow: inherit;
}

:host([has-icon]) .ui5-button-text {
Expand All @@ -104,9 +110,10 @@
}

bdi {
display: flex;
justify-content: flex-start;
align-items: center;
display: block;
white-space: inherit;
overflow: inherit;
text-overflow: inherit;
}

:host([active]:not([disabled])) {
Expand Down
34 changes: 34 additions & 0 deletions packages/main/src/themes/SegmentedButton.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
:host(:not([hidden])) {
display: inline-block;
}

.ui5-segmentedbutton-root {
display: flex;
}

::slotted(ui5-togglebutton) {
border-radius: 0;
height: 2.75rem;
min-width: 2.5rem;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}

::slotted(ui5-togglebutton:nth-child(odd)) {
border: 1px solid var(--sapButton_Selected_BorderColor);
border-right: 0;
border-left: 0;
}

::slotted(ui5-togglebutton:last-child) {
border-top-right-radius: 0.375rem;
border-bottom-right-radius: 0.375rem;
border-right: 1px solid var(--sapButton_Selected_BorderColor);
}

::slotted(ui5-togglebutton:first-child) {
border-top-left-radius: 0.375rem;
border-bottom-left-radius: 0.375rem;
border-left: 1px solid var(--sapButton_Selected_BorderColor);
}
71 changes: 71 additions & 0 deletions packages/main/test/pages/SegmentedButton.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<!DOCTYPE html>
ilhan007 marked this conversation as resolved.
Show resolved Hide resolved
<html>

<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta charset="utf-8">

<title>ui5-segmentedbutton</title>

<script src="../../webcomponentsjs/webcomponents-loader.js"></script>
<script src="../../resources/bundle.esm.js" type="module"></script>
<script nomodule src="../../resources/bundle.es5.js"></script>

<script>delete Document.prototype.adoptedStyleSheets;</script>

</head>

<body>
<header class="header">
<h1 class="header-title">ui5-segmentedbutton</h1>
</header>

<section class="main">
<div class="samples">
<h2>Segmentedbutton example</h2>
<div class="sample" id="main-sample">
<section>
<ui5-segmentedbutton id="segButton1">
<ui5-togglebutton icon="employee" pressed></ui5-togglebutton>
<ui5-togglebutton>ToggleButton</ui5-togglebutton>
<ui5-togglebutton>Button</ui5-togglebutton>
</ui5-segmentedbutton>
</section>

<section>
<h1>Example with 4 buttons</h1>

<ui5-segmentedbutton>
<ui5-togglebutton>Button</ui5-togglebutton>
<ui5-togglebutton>Button</ui5-togglebutton>
<ui5-togglebutton>Click</ui5-togglebutton>
<ui5-togglebutton pressed>Pressed ToggleButton</ui5-togglebutton>
</ui5-segmentedbutton>
</section>

<section>
<h1>Example with 5 buttons</h1>

<ui5-segmentedbutton id="segButton2">
<ui5-togglebutton pressed>Word</ui5-togglebutton>
<ui5-togglebutton pressed>Pressed ToggleButton With Bigger Text</ui5-togglebutton>
<ui5-togglebutton pressed>Button</ui5-togglebutton>
<ui5-togglebutton pressed>Pressed ToggleButton</ui5-togglebutton>
<ui5-togglebutton pressed>A</ui5-togglebutton>
</ui5-segmentedbutton>
</section>

<section>
<h1>Example with Icons</h1>

<ui5-segmentedbutton>
<ui5-togglebutton icon="employee" pressed></ui5-togglebutton>
<ui5-togglebutton icon="menu"></ui5-togglebutton>
<ui5-togglebutton icon="factory"></ui5-togglebutton>
</ui5-segmentedbutton>
</section>
</div>
</div>
</section>
</body>
</html>
48 changes: 48 additions & 0 deletions packages/main/test/specs/SegmentedButton.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const assert = require("chai").assert;

describe("SegmentedButton general interaction", () => {
browser.url("http://localhost:8080/test-resources/pages/SegmentedButton.html");

it("tests rendering of segmentedbutton", () => {
const segmentedButton = browser.$("#segButton1");
const toggleButton1 = browser.$("#segButton1 > ui5-togglebutton:first-child");
const toggleButton2 = browser.$("#segButton1 > ui5-togglebutton:nth-child(2)");
const toggleButton3 = browser.$("#segButton1 > ui5-togglebutton:last-child");

// all buttons should be rendered
assert.ok(segmentedButton.isExisting(), "SegmentedButton is rendered");
assert.ok(toggleButton1.isExisting(), "First ToggleButton is rendered");
assert.ok(toggleButton2.isExisting(), "Second ToggleButton is rendered");
assert.ok(toggleButton3.isExisting(), "Third ToggleButton is rendered");
});

it("tests if pressed attribute is applied", () => {
const toggleButton = browser.$("#segButton1 > ui5-togglebutton:first-child");

assert.ok(toggleButton.getProperty("pressed"), "ToggleButton has property pressed");
});

it("tests if pressed attribute is switched to the newly pressed button", () => {
const firstToggleButton = browser.$("#segButton1 > ui5-togglebutton:first-child");
const lastToggleButton = browser.$("#segButton1 > ui5-togglebutton:last-child");

lastToggleButton.click();

assert.ok(lastToggleButton.getProperty("pressed"), "Last ToggleButton has property pressed");
assert.ok(!firstToggleButton.getProperty("pressed"), "First ToggleButton should not be pressed anymore");
});

it("tests if pressed attribute is applied only to last child when all buttons are pressed", () => {
const toggleButton1 = browser.$("#segButton2 > ui5-togglebutton:first-child");
const toggleButton2 = browser.$("#segButton2 > ui5-togglebutton:nth-child(2)");
const toggleButton3 = browser.$("#segButton2 > ui5-togglebutton:nth-child(3)");
const toggleButton4 = browser.$("#segButton2 > ui5-togglebutton:last-child");

// only last button should be pressed
assert.ok(!toggleButton1.getProperty("pressed"), "ToggleButton should not be pressed");
assert.ok(!toggleButton2.getProperty("pressed"), "ToggleButton should not be pressed");
assert.ok(!toggleButton3.getProperty("pressed"), "ToggleButton should not be pressed");
assert.ok(toggleButton4.getProperty("pressed"), "ToggleButton has property pressed");

});
});