switch.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*!
  2. * All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
  4. * v1.0.0-beta.97
  5. */
  6. import { h, Host } from "@stencil/core";
  7. import { focusElement, toAriaBoolean } from "../../utils/dom";
  8. import { connectLabel, disconnectLabel, getLabelText } from "../../utils/label";
  9. import { connectForm, disconnectForm, HiddenFormInputSlot } from "../../utils/form";
  10. import { updateHostInteraction } from "../../utils/interactive";
  11. import { isActivationKey } from "../../utils/key";
  12. export class Switch {
  13. constructor() {
  14. //--------------------------------------------------------------------------
  15. //
  16. // Properties
  17. //
  18. //--------------------------------------------------------------------------
  19. /** When `true`, interaction is prevented and the component is displayed with lower opacity. */
  20. this.disabled = false;
  21. /** Specifies the size of the component. */
  22. this.scale = "m";
  23. /**
  24. * When `true`, the component is checked.
  25. *
  26. * @deprecated use `checked` instead.
  27. */
  28. this.switched = false;
  29. /** When `true`, the component is checked. */
  30. this.checked = false;
  31. //--------------------------------------------------------------------------
  32. //
  33. // Private Methods
  34. //
  35. //--------------------------------------------------------------------------
  36. this.keyDownHandler = (event) => {
  37. if (!this.disabled && isActivationKey(event.key)) {
  38. this.toggle();
  39. event.preventDefault();
  40. }
  41. };
  42. this.clickHandler = () => {
  43. this.toggle();
  44. };
  45. this.setSwitchEl = (el) => {
  46. this.switchEl = el;
  47. };
  48. }
  49. switchedWatcher(switched) {
  50. this.checked = switched;
  51. }
  52. //--------------------------------------------------------------------------
  53. //
  54. // Public Methods
  55. //
  56. //--------------------------------------------------------------------------
  57. /** Sets focus on the component. */
  58. async setFocus() {
  59. focusElement(this.switchEl);
  60. }
  61. onLabelClick() {
  62. if (!this.disabled) {
  63. this.toggle();
  64. this.setFocus();
  65. }
  66. }
  67. toggle() {
  68. this.checked = !this.checked;
  69. this.calciteSwitchChange.emit({
  70. switched: this.checked
  71. });
  72. }
  73. //--------------------------------------------------------------------------
  74. //
  75. // Lifecycle
  76. //
  77. //--------------------------------------------------------------------------
  78. connectedCallback() {
  79. const initiallyChecked = this.checked || this.switched;
  80. if (initiallyChecked) {
  81. // if either prop is set, we ensure both are synced initially
  82. this.switched = this.checked = initiallyChecked;
  83. }
  84. connectLabel(this);
  85. connectForm(this);
  86. }
  87. disconnectedCallback() {
  88. disconnectLabel(this);
  89. disconnectForm(this);
  90. }
  91. componentDidRender() {
  92. updateHostInteraction(this);
  93. }
  94. // --------------------------------------------------------------------------
  95. //
  96. // Render Methods
  97. //
  98. // --------------------------------------------------------------------------
  99. render() {
  100. return (h(Host, { onClick: this.clickHandler, onKeyDown: this.keyDownHandler }, h("div", { "aria-checked": toAriaBoolean(this.checked), "aria-label": getLabelText(this), class: "container", ref: this.setSwitchEl, role: "switch", tabIndex: 0 }, h("div", { class: "track" }, h("div", { class: "handle" })), h(HiddenFormInputSlot, { component: this }))));
  101. }
  102. static get is() { return "calcite-switch"; }
  103. static get encapsulation() { return "shadow"; }
  104. static get originalStyleUrls() {
  105. return {
  106. "$": ["switch.scss"]
  107. };
  108. }
  109. static get styleUrls() {
  110. return {
  111. "$": ["switch.css"]
  112. };
  113. }
  114. static get properties() {
  115. return {
  116. "disabled": {
  117. "type": "boolean",
  118. "mutable": false,
  119. "complexType": {
  120. "original": "boolean",
  121. "resolved": "boolean",
  122. "references": {}
  123. },
  124. "required": false,
  125. "optional": false,
  126. "docs": {
  127. "tags": [],
  128. "text": "When `true`, interaction is prevented and the component is displayed with lower opacity."
  129. },
  130. "attribute": "disabled",
  131. "reflect": true,
  132. "defaultValue": "false"
  133. },
  134. "label": {
  135. "type": "string",
  136. "mutable": false,
  137. "complexType": {
  138. "original": "string",
  139. "resolved": "string",
  140. "references": {}
  141. },
  142. "required": false,
  143. "optional": true,
  144. "docs": {
  145. "tags": [],
  146. "text": "Accessible name for the component."
  147. },
  148. "attribute": "label",
  149. "reflect": false
  150. },
  151. "name": {
  152. "type": "string",
  153. "mutable": false,
  154. "complexType": {
  155. "original": "string",
  156. "resolved": "string",
  157. "references": {}
  158. },
  159. "required": false,
  160. "optional": false,
  161. "docs": {
  162. "tags": [],
  163. "text": "Specifies the name of the component on form submission."
  164. },
  165. "attribute": "name",
  166. "reflect": true
  167. },
  168. "scale": {
  169. "type": "string",
  170. "mutable": false,
  171. "complexType": {
  172. "original": "Scale",
  173. "resolved": "\"l\" | \"m\" | \"s\"",
  174. "references": {
  175. "Scale": {
  176. "location": "import",
  177. "path": "../interfaces"
  178. }
  179. }
  180. },
  181. "required": false,
  182. "optional": false,
  183. "docs": {
  184. "tags": [],
  185. "text": "Specifies the size of the component."
  186. },
  187. "attribute": "scale",
  188. "reflect": true,
  189. "defaultValue": "\"m\""
  190. },
  191. "switched": {
  192. "type": "boolean",
  193. "mutable": true,
  194. "complexType": {
  195. "original": "boolean",
  196. "resolved": "boolean",
  197. "references": {}
  198. },
  199. "required": false,
  200. "optional": false,
  201. "docs": {
  202. "tags": [{
  203. "name": "deprecated",
  204. "text": "use `checked` instead."
  205. }],
  206. "text": "When `true`, the component is checked."
  207. },
  208. "attribute": "switched",
  209. "reflect": true,
  210. "defaultValue": "false"
  211. },
  212. "checked": {
  213. "type": "boolean",
  214. "mutable": true,
  215. "complexType": {
  216. "original": "boolean",
  217. "resolved": "boolean",
  218. "references": {}
  219. },
  220. "required": false,
  221. "optional": false,
  222. "docs": {
  223. "tags": [],
  224. "text": "When `true`, the component is checked."
  225. },
  226. "attribute": "checked",
  227. "reflect": true,
  228. "defaultValue": "false"
  229. },
  230. "value": {
  231. "type": "any",
  232. "mutable": false,
  233. "complexType": {
  234. "original": "any",
  235. "resolved": "any",
  236. "references": {}
  237. },
  238. "required": false,
  239. "optional": false,
  240. "docs": {
  241. "tags": [],
  242. "text": "The component's value."
  243. },
  244. "attribute": "value",
  245. "reflect": false
  246. }
  247. };
  248. }
  249. static get events() {
  250. return [{
  251. "method": "calciteSwitchChange",
  252. "name": "calciteSwitchChange",
  253. "bubbles": true,
  254. "cancelable": false,
  255. "composed": true,
  256. "docs": {
  257. "tags": [],
  258. "text": "Fires when the `checked` value has changed.\n\n**Note:** The event payload is deprecated, use the component's `checked` property instead."
  259. },
  260. "complexType": {
  261. "original": "DeprecatedEventPayload",
  262. "resolved": "any",
  263. "references": {
  264. "DeprecatedEventPayload": {
  265. "location": "import",
  266. "path": "../interfaces"
  267. }
  268. }
  269. }
  270. }];
  271. }
  272. static get methods() {
  273. return {
  274. "setFocus": {
  275. "complexType": {
  276. "signature": "() => Promise<void>",
  277. "parameters": [],
  278. "references": {
  279. "Promise": {
  280. "location": "global"
  281. }
  282. },
  283. "return": "Promise<void>"
  284. },
  285. "docs": {
  286. "text": "Sets focus on the component.",
  287. "tags": []
  288. }
  289. }
  290. };
  291. }
  292. static get elementRef() { return "el"; }
  293. static get watchers() {
  294. return [{
  295. "propName": "switched",
  296. "methodName": "switchedWatcher"
  297. }];
  298. }
  299. }