-
Notifications
You must be signed in to change notification settings - Fork 273
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
Changes from 6 commits
8317022
58e4732
a00477c
6b708f9
7efe5d3
cacb912
ac33ee8
6affe4f
1a3f260
125cc58
d9b5827
60063a3
0586c75
263f526
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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> | ||
{{/each}} | ||
</div> |
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: { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I found one issue:
|
||
|
||
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; |
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); | ||
} |
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> |
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"); | ||
|
||
}); | ||
}); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.