tile-select.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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 } from "@stencil/core";
  7. import { guid } from "../../utils/guid";
  8. import { CSS } from "./resources";
  9. import { updateHostInteraction } from "../../utils/interactive";
  10. /**
  11. * @slot - A slot for adding custom content.
  12. */
  13. export class TileSelect {
  14. constructor() {
  15. //--------------------------------------------------------------------------
  16. //
  17. // Properties
  18. //
  19. //--------------------------------------------------------------------------
  20. /** When `true`, the component is checked. */
  21. this.checked = false;
  22. /** When `true`, interaction is prevented and the component is displayed with lower opacity. */
  23. this.disabled = false;
  24. /** When `true`, the component is not displayed and is not focusable or checkable. */
  25. this.hidden = false;
  26. /** When `true`, displays an interactive input based on the `type` property. */
  27. this.inputEnabled = false;
  28. /** When `inputEnabled` is `true`, specifies the placement of the interactive input on the component. */
  29. this.inputAlignment = "start";
  30. /**
  31. * The selection mode of the component.
  32. *
  33. * Use radio for single selection, and checkbox for multiple selections.
  34. */
  35. this.type = "radio";
  36. /** Specifies the width of the component. */
  37. this.width = "auto";
  38. this.guid = `calcite-tile-select-${guid()}`;
  39. //--------------------------------------------------------------------------
  40. //
  41. // State
  42. //
  43. //--------------------------------------------------------------------------
  44. /** The focused state of the tile-select. */
  45. this.focused = false;
  46. }
  47. checkedChanged(newChecked) {
  48. this.input.checked = newChecked;
  49. }
  50. nameChanged(newName) {
  51. this.input.name = newName;
  52. }
  53. //--------------------------------------------------------------------------
  54. //
  55. // Public Methods
  56. //
  57. //--------------------------------------------------------------------------
  58. /** Sets focus on the component. */
  59. async setFocus() {
  60. var _a;
  61. (_a = this.input) === null || _a === void 0 ? void 0 : _a.setFocus();
  62. }
  63. //--------------------------------------------------------------------------
  64. //
  65. // Event Listeners
  66. //
  67. //--------------------------------------------------------------------------
  68. checkboxChangeHandler(event) {
  69. const checkbox = event.target;
  70. if (checkbox === this.input) {
  71. this.checked = checkbox.checked;
  72. }
  73. event.stopPropagation();
  74. this.calciteTileSelectChange.emit();
  75. }
  76. checkboxFocusBlurHandler(event) {
  77. const checkbox = event.target;
  78. if (checkbox === this.input) {
  79. this.focused = event.detail;
  80. }
  81. event.stopPropagation();
  82. }
  83. radioButtonChangeHandler(event) {
  84. const radioButton = event.target;
  85. if (radioButton === this.input) {
  86. this.checked = radioButton.checked;
  87. }
  88. event.stopPropagation();
  89. this.calciteTileSelectChange.emit();
  90. }
  91. radioButtonCheckedChangeHandler(event) {
  92. const radioButton = event.target;
  93. if (radioButton === this.input) {
  94. this.checked = radioButton.checked;
  95. }
  96. event.stopPropagation();
  97. }
  98. radioButtonFocusBlurHandler(event) {
  99. const radioButton = event.target;
  100. if (radioButton === this.input) {
  101. this.focused = radioButton.focused;
  102. }
  103. event.stopPropagation();
  104. }
  105. click(event) {
  106. const target = event.target;
  107. const targets = ["calcite-tile", "calcite-tile-select"];
  108. if (targets.includes(target.localName)) {
  109. this.input.click();
  110. }
  111. }
  112. mouseenter() {
  113. if (this.input.localName === "calcite-radio-button") {
  114. this.input.hovered = true;
  115. }
  116. if (this.input.localName === "calcite-checkbox") {
  117. this.input.hovered = true;
  118. }
  119. }
  120. mouseleave() {
  121. if (this.input.localName === "calcite-radio-button") {
  122. this.input.hovered = false;
  123. }
  124. if (this.input.localName === "calcite-checkbox") {
  125. this.input.hovered = false;
  126. }
  127. }
  128. //--------------------------------------------------------------------------
  129. //
  130. // Lifecycle
  131. //
  132. //--------------------------------------------------------------------------
  133. connectedCallback() {
  134. this.renderInput();
  135. }
  136. disconnectedCallback() {
  137. this.input.parentNode.removeChild(this.input);
  138. }
  139. componentDidRender() {
  140. updateHostInteraction(this);
  141. }
  142. // --------------------------------------------------------------------------
  143. //
  144. // Render Methods
  145. //
  146. // --------------------------------------------------------------------------
  147. renderInput() {
  148. this.input = document.createElement(this.type === "radio" ? "calcite-radio-button" : "calcite-checkbox");
  149. this.input.checked = this.checked;
  150. this.input.disabled = this.disabled;
  151. this.input.hidden = this.hidden;
  152. this.input.id = this.guid;
  153. this.input.label = this.heading || this.name || "";
  154. if (this.name) {
  155. this.input.name = this.name;
  156. }
  157. if (this.value) {
  158. this.input.value = this.value != null ? this.value.toString() : "";
  159. }
  160. this.el.insertAdjacentElement("beforeend", this.input);
  161. }
  162. render() {
  163. const { checked, description, disabled, focused, heading, icon, inputAlignment, inputEnabled, width } = this;
  164. return (h("div", { class: {
  165. checked,
  166. container: true,
  167. [CSS.description]: Boolean(description),
  168. [CSS.descriptionOnly]: Boolean(!heading && !icon && description),
  169. disabled,
  170. focused,
  171. [CSS.heading]: Boolean(heading),
  172. [CSS.headingOnly]: heading && !icon && !description,
  173. [CSS.icon]: Boolean(icon),
  174. [CSS.iconOnly]: !heading && icon && !description,
  175. [CSS.inputAlignmentEnd]: inputAlignment === "end",
  176. [CSS.inputAlignmentStart]: inputAlignment === "start",
  177. [CSS.inputEnabled]: inputEnabled,
  178. [CSS.largeVisual]: heading && icon && !description,
  179. [CSS.widthAuto]: width === "auto",
  180. [CSS.widthFull]: width === "full"
  181. } }, h("calcite-tile", { active: checked, description: description, embed: true, heading: heading, icon: icon }), h("slot", null)));
  182. }
  183. static get is() { return "calcite-tile-select"; }
  184. static get encapsulation() { return "shadow"; }
  185. static get originalStyleUrls() {
  186. return {
  187. "$": ["tile-select.scss"]
  188. };
  189. }
  190. static get styleUrls() {
  191. return {
  192. "$": ["tile-select.css"]
  193. };
  194. }
  195. static get properties() {
  196. return {
  197. "checked": {
  198. "type": "boolean",
  199. "mutable": true,
  200. "complexType": {
  201. "original": "boolean",
  202. "resolved": "boolean",
  203. "references": {}
  204. },
  205. "required": false,
  206. "optional": false,
  207. "docs": {
  208. "tags": [],
  209. "text": "When `true`, the component is checked."
  210. },
  211. "attribute": "checked",
  212. "reflect": true,
  213. "defaultValue": "false"
  214. },
  215. "description": {
  216. "type": "string",
  217. "mutable": false,
  218. "complexType": {
  219. "original": "string",
  220. "resolved": "string",
  221. "references": {}
  222. },
  223. "required": false,
  224. "optional": true,
  225. "docs": {
  226. "tags": [],
  227. "text": "A description for the component, which displays below the heading."
  228. },
  229. "attribute": "description",
  230. "reflect": true
  231. },
  232. "disabled": {
  233. "type": "boolean",
  234. "mutable": false,
  235. "complexType": {
  236. "original": "boolean",
  237. "resolved": "boolean",
  238. "references": {}
  239. },
  240. "required": false,
  241. "optional": false,
  242. "docs": {
  243. "tags": [],
  244. "text": "When `true`, interaction is prevented and the component is displayed with lower opacity."
  245. },
  246. "attribute": "disabled",
  247. "reflect": true,
  248. "defaultValue": "false"
  249. },
  250. "heading": {
  251. "type": "string",
  252. "mutable": false,
  253. "complexType": {
  254. "original": "string",
  255. "resolved": "string",
  256. "references": {}
  257. },
  258. "required": false,
  259. "optional": true,
  260. "docs": {
  261. "tags": [],
  262. "text": "The component header text, which displays between the icon and description."
  263. },
  264. "attribute": "heading",
  265. "reflect": true
  266. },
  267. "hidden": {
  268. "type": "boolean",
  269. "mutable": false,
  270. "complexType": {
  271. "original": "boolean",
  272. "resolved": "boolean",
  273. "references": {}
  274. },
  275. "required": false,
  276. "optional": false,
  277. "docs": {
  278. "tags": [],
  279. "text": "When `true`, the component is not displayed and is not focusable or checkable."
  280. },
  281. "attribute": "hidden",
  282. "reflect": true,
  283. "defaultValue": "false"
  284. },
  285. "icon": {
  286. "type": "string",
  287. "mutable": false,
  288. "complexType": {
  289. "original": "string",
  290. "resolved": "string",
  291. "references": {}
  292. },
  293. "required": false,
  294. "optional": true,
  295. "docs": {
  296. "tags": [],
  297. "text": "Specifies an icon to display."
  298. },
  299. "attribute": "icon",
  300. "reflect": true
  301. },
  302. "name": {
  303. "type": "any",
  304. "mutable": false,
  305. "complexType": {
  306. "original": "any",
  307. "resolved": "any",
  308. "references": {}
  309. },
  310. "required": false,
  311. "optional": false,
  312. "docs": {
  313. "tags": [],
  314. "text": "Specifies the name of the component on form submission."
  315. },
  316. "attribute": "name",
  317. "reflect": true
  318. },
  319. "inputEnabled": {
  320. "type": "boolean",
  321. "mutable": false,
  322. "complexType": {
  323. "original": "boolean",
  324. "resolved": "boolean",
  325. "references": {}
  326. },
  327. "required": false,
  328. "optional": false,
  329. "docs": {
  330. "tags": [],
  331. "text": "When `true`, displays an interactive input based on the `type` property."
  332. },
  333. "attribute": "input-enabled",
  334. "reflect": true,
  335. "defaultValue": "false"
  336. },
  337. "inputAlignment": {
  338. "type": "string",
  339. "mutable": false,
  340. "complexType": {
  341. "original": "Extract<\"end\" | \"start\", Alignment>",
  342. "resolved": "\"end\" | \"start\"",
  343. "references": {
  344. "Extract": {
  345. "location": "global"
  346. },
  347. "Alignment": {
  348. "location": "import",
  349. "path": "../interfaces"
  350. }
  351. }
  352. },
  353. "required": false,
  354. "optional": false,
  355. "docs": {
  356. "tags": [],
  357. "text": "When `inputEnabled` is `true`, specifies the placement of the interactive input on the component."
  358. },
  359. "attribute": "input-alignment",
  360. "reflect": true,
  361. "defaultValue": "\"start\""
  362. },
  363. "type": {
  364. "type": "string",
  365. "mutable": false,
  366. "complexType": {
  367. "original": "TileSelectType",
  368. "resolved": "\"checkbox\" | \"radio\"",
  369. "references": {
  370. "TileSelectType": {
  371. "location": "import",
  372. "path": "./interfaces"
  373. }
  374. }
  375. },
  376. "required": false,
  377. "optional": false,
  378. "docs": {
  379. "tags": [],
  380. "text": "The selection mode of the component.\n\nUse radio for single selection, and checkbox for multiple selections."
  381. },
  382. "attribute": "type",
  383. "reflect": true,
  384. "defaultValue": "\"radio\""
  385. },
  386. "value": {
  387. "type": "any",
  388. "mutable": false,
  389. "complexType": {
  390. "original": "any",
  391. "resolved": "any",
  392. "references": {}
  393. },
  394. "required": false,
  395. "optional": true,
  396. "docs": {
  397. "tags": [],
  398. "text": "The component's value."
  399. },
  400. "attribute": "value",
  401. "reflect": false
  402. },
  403. "width": {
  404. "type": "string",
  405. "mutable": false,
  406. "complexType": {
  407. "original": "Extract<\"auto\" | \"full\", Width>",
  408. "resolved": "\"auto\" | \"full\"",
  409. "references": {
  410. "Extract": {
  411. "location": "global"
  412. },
  413. "Width": {
  414. "location": "import",
  415. "path": "../interfaces"
  416. }
  417. }
  418. },
  419. "required": false,
  420. "optional": false,
  421. "docs": {
  422. "tags": [],
  423. "text": "Specifies the width of the component."
  424. },
  425. "attribute": "width",
  426. "reflect": true,
  427. "defaultValue": "\"auto\""
  428. }
  429. };
  430. }
  431. static get states() {
  432. return {
  433. "focused": {}
  434. };
  435. }
  436. static get events() {
  437. return [{
  438. "method": "calciteTileSelectChange",
  439. "name": "calciteTileSelectChange",
  440. "bubbles": true,
  441. "cancelable": false,
  442. "composed": true,
  443. "docs": {
  444. "tags": [],
  445. "text": "Emits a custom change event.\n\nFor checkboxes it emits when checked or unchecked.\n\nFor radios it only emits when checked."
  446. },
  447. "complexType": {
  448. "original": "void",
  449. "resolved": "void",
  450. "references": {}
  451. }
  452. }];
  453. }
  454. static get methods() {
  455. return {
  456. "setFocus": {
  457. "complexType": {
  458. "signature": "() => Promise<void>",
  459. "parameters": [],
  460. "references": {
  461. "Promise": {
  462. "location": "global"
  463. }
  464. },
  465. "return": "Promise<void>"
  466. },
  467. "docs": {
  468. "text": "Sets focus on the component.",
  469. "tags": []
  470. }
  471. }
  472. };
  473. }
  474. static get elementRef() { return "el"; }
  475. static get watchers() {
  476. return [{
  477. "propName": "checked",
  478. "methodName": "checkedChanged"
  479. }, {
  480. "propName": "name",
  481. "methodName": "nameChanged"
  482. }];
  483. }
  484. static get listeners() {
  485. return [{
  486. "name": "calciteCheckboxChange",
  487. "method": "checkboxChangeHandler",
  488. "target": undefined,
  489. "capture": false,
  490. "passive": false
  491. }, {
  492. "name": "calciteInternalCheckboxFocus",
  493. "method": "checkboxFocusBlurHandler",
  494. "target": undefined,
  495. "capture": false,
  496. "passive": false
  497. }, {
  498. "name": "calciteInternalCheckboxBlur",
  499. "method": "checkboxFocusBlurHandler",
  500. "target": undefined,
  501. "capture": false,
  502. "passive": false
  503. }, {
  504. "name": "calciteRadioButtonChange",
  505. "method": "radioButtonChangeHandler",
  506. "target": undefined,
  507. "capture": false,
  508. "passive": false
  509. }, {
  510. "name": "calciteInternalRadioButtonCheckedChange",
  511. "method": "radioButtonCheckedChangeHandler",
  512. "target": undefined,
  513. "capture": false,
  514. "passive": false
  515. }, {
  516. "name": "calciteInternalRadioButtonFocus",
  517. "method": "radioButtonFocusBlurHandler",
  518. "target": undefined,
  519. "capture": false,
  520. "passive": false
  521. }, {
  522. "name": "calciteInternalRadioButtonBlur",
  523. "method": "radioButtonFocusBlurHandler",
  524. "target": undefined,
  525. "capture": false,
  526. "passive": false
  527. }, {
  528. "name": "click",
  529. "method": "click",
  530. "target": undefined,
  531. "capture": false,
  532. "passive": false
  533. }, {
  534. "name": "pointerenter",
  535. "method": "mouseenter",
  536. "target": undefined,
  537. "capture": false,
  538. "passive": true
  539. }, {
  540. "name": "pointerleave",
  541. "method": "mouseleave",
  542. "target": undefined,
  543. "capture": false,
  544. "passive": true
  545. }];
  546. }
  547. }