|
| 1 | +import UI5Element from "@ui5/webcomponents-base/dist/UI5Element.js"; |
| 2 | +import ItemNavigation from "@ui5/webcomponents-base/dist/delegate/ItemNavigation.js"; |
| 3 | +import litRender from "@ui5/webcomponents-base/dist/renderer/LitRenderer.js"; |
| 4 | +import ResizeHandler from "@ui5/webcomponents-base/dist/delegate/ResizeHandler.js"; |
| 5 | + |
| 6 | +// Template |
| 7 | +import SegmentedButtonTemplate from "./generated/templates/SegmentedButtonTemplate.lit.js"; |
| 8 | + |
| 9 | +// Styles |
| 10 | +import SegmentedButtonCss from "./generated/themes/SegmentedButton.css.js"; |
| 11 | + |
| 12 | +/** |
| 13 | + * @public |
| 14 | + */ |
| 15 | +const metadata = { |
| 16 | + tag: "ui5-segmentedbutton", |
| 17 | + properties: /** @lends sap.ui.webcomponents.main.SegmentedButton.prototype */ {}, |
| 18 | + slots: /** @lends sap.ui.webcomponents.main.SegmentedButton.prototype */ { |
| 19 | + |
| 20 | + /** |
| 21 | + * Defines the buttons of <code>ui5-segmentedbutton</code>. |
| 22 | + * <br><br> |
| 23 | + * <b>Note:</b> Multiple buttons are allowed. |
| 24 | + * <br><br> |
| 25 | + * <b>Note:</b> Use the <code>ui5-togglebutton</code> for the intended design. |
| 26 | + * @type {HTMLElement[]} |
| 27 | + * @slot |
| 28 | + * @public |
| 29 | + */ |
| 30 | + "default": { |
| 31 | + propertyName: "buttons", |
| 32 | + type: HTMLElement, |
| 33 | + }, |
| 34 | + }, |
| 35 | + events: /** @lends sap.ui.webcomponents.main.SegmentedButton.prototype */ { |
| 36 | + |
| 37 | + /** |
| 38 | + * Fired when the selected button changes. |
| 39 | + * |
| 40 | + * @event |
| 41 | + * @param {HTMLElement} selectedButton the pressed button. |
| 42 | + * @public |
| 43 | + */ |
| 44 | + selectionChange: { |
| 45 | + detail: { |
| 46 | + selectedButton: { type: HTMLElement }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + }, |
| 50 | +}; |
| 51 | + |
| 52 | +/** |
| 53 | + * @class |
| 54 | + * |
| 55 | + * <h3 class="comment-api-title">Overview</h3> |
| 56 | + * |
| 57 | + * The <code>SegmentedButton</code> shows a group of buttons. When the user clicks or taps |
| 58 | + * one of the buttons, it stays in a pressed state. It automatically resizes the buttons |
| 59 | + * to fit proportionally within the component. When no width is set, the component uses the available width. |
| 60 | + * <br><br> |
| 61 | + * <b>Note:</b> There can be just one selected <code>button</code> at a time. |
| 62 | + * |
| 63 | + * <h3>ES6 Module Import</h3> |
| 64 | + * |
| 65 | + * <code>import "@ui5/webcomponents/dist/SegmentedButton";</code> |
| 66 | + * |
| 67 | + * @constructor |
| 68 | + * @author SAP SE |
| 69 | + * @alias sap.ui.webcomponents.main.SegmentedButton |
| 70 | + * @extends sap.ui.webcomponents.base.UI5Element |
| 71 | + * @tagname ui5-segmentedbutton |
| 72 | + * @since 1.0.0-rc.6 |
| 73 | + * @public |
| 74 | + */ |
| 75 | +class SegmentedButton extends UI5Element { |
| 76 | + static get metadata() { |
| 77 | + return metadata; |
| 78 | + } |
| 79 | + |
| 80 | + static get render() { |
| 81 | + return litRender; |
| 82 | + } |
| 83 | + |
| 84 | + static get template() { |
| 85 | + return SegmentedButtonTemplate; |
| 86 | + } |
| 87 | + |
| 88 | + static get styles() { |
| 89 | + return SegmentedButtonCss; |
| 90 | + } |
| 91 | + |
| 92 | + constructor() { |
| 93 | + super(); |
| 94 | + this.initItemNavigation(); |
| 95 | + |
| 96 | + this.absoluteWidthSet = false; // set to true whenever we set absolute width to the component |
| 97 | + this.percentageWidthSet = false; // set to true whenever we set 100% width to the component |
| 98 | + } |
| 99 | + |
| 100 | + onEnterDOM() { |
| 101 | + this._handleResizeBound = this._handleResize.bind(this); |
| 102 | + |
| 103 | + ResizeHandler.register(document.body, this._handleResizeBound); |
| 104 | + } |
| 105 | + |
| 106 | + onExitDOM() { |
| 107 | + ResizeHandler.deregister(document.body, this._handleResizeBound); |
| 108 | + } |
| 109 | + |
| 110 | + onBeforeRendering() { |
| 111 | + this.normalizeSelection(); |
| 112 | + } |
| 113 | + |
| 114 | + async onAfterRendering() { |
| 115 | + await Promise.all(this.buttons.map(button => button._waitForDomRef)); |
| 116 | + this.widths = this.buttons.map(button => button.offsetWidth); |
| 117 | + } |
| 118 | + |
| 119 | + initItemNavigation() { |
| 120 | + this._itemNavigation = new ItemNavigation(this); |
| 121 | + |
| 122 | + this._itemNavigation.getItemsCallback = () => this.getSlottedNodes("buttons"); |
| 123 | + } |
| 124 | + |
| 125 | + normalizeSelection() { |
| 126 | + this._selectedButton = this.buttons.filter(button => button.pressed).pop(); |
| 127 | + |
| 128 | + if (this._selectedButton) { |
| 129 | + this.buttons.forEach(button => { |
| 130 | + button.pressed = false; |
| 131 | + }); |
| 132 | + this._selectedButton.pressed = true; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + _onclick(event) { |
| 137 | + if (event.target !== this._selectedButton) { |
| 138 | + if (this._selectedButton) { |
| 139 | + this._selectedButton.pressed = false; |
| 140 | + } |
| 141 | + this._selectedButton = event.target; |
| 142 | + this.fireEvent("selectionChange", { |
| 143 | + selectedButton: this._selectedButton, |
| 144 | + }); |
| 145 | + } |
| 146 | + this._selectedButton.pressed = true; |
| 147 | + |
| 148 | + this._itemNavigation.update(this._selectedButton); |
| 149 | + |
| 150 | + return this; |
| 151 | + } |
| 152 | + |
| 153 | + _handleResize() { |
| 154 | + const parentWidth = this.parentNode.offsetWidth; |
| 155 | + |
| 156 | + if (!this.style.width || this.percentageWidthSet) { |
| 157 | + this.style.width = `${Math.max(...this.widths) * this.buttons.length}px`; |
| 158 | + this.absoluteWidthSet = true; |
| 159 | + } |
| 160 | + |
| 161 | + this.buttons.forEach(button => { |
| 162 | + button.style.width = "100%"; |
| 163 | + }); |
| 164 | + |
| 165 | + if (parentWidth <= this.offsetWidth && this.absoluteWidthSet) { |
| 166 | + this.style.width = "100%"; |
| 167 | + this.percentageWidthSet = true; |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Currently selected button. |
| 173 | + * |
| 174 | + * @readonly |
| 175 | + * @type { ui5-togglebutton } |
| 176 | + * @public |
| 177 | + */ |
| 178 | + get selectedButton() { |
| 179 | + return this._selectedButton; |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +SegmentedButton.define(); |
| 184 | + |
| 185 | +export default SegmentedButton; |
0 commit comments