-
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8317022
feat(ui5-segmentedbutton): initial implementation
ivoplashkov 58e4732
test page examples updated
ivoplashkov a00477c
togglebutton can no longer be deactivated if once pressed
ivoplashkov 6b708f9
fixed styles and resizing in IE
ivoplashkov 7efe5d3
fixed eslint errors
ivoplashkov cacb912
added tests
ivoplashkov ac33ee8
fixed behaviour and added docs
ivoplashkov 6affe4f
fixed lint errors
ivoplashkov 1a3f260
Merge branch 'master' into implement-segmented-button
ivoplashkov 125cc58
removed individualSlot
ivoplashkov d9b5827
Merge branch 'implement-segmented-button' of https://github.com/ivopl…
ivoplashkov 60063a3
add: keyboard handling and sample page.
ivoplashkov 0586c75
fixed resize handling
ivoplashkov 263f526
fixed identation
ivoplashkov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
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 selected button changes. | ||
* | ||
* @event | ||
* @param {HTMLElement} selectedButton the pressed button. | ||
* @public | ||
*/ | ||
selectionChange: { | ||
detail: { | ||
selectedButton: { 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.normalizeSelection(); | ||
} | ||
|
||
async onAfterRendering() { | ||
await Promise.all(this.buttons.map(button => button._waitForDomRef)); | ||
this.widths = this.buttons.map(button => button.offsetWidth); | ||
} | ||
|
||
normalizeSelection() { | ||
this._selectedButton = this.buttons.filter(button => button.pressed).pop(); | ||
|
||
if (this._selectedButton) { | ||
this.buttons.forEach(button => { | ||
button.pressed = false; | ||
}); | ||
this._selectedButton.pressed = true; | ||
} | ||
} | ||
|
||
_onclick(event) { | ||
if (event.target !== this._selectedButton) { | ||
if (this._selectedButton) { | ||
this._selectedButton.pressed = false; | ||
} | ||
this._selectedButton = event.target; | ||
this.fireEvent("selectionChange", { | ||
selectedButton: this._selectedButton, | ||
}); | ||
} | ||
this._selectedButton.pressed = true; | ||
|
||
return this; | ||
} | ||
|
||
_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:
|
||
|
||
if (!this.style.width) { | ||
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%"; | ||
} | ||
} | ||
|
||
/** | ||
* Currently selected button. | ||
* | ||
* @readonly | ||
* @type { ui5-togglebutton } | ||
* @public | ||
*/ | ||
get selectedButton() { | ||
return this._selectedButton; | ||
} | ||
} | ||
|
||
SegmentedButton.define(); | ||
|
||
export default SegmentedButton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 style="width: 500px;"> | ||
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
|
||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.