|
| 1 | +import WebComponent from "@ui5/webcomponents-base/src/sap/ui/webcomponents/base/WebComponent"; |
| 2 | +import KeyCodes from "@ui5/webcomponents-core/dist/sap/ui/events/KeyCodes"; |
| 3 | +import Bootstrap from "@ui5/webcomponents-base/src/sap/ui/webcomponents/base/Bootstrap"; |
| 4 | + |
| 5 | +// Template |
| 6 | +import ShadowDOM from "@ui5/webcomponents-base/src/sap/ui/webcomponents/base/compatibility/ShadowDOM"; |
| 7 | +import SwitchRenderer from "./build/compiled/SwitchRenderer.lit"; |
| 8 | +import SwitchTemplateContext from "./SwitchTemplateContext"; |
| 9 | +import SwitchType from "./types/SwitchType"; |
| 10 | + |
| 11 | +// Styles |
| 12 | +import belize from "./themes/sap_belize/Switch.less"; |
| 13 | +import belizeHcb from "./themes/sap_belize_hcb/Switch.less"; |
| 14 | +import fiori3 from "./themes/sap_fiori_3/Switch.less"; |
| 15 | + |
| 16 | +ShadowDOM.registerStyle("sap_belize", "Switch.css", belize); |
| 17 | +ShadowDOM.registerStyle("sap_belize_hcb", "Switch.css", belizeHcb); |
| 18 | +ShadowDOM.registerStyle("sap_fiori_3", "Switch.css", fiori3); |
| 19 | + |
| 20 | +/** |
| 21 | + * @public |
| 22 | + */ |
| 23 | +const metadata = { |
| 24 | + tag: "ui5-switch", |
| 25 | + styleUrl: ["Switch.css"], |
| 26 | + properties: /** @lends sap.ui.webcomponents.main.Switch.prototype */ { |
| 27 | + |
| 28 | + /** |
| 29 | + * Defines if the <code>ui5-switch</code> is checked. |
| 30 | + * <br><br> |
| 31 | + * <b>Note:</b> The property can be changed with user interaction, |
| 32 | + * either by cliking/tapping on the <code>ui5-switch</code>, or by |
| 33 | + * pressing the <code>Enter</code> or <code>Space</code> key. |
| 34 | + * |
| 35 | + * @type {boolean} |
| 36 | + * @default false |
| 37 | + * @public |
| 38 | + */ |
| 39 | + checked: { |
| 40 | + type: Boolean, |
| 41 | + }, |
| 42 | + |
| 43 | + /** |
| 44 | + * Defines whether the <code>ui5-switch</code> is disabled. |
| 45 | + * <br><br> |
| 46 | + * <b>Note:</b> A disabled <code>ui5-switch</code> is noninteractive. |
| 47 | + * |
| 48 | + * @type {boolean} |
| 49 | + * @default false |
| 50 | + * @public |
| 51 | + */ |
| 52 | + disabled: { |
| 53 | + type: Boolean, |
| 54 | + }, |
| 55 | + |
| 56 | + /** |
| 57 | + * Defines the text of the <code>ui5-switch</code> when switched on. |
| 58 | + * |
| 59 | + * <br><br> |
| 60 | + * <b>Note:</b> We recommend using short texts (up to 3 letters, because larger texts might be cut off. |
| 61 | + * @type {string} |
| 62 | + * @public |
| 63 | + */ |
| 64 | + textOn: { |
| 65 | + type: String, |
| 66 | + defaultValue: "", |
| 67 | + }, |
| 68 | + |
| 69 | + /** |
| 70 | + * Defines the text of the <code>ui5-switch</code> when switched off. |
| 71 | + * |
| 72 | + * <br><br> |
| 73 | + * <b>Note:</b> We recommend using short texts (up to 3 letters, because larger texts might be cut off. |
| 74 | + * @type {string} |
| 75 | + * @public |
| 76 | + */ |
| 77 | + textOff: { |
| 78 | + type: String, |
| 79 | + defaultValue: "", |
| 80 | + }, |
| 81 | + |
| 82 | + /** |
| 83 | + * Defines the <code>ui5-switch</code> type. |
| 84 | + * <br> |
| 85 | + * Available options are <code>Textual</code> and <code>Graphical</code>. |
| 86 | + * |
| 87 | + * <br><br> |
| 88 | + * <b>Note:</b> If <code>Graphical</code> type is set, |
| 89 | + * positive and negative icons will replace the <code>textOn</code> and <code>textOff</code>. |
| 90 | + * @type {string} |
| 91 | + * @default Standard |
| 92 | + * @public |
| 93 | + */ |
| 94 | + type: { |
| 95 | + type: String, |
| 96 | + defaultValue: SwitchType.Textual, |
| 97 | + }, |
| 98 | + }, |
| 99 | + events: /** @lends sap.ui.webcomponents.main.Switch.prototype */ { |
| 100 | + |
| 101 | + /** |
| 102 | + * Fired when the <code>ui5-switch</code> checked state changes. |
| 103 | + * |
| 104 | + * @public |
| 105 | + * @event |
| 106 | + */ |
| 107 | + change: {}, |
| 108 | + }, |
| 109 | +}; |
| 110 | + |
| 111 | +/** |
| 112 | + * @class |
| 113 | + * |
| 114 | + * <h3 class="comment-api-title">Overview</h3> |
| 115 | + * The <code>ui5-switch</code> component is used for changing between binary states. |
| 116 | + * |
| 117 | + * <h3>ES6 Module Import</h3> |
| 118 | + * |
| 119 | + * <code>import "@ui5/webcomponents/dist/Switch";</code> |
| 120 | + * |
| 121 | + * @constructor |
| 122 | + * @author SAP SE |
| 123 | + * @alias sap.ui.webcomponents.main.Switch |
| 124 | + * @extends sap.ui.webcomponents.base.WebComponent |
| 125 | + * @tagname ui5-switch |
| 126 | + * @public |
| 127 | + */ |
| 128 | +class Switch extends WebComponent { |
| 129 | + static get metadata() { |
| 130 | + return metadata; |
| 131 | + } |
| 132 | + |
| 133 | + static get renderer() { |
| 134 | + return SwitchRenderer; |
| 135 | + } |
| 136 | + |
| 137 | + constructor(state) { |
| 138 | + super(state); |
| 139 | + } |
| 140 | + |
| 141 | + ontap(event) { |
| 142 | + if (this._tapAllowed(event.ui5target)) { |
| 143 | + this.toggle(); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + onkeydown(event) { |
| 148 | + if (event.keyCode === KeyCodes.SPACE) { |
| 149 | + event.preventDefault(); |
| 150 | + } |
| 151 | + |
| 152 | + if (event.keyCode === KeyCodes.ENTER) { |
| 153 | + this.toggle(); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + onkeyup(event) { |
| 158 | + if (event.keyCode === KeyCodes.SPACE) { |
| 159 | + this.toggle(); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + toggle() { |
| 164 | + if (!this.disabled) { |
| 165 | + this.checked = !this.checked; |
| 166 | + this.fireEvent("change"); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + _tapAllowed(target) { |
| 171 | + return target !== this; |
| 172 | + } |
| 173 | + |
| 174 | + static get calculateTemplateContext() { |
| 175 | + return SwitchTemplateContext.calculate; |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +Bootstrap.boot().then(_ => { |
| 180 | + Switch.define(); |
| 181 | +}); |
| 182 | + |
| 183 | + |
| 184 | +export default Switch; |
0 commit comments