chip.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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.82
  5. */
  6. import { Component, h, Prop, Event, Element, Method } 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. /** specify the appearance style of the button, defaults to solid. */
  23. this.appearance = "solid";
  24. /** specify the color of the button, defaults to blue */
  25. this.color = "grey";
  26. /** Optionally show a button the user can click to dismiss the chip */
  27. this.dismissible = false;
  28. /** Aria label for the "x" button
  29. * @default "Close"
  30. */
  31. this.dismissLabel = TEXT.close;
  32. /** flip the icon in rtl */
  33. this.iconFlipRtl = false;
  34. /** specify the scale of the chip, defaults to m */
  35. this.scale = "m";
  36. // --------------------------------------------------------------------------
  37. //
  38. // Private Methods
  39. //
  40. // --------------------------------------------------------------------------
  41. this.closeClickHandler = (event) => {
  42. event.preventDefault();
  43. this.calciteChipDismiss.emit(this.el);
  44. };
  45. this.guid = guid();
  46. }
  47. // --------------------------------------------------------------------------
  48. //
  49. // Lifecycle
  50. //
  51. // --------------------------------------------------------------------------
  52. connectedCallback() {
  53. connectConditionalSlotComponent(this);
  54. }
  55. disconnectedCallback() {
  56. disconnectConditionalSlotComponent(this);
  57. }
  58. //--------------------------------------------------------------------------
  59. //
  60. // Public Methods
  61. //
  62. //--------------------------------------------------------------------------
  63. /** Sets focus on the component. */
  64. async setFocus() {
  65. var _a;
  66. (_a = this.closeButton) === null || _a === void 0 ? void 0 : _a.focus();
  67. }
  68. //--------------------------------------------------------------------------
  69. //
  70. // Render Methods
  71. //
  72. //--------------------------------------------------------------------------
  73. renderChipImage() {
  74. const { el } = this;
  75. const hasChipImage = getSlotted(el, SLOTS.image);
  76. return hasChipImage ? (h("div", { class: CSS.imageContainer, key: "image" },
  77. h("slot", { name: SLOTS.image }))) : null;
  78. }
  79. render() {
  80. const iconEl = (h("calcite-icon", { class: CSS.chipIcon, flipRtl: this.iconFlipRtl, icon: this.icon, scale: "s" }));
  81. const closeButton = (h("button", { "aria-describedby": this.guid, "aria-label": this.dismissLabel, class: CSS.close, onClick: this.closeClickHandler, ref: (el) => (this.closeButton = el) },
  82. h("calcite-icon", { class: CSS.closeIcon, icon: ICONS.close, scale: "s" })));
  83. return (h("div", { class: "container" },
  84. this.renderChipImage(),
  85. this.icon ? iconEl : null,
  86. h("span", { class: CSS.title, id: this.guid },
  87. h("slot", null)),
  88. this.dismissible ? closeButton : null));
  89. }
  90. static get is() { return "calcite-chip"; }
  91. static get encapsulation() { return "shadow"; }
  92. static get originalStyleUrls() { return {
  93. "$": ["chip.scss"]
  94. }; }
  95. static get styleUrls() { return {
  96. "$": ["chip.css"]
  97. }; }
  98. static get properties() { return {
  99. "appearance": {
  100. "type": "string",
  101. "mutable": false,
  102. "complexType": {
  103. "original": "Extract<\"solid\" | \"clear\", Appearance>",
  104. "resolved": "\"clear\" | \"solid\"",
  105. "references": {
  106. "Extract": {
  107. "location": "global"
  108. },
  109. "Appearance": {
  110. "location": "import",
  111. "path": "../interfaces"
  112. }
  113. }
  114. },
  115. "required": false,
  116. "optional": false,
  117. "docs": {
  118. "tags": [],
  119. "text": "specify the appearance style of the button, defaults to solid."
  120. },
  121. "attribute": "appearance",
  122. "reflect": true,
  123. "defaultValue": "\"solid\""
  124. },
  125. "color": {
  126. "type": "string",
  127. "mutable": false,
  128. "complexType": {
  129. "original": "ChipColor",
  130. "resolved": "\"blue\" | \"green\" | \"grey\" | \"red\" | \"yellow\"",
  131. "references": {
  132. "ChipColor": {
  133. "location": "import",
  134. "path": "./interfaces"
  135. }
  136. }
  137. },
  138. "required": false,
  139. "optional": false,
  140. "docs": {
  141. "tags": [],
  142. "text": "specify the color of the button, defaults to blue"
  143. },
  144. "attribute": "color",
  145. "reflect": true,
  146. "defaultValue": "\"grey\""
  147. },
  148. "dismissible": {
  149. "type": "boolean",
  150. "mutable": false,
  151. "complexType": {
  152. "original": "boolean",
  153. "resolved": "boolean",
  154. "references": {}
  155. },
  156. "required": false,
  157. "optional": false,
  158. "docs": {
  159. "tags": [],
  160. "text": "Optionally show a button the user can click to dismiss the chip"
  161. },
  162. "attribute": "dismissible",
  163. "reflect": true,
  164. "defaultValue": "false"
  165. },
  166. "dismissLabel": {
  167. "type": "string",
  168. "mutable": false,
  169. "complexType": {
  170. "original": "string",
  171. "resolved": "string",
  172. "references": {}
  173. },
  174. "required": false,
  175. "optional": true,
  176. "docs": {
  177. "tags": [{
  178. "name": "default",
  179. "text": "\"Close\""
  180. }],
  181. "text": "Aria label for the \"x\" button"
  182. },
  183. "attribute": "dismiss-label",
  184. "reflect": false,
  185. "defaultValue": "TEXT.close"
  186. },
  187. "icon": {
  188. "type": "string",
  189. "mutable": false,
  190. "complexType": {
  191. "original": "string",
  192. "resolved": "string",
  193. "references": {}
  194. },
  195. "required": false,
  196. "optional": true,
  197. "docs": {
  198. "tags": [],
  199. "text": "optionally pass an icon to display - accepts Calcite UI icon names"
  200. },
  201. "attribute": "icon",
  202. "reflect": true
  203. },
  204. "iconFlipRtl": {
  205. "type": "boolean",
  206. "mutable": false,
  207. "complexType": {
  208. "original": "boolean",
  209. "resolved": "boolean",
  210. "references": {}
  211. },
  212. "required": false,
  213. "optional": false,
  214. "docs": {
  215. "tags": [],
  216. "text": "flip the icon in rtl"
  217. },
  218. "attribute": "icon-flip-rtl",
  219. "reflect": true,
  220. "defaultValue": "false"
  221. },
  222. "scale": {
  223. "type": "string",
  224. "mutable": false,
  225. "complexType": {
  226. "original": "Scale",
  227. "resolved": "\"l\" | \"m\" | \"s\"",
  228. "references": {
  229. "Scale": {
  230. "location": "import",
  231. "path": "../interfaces"
  232. }
  233. }
  234. },
  235. "required": false,
  236. "optional": false,
  237. "docs": {
  238. "tags": [],
  239. "text": "specify the scale of the chip, defaults to m"
  240. },
  241. "attribute": "scale",
  242. "reflect": true,
  243. "defaultValue": "\"m\""
  244. },
  245. "value": {
  246. "type": "any",
  247. "mutable": false,
  248. "complexType": {
  249. "original": "any",
  250. "resolved": "any",
  251. "references": {}
  252. },
  253. "required": true,
  254. "optional": false,
  255. "docs": {
  256. "tags": [],
  257. "text": "The assigned value for the chip"
  258. },
  259. "attribute": "value",
  260. "reflect": false
  261. }
  262. }; }
  263. static get events() { return [{
  264. "method": "calciteChipDismiss",
  265. "name": "calciteChipDismiss",
  266. "bubbles": true,
  267. "cancelable": true,
  268. "composed": true,
  269. "docs": {
  270. "tags": [],
  271. "text": "Emitted when the dismiss button is clicked"
  272. },
  273. "complexType": {
  274. "original": "any",
  275. "resolved": "any",
  276. "references": {}
  277. }
  278. }]; }
  279. static get methods() { return {
  280. "setFocus": {
  281. "complexType": {
  282. "signature": "() => Promise<void>",
  283. "parameters": [],
  284. "references": {
  285. "Promise": {
  286. "location": "global"
  287. }
  288. },
  289. "return": "Promise<void>"
  290. },
  291. "docs": {
  292. "text": "Sets focus on the component.",
  293. "tags": []
  294. }
  295. }
  296. }; }
  297. static get elementRef() { return "el"; }
  298. }