tab.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 { nodeListToArray } from "../../utils/dom";
  9. /**
  10. * @slot - A slot for adding content to the component.
  11. */
  12. export class Tab {
  13. constructor() {
  14. /**
  15. * When `true`, the component's contents are selected.
  16. *
  17. * Only one tab can be selected within the `calcite-tabs` parent.
  18. *
  19. * @deprecated Use `selected` instead.
  20. */
  21. this.active = false;
  22. /**
  23. * When `true`, the component's contents are selected.
  24. *
  25. * Only one tab can be selected within the `calcite-tabs` parent.
  26. */
  27. this.selected = false;
  28. /**
  29. * @internal
  30. */
  31. this.scale = "m";
  32. this.guid = `calcite-tab-title-${guid()}`;
  33. }
  34. activeHandler(value) {
  35. this.selected = value;
  36. }
  37. selectedHandler(value) {
  38. this.active = value;
  39. }
  40. //--------------------------------------------------------------------------
  41. //
  42. // Lifecycle
  43. //
  44. //--------------------------------------------------------------------------
  45. render() {
  46. const id = this.el.id || this.guid;
  47. return (h(Host, { "aria-labelledby": this.labeledBy, id: id }, h("div", { class: "container", role: "tabpanel", tabIndex: this.selected ? 0 : -1 }, h("section", null, h("slot", null)))));
  48. }
  49. connectedCallback() {
  50. this.parentTabsEl = this.el.closest("calcite-tabs");
  51. const isSelected = this.selected || this.active;
  52. if (isSelected) {
  53. this.activeHandler(isSelected);
  54. this.selectedHandler(isSelected);
  55. }
  56. }
  57. componentDidLoad() {
  58. this.calciteInternalTabRegister.emit();
  59. }
  60. componentWillRender() {
  61. var _a;
  62. this.scale = (_a = this.parentTabsEl) === null || _a === void 0 ? void 0 : _a.scale;
  63. }
  64. disconnectedCallback() {
  65. var _a;
  66. // Dispatching to body in order to be listened by other elements that are still connected to the DOM.
  67. (_a = document.body) === null || _a === void 0 ? void 0 : _a.dispatchEvent(new CustomEvent("calciteTabUnregister", {
  68. detail: this.el
  69. }));
  70. }
  71. //--------------------------------------------------------------------------
  72. //
  73. // Event Listeners
  74. //
  75. //--------------------------------------------------------------------------
  76. internalTabChangeHandler(event) {
  77. const targetTabsEl = event
  78. .composedPath()
  79. .find((el) => el.tagName === "CALCITE-TABS");
  80. // to allow `<calcite-tabs>` to be nested we need to make sure this
  81. // `calciteTabChange` event was actually fired from a within the same
  82. // `<calcite-tabs>` that is the a parent of this tab.
  83. if (targetTabsEl !== this.parentTabsEl) {
  84. return;
  85. }
  86. if (this.tab) {
  87. this.selected = this.tab === event.detail.tab;
  88. }
  89. else {
  90. this.getTabIndex().then((index) => {
  91. this.selected = index === event.detail.tab;
  92. });
  93. }
  94. event.stopPropagation();
  95. }
  96. //--------------------------------------------------------------------------
  97. //
  98. // Public Methods
  99. //
  100. //--------------------------------------------------------------------------
  101. /**
  102. * Returns the index of the component item within the tab array.
  103. */
  104. async getTabIndex() {
  105. return Array.prototype.indexOf.call(nodeListToArray(this.el.parentElement.children).filter((el) => el.matches("calcite-tab")), this.el);
  106. }
  107. //--------------------------------------------------------------------------
  108. //
  109. // Private Methods
  110. //
  111. //--------------------------------------------------------------------------
  112. /**
  113. * @param tabIds
  114. * @param titleIds
  115. * @internal
  116. */
  117. async updateAriaInfo(tabIds = [], titleIds = []) {
  118. this.labeledBy = titleIds[tabIds.indexOf(this.el.id)] || null;
  119. }
  120. static get is() { return "calcite-tab"; }
  121. static get encapsulation() { return "shadow"; }
  122. static get originalStyleUrls() {
  123. return {
  124. "$": ["tab.scss"]
  125. };
  126. }
  127. static get styleUrls() {
  128. return {
  129. "$": ["tab.css"]
  130. };
  131. }
  132. static get properties() {
  133. return {
  134. "tab": {
  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": "Specifies a unique name for the component.\n\nWhen specified, use the same value on the `calcite-tab-title`."
  147. },
  148. "attribute": "tab",
  149. "reflect": true
  150. },
  151. "active": {
  152. "type": "boolean",
  153. "mutable": true,
  154. "complexType": {
  155. "original": "boolean",
  156. "resolved": "boolean",
  157. "references": {}
  158. },
  159. "required": false,
  160. "optional": false,
  161. "docs": {
  162. "tags": [{
  163. "name": "deprecated",
  164. "text": "Use `selected` instead."
  165. }],
  166. "text": "When `true`, the component's contents are selected.\n\nOnly one tab can be selected within the `calcite-tabs` parent."
  167. },
  168. "attribute": "active",
  169. "reflect": true,
  170. "defaultValue": "false"
  171. },
  172. "selected": {
  173. "type": "boolean",
  174. "mutable": true,
  175. "complexType": {
  176. "original": "boolean",
  177. "resolved": "boolean",
  178. "references": {}
  179. },
  180. "required": false,
  181. "optional": false,
  182. "docs": {
  183. "tags": [],
  184. "text": "When `true`, the component's contents are selected.\n\nOnly one tab can be selected within the `calcite-tabs` parent."
  185. },
  186. "attribute": "selected",
  187. "reflect": true,
  188. "defaultValue": "false"
  189. },
  190. "scale": {
  191. "type": "string",
  192. "mutable": true,
  193. "complexType": {
  194. "original": "Scale",
  195. "resolved": "\"l\" | \"m\" | \"s\"",
  196. "references": {
  197. "Scale": {
  198. "location": "import",
  199. "path": "../interfaces"
  200. }
  201. }
  202. },
  203. "required": false,
  204. "optional": false,
  205. "docs": {
  206. "tags": [{
  207. "name": "internal",
  208. "text": undefined
  209. }],
  210. "text": ""
  211. },
  212. "attribute": "scale",
  213. "reflect": true,
  214. "defaultValue": "\"m\""
  215. }
  216. };
  217. }
  218. static get states() {
  219. return {
  220. "labeledBy": {}
  221. };
  222. }
  223. static get events() {
  224. return [{
  225. "method": "calciteInternalTabRegister",
  226. "name": "calciteInternalTabRegister",
  227. "bubbles": true,
  228. "cancelable": false,
  229. "composed": true,
  230. "docs": {
  231. "tags": [{
  232. "name": "internal",
  233. "text": undefined
  234. }],
  235. "text": ""
  236. },
  237. "complexType": {
  238. "original": "void",
  239. "resolved": "void",
  240. "references": {}
  241. }
  242. }];
  243. }
  244. static get methods() {
  245. return {
  246. "getTabIndex": {
  247. "complexType": {
  248. "signature": "() => Promise<number>",
  249. "parameters": [],
  250. "references": {
  251. "Promise": {
  252. "location": "global"
  253. }
  254. },
  255. "return": "Promise<number>"
  256. },
  257. "docs": {
  258. "text": "Returns the index of the component item within the tab array.",
  259. "tags": []
  260. }
  261. },
  262. "updateAriaInfo": {
  263. "complexType": {
  264. "signature": "(tabIds?: string[], titleIds?: string[]) => Promise<void>",
  265. "parameters": [{
  266. "tags": [{
  267. "name": "param",
  268. "text": "tabIds"
  269. }],
  270. "text": ""
  271. }, {
  272. "tags": [{
  273. "name": "param",
  274. "text": "titleIds"
  275. }],
  276. "text": ""
  277. }],
  278. "references": {
  279. "Promise": {
  280. "location": "global"
  281. }
  282. },
  283. "return": "Promise<void>"
  284. },
  285. "docs": {
  286. "text": "",
  287. "tags": [{
  288. "name": "param",
  289. "text": "tabIds"
  290. }, {
  291. "name": "param",
  292. "text": "titleIds"
  293. }, {
  294. "name": "internal",
  295. "text": undefined
  296. }]
  297. }
  298. }
  299. };
  300. }
  301. static get elementRef() { return "el"; }
  302. static get watchers() {
  303. return [{
  304. "propName": "active",
  305. "methodName": "activeHandler"
  306. }, {
  307. "propName": "selected",
  308. "methodName": "selectedHandler"
  309. }];
  310. }
  311. static get listeners() {
  312. return [{
  313. "name": "calciteInternalTabChange",
  314. "method": "internalTabChangeHandler",
  315. "target": "body",
  316. "capture": false,
  317. "passive": false
  318. }];
  319. }
  320. }