checkbox.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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 { guid } from "../../utils/guid";
  8. import { HiddenFormInputSlot } from "../../utils/form";
  9. import { connectLabel, disconnectLabel, getLabelText } from "../../utils/label";
  10. import { connectForm, disconnectForm } from "../../utils/form";
  11. import { updateHostInteraction } from "../../utils/interactive";
  12. import { toAriaBoolean } from "../../utils/dom";
  13. import { isActivationKey } from "../../utils/key";
  14. export class Checkbox {
  15. constructor() {
  16. //--------------------------------------------------------------------------
  17. //
  18. // Properties
  19. //
  20. //--------------------------------------------------------------------------
  21. /** When `true`, the component is checked. */
  22. this.checked = false;
  23. /** When `true`, interaction is prevented and the component is displayed with lower opacity. */
  24. this.disabled = false;
  25. /**
  26. * The hovered state of the checkbox.
  27. *
  28. * @internal
  29. */
  30. this.hovered = false;
  31. /**
  32. * When `true`, the component is initially indeterminate, which is independent from its `checked` value.
  33. *
  34. * The state is visual only, and can look different across browsers.
  35. *
  36. * @mdn [indeterminate](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox#indeterminate_state_checkboxes)
  37. */
  38. this.indeterminate = false;
  39. /**
  40. * When `true`, the component must have a value in order for the form to submit.
  41. *
  42. * @internal
  43. */
  44. this.required = false;
  45. /** Specifies the size of the component. */
  46. this.scale = "m";
  47. //--------------------------------------------------------------------------
  48. //
  49. // Private Properties
  50. //
  51. //--------------------------------------------------------------------------
  52. this.checkedPath = "M5.5 12L2 8.689l.637-.636L5.5 10.727l8.022-7.87.637.637z";
  53. this.indeterminatePath = "M13 8v1H3V8z";
  54. //--------------------------------------------------------------------------
  55. //
  56. // Private Methods
  57. //
  58. //--------------------------------------------------------------------------
  59. this.getPath = () => this.indeterminate ? this.indeterminatePath : this.checked ? this.checkedPath : "";
  60. this.toggle = () => {
  61. if (!this.disabled) {
  62. this.checked = !this.checked;
  63. this.setFocus();
  64. this.indeterminate = false;
  65. this.calciteCheckboxChange.emit();
  66. }
  67. };
  68. this.keyDownHandler = (event) => {
  69. if (isActivationKey(event.key)) {
  70. this.toggle();
  71. event.preventDefault();
  72. }
  73. };
  74. this.clickHandler = () => {
  75. this.toggle();
  76. };
  77. //--------------------------------------------------------------------------
  78. //
  79. // Event Listeners
  80. //
  81. //--------------------------------------------------------------------------
  82. this.onToggleBlur = () => {
  83. this.calciteInternalCheckboxBlur.emit(false);
  84. };
  85. this.onToggleFocus = () => {
  86. this.calciteInternalCheckboxFocus.emit(true);
  87. };
  88. this.onLabelClick = () => {
  89. this.toggle();
  90. };
  91. }
  92. //--------------------------------------------------------------------------
  93. //
  94. // Public Methods
  95. //
  96. //--------------------------------------------------------------------------
  97. /** Sets focus on the component. */
  98. async setFocus() {
  99. var _a;
  100. (_a = this.toggleEl) === null || _a === void 0 ? void 0 : _a.focus();
  101. }
  102. //--------------------------------------------------------------------------
  103. //
  104. // Lifecycle
  105. //
  106. //--------------------------------------------------------------------------
  107. connectedCallback() {
  108. this.guid = this.el.id || `calcite-checkbox-${guid()}`;
  109. connectLabel(this);
  110. connectForm(this);
  111. }
  112. disconnectedCallback() {
  113. disconnectLabel(this);
  114. disconnectForm(this);
  115. }
  116. componentDidRender() {
  117. updateHostInteraction(this);
  118. }
  119. // --------------------------------------------------------------------------
  120. //
  121. // Render Methods
  122. //
  123. // --------------------------------------------------------------------------
  124. render() {
  125. return (h(Host, { onClick: this.clickHandler, onKeyDown: this.keyDownHandler }, h("div", { "aria-checked": toAriaBoolean(this.checked), "aria-label": getLabelText(this), class: "toggle", onBlur: this.onToggleBlur, onFocus: this.onToggleFocus, ref: (toggleEl) => (this.toggleEl = toggleEl), role: "checkbox", tabIndex: this.disabled ? undefined : 0 }, h("svg", { "aria-hidden": "true", class: "check-svg", viewBox: "0 0 16 16" }, h("path", { d: this.getPath() })), h("slot", null)), h(HiddenFormInputSlot, { component: this })));
  126. }
  127. static get is() { return "calcite-checkbox"; }
  128. static get encapsulation() { return "shadow"; }
  129. static get originalStyleUrls() {
  130. return {
  131. "$": ["checkbox.scss"]
  132. };
  133. }
  134. static get styleUrls() {
  135. return {
  136. "$": ["checkbox.css"]
  137. };
  138. }
  139. static get properties() {
  140. return {
  141. "checked": {
  142. "type": "boolean",
  143. "mutable": true,
  144. "complexType": {
  145. "original": "boolean",
  146. "resolved": "boolean",
  147. "references": {}
  148. },
  149. "required": false,
  150. "optional": false,
  151. "docs": {
  152. "tags": [],
  153. "text": "When `true`, the component is checked."
  154. },
  155. "attribute": "checked",
  156. "reflect": true,
  157. "defaultValue": "false"
  158. },
  159. "disabled": {
  160. "type": "boolean",
  161. "mutable": false,
  162. "complexType": {
  163. "original": "boolean",
  164. "resolved": "boolean",
  165. "references": {}
  166. },
  167. "required": false,
  168. "optional": false,
  169. "docs": {
  170. "tags": [],
  171. "text": "When `true`, interaction is prevented and the component is displayed with lower opacity."
  172. },
  173. "attribute": "disabled",
  174. "reflect": true,
  175. "defaultValue": "false"
  176. },
  177. "guid": {
  178. "type": "string",
  179. "mutable": true,
  180. "complexType": {
  181. "original": "string",
  182. "resolved": "string",
  183. "references": {}
  184. },
  185. "required": false,
  186. "optional": false,
  187. "docs": {
  188. "tags": [],
  189. "text": "The `id` attribute of the component. When omitted, a globally unique identifier is used."
  190. },
  191. "attribute": "guid",
  192. "reflect": true
  193. },
  194. "hovered": {
  195. "type": "boolean",
  196. "mutable": true,
  197. "complexType": {
  198. "original": "boolean",
  199. "resolved": "boolean",
  200. "references": {}
  201. },
  202. "required": false,
  203. "optional": false,
  204. "docs": {
  205. "tags": [{
  206. "name": "internal",
  207. "text": undefined
  208. }],
  209. "text": "The hovered state of the checkbox."
  210. },
  211. "attribute": "hovered",
  212. "reflect": true,
  213. "defaultValue": "false"
  214. },
  215. "indeterminate": {
  216. "type": "boolean",
  217. "mutable": true,
  218. "complexType": {
  219. "original": "boolean",
  220. "resolved": "boolean",
  221. "references": {}
  222. },
  223. "required": false,
  224. "optional": false,
  225. "docs": {
  226. "tags": [{
  227. "name": "mdn",
  228. "text": "[indeterminate](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox#indeterminate_state_checkboxes)"
  229. }],
  230. "text": "When `true`, the component is initially indeterminate, which is independent from its `checked` value.\n\nThe state is visual only, and can look different across browsers."
  231. },
  232. "attribute": "indeterminate",
  233. "reflect": true,
  234. "defaultValue": "false"
  235. },
  236. "label": {
  237. "type": "string",
  238. "mutable": false,
  239. "complexType": {
  240. "original": "string",
  241. "resolved": "string",
  242. "references": {}
  243. },
  244. "required": false,
  245. "optional": true,
  246. "docs": {
  247. "tags": [{
  248. "name": "internal",
  249. "text": undefined
  250. }],
  251. "text": "Accessible name for the component."
  252. },
  253. "attribute": "label",
  254. "reflect": false
  255. },
  256. "name": {
  257. "type": "any",
  258. "mutable": false,
  259. "complexType": {
  260. "original": "any",
  261. "resolved": "any",
  262. "references": {}
  263. },
  264. "required": false,
  265. "optional": false,
  266. "docs": {
  267. "tags": [],
  268. "text": "Specifies the name of the component on form submission."
  269. },
  270. "attribute": "name",
  271. "reflect": true
  272. },
  273. "required": {
  274. "type": "boolean",
  275. "mutable": false,
  276. "complexType": {
  277. "original": "boolean",
  278. "resolved": "boolean",
  279. "references": {}
  280. },
  281. "required": false,
  282. "optional": false,
  283. "docs": {
  284. "tags": [{
  285. "name": "internal",
  286. "text": undefined
  287. }],
  288. "text": "When `true`, the component must have a value in order for the form to submit."
  289. },
  290. "attribute": "required",
  291. "reflect": true,
  292. "defaultValue": "false"
  293. },
  294. "scale": {
  295. "type": "string",
  296. "mutable": false,
  297. "complexType": {
  298. "original": "Scale",
  299. "resolved": "\"l\" | \"m\" | \"s\"",
  300. "references": {
  301. "Scale": {
  302. "location": "import",
  303. "path": "../interfaces"
  304. }
  305. }
  306. },
  307. "required": false,
  308. "optional": false,
  309. "docs": {
  310. "tags": [],
  311. "text": "Specifies the size of the component."
  312. },
  313. "attribute": "scale",
  314. "reflect": true,
  315. "defaultValue": "\"m\""
  316. },
  317. "value": {
  318. "type": "any",
  319. "mutable": false,
  320. "complexType": {
  321. "original": "any",
  322. "resolved": "any",
  323. "references": {}
  324. },
  325. "required": false,
  326. "optional": false,
  327. "docs": {
  328. "tags": [],
  329. "text": "The component's value."
  330. },
  331. "attribute": "value",
  332. "reflect": false
  333. }
  334. };
  335. }
  336. static get events() {
  337. return [{
  338. "method": "calciteInternalCheckboxBlur",
  339. "name": "calciteInternalCheckboxBlur",
  340. "bubbles": true,
  341. "cancelable": false,
  342. "composed": true,
  343. "docs": {
  344. "tags": [{
  345. "name": "internal",
  346. "text": undefined
  347. }],
  348. "text": "Emits when the component is blurred."
  349. },
  350. "complexType": {
  351. "original": "boolean",
  352. "resolved": "boolean",
  353. "references": {}
  354. }
  355. }, {
  356. "method": "calciteCheckboxChange",
  357. "name": "calciteCheckboxChange",
  358. "bubbles": true,
  359. "cancelable": false,
  360. "composed": true,
  361. "docs": {
  362. "tags": [],
  363. "text": "Emits when the component's `checked` status changes."
  364. },
  365. "complexType": {
  366. "original": "void",
  367. "resolved": "void",
  368. "references": {}
  369. }
  370. }, {
  371. "method": "calciteInternalCheckboxFocus",
  372. "name": "calciteInternalCheckboxFocus",
  373. "bubbles": true,
  374. "cancelable": false,
  375. "composed": true,
  376. "docs": {
  377. "tags": [{
  378. "name": "internal",
  379. "text": undefined
  380. }],
  381. "text": "Emits when the component is focused."
  382. },
  383. "complexType": {
  384. "original": "boolean",
  385. "resolved": "boolean",
  386. "references": {}
  387. }
  388. }];
  389. }
  390. static get methods() {
  391. return {
  392. "setFocus": {
  393. "complexType": {
  394. "signature": "() => Promise<void>",
  395. "parameters": [],
  396. "references": {
  397. "Promise": {
  398. "location": "global"
  399. }
  400. },
  401. "return": "Promise<void>"
  402. },
  403. "docs": {
  404. "text": "Sets focus on the component.",
  405. "tags": []
  406. }
  407. }
  408. };
  409. }
  410. static get elementRef() { return "el"; }
  411. }