chip.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 { getSlotted } from "../../utils/dom";
  8. import { guid } from "../../utils/guid";
  9. import { CSS, TEXT, SLOTS, ICONS } from "./resources";
  10. import { connectConditionalSlotComponent, disconnectConditionalSlotComponent } from "../../utils/conditionalSlot";
  11. /**
  12. * @slot - A slot for adding text.
  13. * @slot image - A slot for adding an image.
  14. */
  15. export class Chip {
  16. constructor() {
  17. //--------------------------------------------------------------------------
  18. //
  19. // Public Properties
  20. //
  21. //--------------------------------------------------------------------------
  22. /** Specifies the appearance style of the component. */
  23. this.appearance = "solid";
  24. /** Specifies the color for the component. */
  25. this.color = "grey";
  26. /**
  27. * When `true`, a close button is added to the component.
  28. *
  29. * @deprecated use `closable` instead.
  30. */
  31. this.dismissible = false;
  32. /** When `true`, a close button is added to the component. */
  33. this.closable = false;
  34. /**
  35. * Accessible name for the component's close button.
  36. *
  37. * @default "Close"
  38. */
  39. this.dismissLabel = TEXT.close;
  40. /** When `true`, the icon will be flipped when the element direction is right-to-left (`"rtl"`). */
  41. this.iconFlipRtl = false;
  42. /** Specifies the size of the component. */
  43. this.scale = "m";
  44. /** When `true`, hides the component. */
  45. this.closed = false;
  46. // --------------------------------------------------------------------------
  47. //
  48. // Private Methods
  49. //
  50. // --------------------------------------------------------------------------
  51. this.closeClickHandler = (event) => {
  52. event.preventDefault();
  53. this.calciteChipDismiss.emit(this.el);
  54. this.closed = true;
  55. };
  56. this.guid = guid();
  57. }
  58. handleDismissible(value) {
  59. this.closable = value;
  60. }
  61. handleClosable(value) {
  62. this.dismissible = value;
  63. }
  64. // --------------------------------------------------------------------------
  65. //
  66. // Lifecycle
  67. //
  68. // --------------------------------------------------------------------------
  69. connectedCallback() {
  70. connectConditionalSlotComponent(this);
  71. if (this.dismissible) {
  72. this.handleDismissible(this.dismissible);
  73. }
  74. if (this.closable) {
  75. this.handleClosable(this.closable);
  76. }
  77. }
  78. disconnectedCallback() {
  79. disconnectConditionalSlotComponent(this);
  80. }
  81. //--------------------------------------------------------------------------
  82. //
  83. // Public Methods
  84. //
  85. //--------------------------------------------------------------------------
  86. /** Sets focus on the component. */
  87. async setFocus() {
  88. var _a;
  89. (_a = this.closeButton) === null || _a === void 0 ? void 0 : _a.focus();
  90. }
  91. //--------------------------------------------------------------------------
  92. //
  93. // Render Methods
  94. //
  95. //--------------------------------------------------------------------------
  96. renderChipImage() {
  97. const { el } = this;
  98. const hasChipImage = getSlotted(el, SLOTS.image);
  99. return hasChipImage ? (h("div", { class: CSS.imageContainer, key: "image" }, h("slot", { name: SLOTS.image }))) : null;
  100. }
  101. render() {
  102. const iconEl = (h("calcite-icon", { class: CSS.chipIcon, flipRtl: this.iconFlipRtl, icon: this.icon, scale: "s" }));
  103. const closeButton = (h("button", { "aria-describedby": this.guid, "aria-label": this.dismissLabel, class: CSS.close, onClick: this.closeClickHandler, ref: (el) => (this.closeButton = el) }, h("calcite-icon", { class: CSS.closeIcon, icon: ICONS.close, scale: "s" })));
  104. return (h("div", { class: "container" }, this.renderChipImage(), this.icon ? iconEl : null, h("span", { class: CSS.title, id: this.guid }, h("slot", null)), this.closable ? closeButton : null));
  105. }
  106. static get is() { return "calcite-chip"; }
  107. static get encapsulation() { return "shadow"; }
  108. static get originalStyleUrls() {
  109. return {
  110. "$": ["chip.scss"]
  111. };
  112. }
  113. static get styleUrls() {
  114. return {
  115. "$": ["chip.css"]
  116. };
  117. }
  118. static get properties() {
  119. return {
  120. "appearance": {
  121. "type": "string",
  122. "mutable": false,
  123. "complexType": {
  124. "original": "Extract<\"solid\" | \"clear\", Appearance>",
  125. "resolved": "\"clear\" | \"solid\"",
  126. "references": {
  127. "Extract": {
  128. "location": "global"
  129. },
  130. "Appearance": {
  131. "location": "import",
  132. "path": "../interfaces"
  133. }
  134. }
  135. },
  136. "required": false,
  137. "optional": false,
  138. "docs": {
  139. "tags": [],
  140. "text": "Specifies the appearance style of the component."
  141. },
  142. "attribute": "appearance",
  143. "reflect": true,
  144. "defaultValue": "\"solid\""
  145. },
  146. "color": {
  147. "type": "string",
  148. "mutable": false,
  149. "complexType": {
  150. "original": "ChipColor",
  151. "resolved": "\"blue\" | \"green\" | \"grey\" | \"red\" | \"yellow\"",
  152. "references": {
  153. "ChipColor": {
  154. "location": "import",
  155. "path": "./interfaces"
  156. }
  157. }
  158. },
  159. "required": false,
  160. "optional": false,
  161. "docs": {
  162. "tags": [],
  163. "text": "Specifies the color for the component."
  164. },
  165. "attribute": "color",
  166. "reflect": true,
  167. "defaultValue": "\"grey\""
  168. },
  169. "dismissible": {
  170. "type": "boolean",
  171. "mutable": true,
  172. "complexType": {
  173. "original": "boolean",
  174. "resolved": "boolean",
  175. "references": {}
  176. },
  177. "required": false,
  178. "optional": false,
  179. "docs": {
  180. "tags": [{
  181. "name": "deprecated",
  182. "text": "use `closable` instead."
  183. }],
  184. "text": "When `true`, a close button is added to the component."
  185. },
  186. "attribute": "dismissible",
  187. "reflect": true,
  188. "defaultValue": "false"
  189. },
  190. "closable": {
  191. "type": "boolean",
  192. "mutable": true,
  193. "complexType": {
  194. "original": "boolean",
  195. "resolved": "boolean",
  196. "references": {}
  197. },
  198. "required": false,
  199. "optional": false,
  200. "docs": {
  201. "tags": [],
  202. "text": "When `true`, a close button is added to the component."
  203. },
  204. "attribute": "closable",
  205. "reflect": true,
  206. "defaultValue": "false"
  207. },
  208. "dismissLabel": {
  209. "type": "string",
  210. "mutable": false,
  211. "complexType": {
  212. "original": "string",
  213. "resolved": "string",
  214. "references": {}
  215. },
  216. "required": false,
  217. "optional": true,
  218. "docs": {
  219. "tags": [{
  220. "name": "default",
  221. "text": "\"Close\""
  222. }],
  223. "text": "Accessible name for the component's close button."
  224. },
  225. "attribute": "dismiss-label",
  226. "reflect": false,
  227. "defaultValue": "TEXT.close"
  228. },
  229. "icon": {
  230. "type": "string",
  231. "mutable": false,
  232. "complexType": {
  233. "original": "string",
  234. "resolved": "string",
  235. "references": {}
  236. },
  237. "required": false,
  238. "optional": true,
  239. "docs": {
  240. "tags": [],
  241. "text": "Specifies an icon to display."
  242. },
  243. "attribute": "icon",
  244. "reflect": true
  245. },
  246. "iconFlipRtl": {
  247. "type": "boolean",
  248. "mutable": false,
  249. "complexType": {
  250. "original": "boolean",
  251. "resolved": "boolean",
  252. "references": {}
  253. },
  254. "required": false,
  255. "optional": false,
  256. "docs": {
  257. "tags": [],
  258. "text": "When `true`, the icon will be flipped when the element direction is right-to-left (`\"rtl\"`)."
  259. },
  260. "attribute": "icon-flip-rtl",
  261. "reflect": true,
  262. "defaultValue": "false"
  263. },
  264. "scale": {
  265. "type": "string",
  266. "mutable": false,
  267. "complexType": {
  268. "original": "Scale",
  269. "resolved": "\"l\" | \"m\" | \"s\"",
  270. "references": {
  271. "Scale": {
  272. "location": "import",
  273. "path": "../interfaces"
  274. }
  275. }
  276. },
  277. "required": false,
  278. "optional": false,
  279. "docs": {
  280. "tags": [],
  281. "text": "Specifies the size of the component."
  282. },
  283. "attribute": "scale",
  284. "reflect": true,
  285. "defaultValue": "\"m\""
  286. },
  287. "value": {
  288. "type": "any",
  289. "mutable": false,
  290. "complexType": {
  291. "original": "any",
  292. "resolved": "any",
  293. "references": {}
  294. },
  295. "required": true,
  296. "optional": false,
  297. "docs": {
  298. "tags": [],
  299. "text": "The component's value."
  300. },
  301. "attribute": "value",
  302. "reflect": false
  303. },
  304. "closed": {
  305. "type": "boolean",
  306. "mutable": true,
  307. "complexType": {
  308. "original": "boolean",
  309. "resolved": "boolean",
  310. "references": {}
  311. },
  312. "required": false,
  313. "optional": false,
  314. "docs": {
  315. "tags": [],
  316. "text": "When `true`, hides the component."
  317. },
  318. "attribute": "closed",
  319. "reflect": true,
  320. "defaultValue": "false"
  321. }
  322. };
  323. }
  324. static get events() {
  325. return [{
  326. "method": "calciteChipDismiss",
  327. "name": "calciteChipDismiss",
  328. "bubbles": true,
  329. "cancelable": false,
  330. "composed": true,
  331. "docs": {
  332. "tags": [],
  333. "text": "Fires when the dismiss button is clicked.\n\n**Note:**: The `el` event payload props is deprecated, please use the event's `target`/`currentTarget` instead."
  334. },
  335. "complexType": {
  336. "original": "DeprecatedEventPayload",
  337. "resolved": "any",
  338. "references": {
  339. "DeprecatedEventPayload": {
  340. "location": "import",
  341. "path": "../interfaces"
  342. }
  343. }
  344. }
  345. }];
  346. }
  347. static get methods() {
  348. return {
  349. "setFocus": {
  350. "complexType": {
  351. "signature": "() => Promise<void>",
  352. "parameters": [],
  353. "references": {
  354. "Promise": {
  355. "location": "global"
  356. }
  357. },
  358. "return": "Promise<void>"
  359. },
  360. "docs": {
  361. "text": "Sets focus on the component.",
  362. "tags": []
  363. }
  364. }
  365. };
  366. }
  367. static get elementRef() { return "el"; }
  368. static get watchers() {
  369. return [{
  370. "propName": "dismissible",
  371. "methodName": "handleDismissible"
  372. }, {
  373. "propName": "closable",
  374. "methodName": "handleClosable"
  375. }];
  376. }
  377. }