inline-editable.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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, Element, Event, h, Listen, Method, Prop, Watch } from "@stencil/core";
  7. import { getElementProp, getSlotted } from "../../utils/dom";
  8. import { TEXT, CSS } from "./resources";
  9. import { connectLabel, disconnectLabel, getLabelText } from "../../utils/label";
  10. import { createObserver } from "../../utils/observers";
  11. import { updateHostInteraction } from "../../utils/interactive";
  12. /**
  13. * @slot - A slot for adding a `calcite-input`.
  14. */
  15. export class InlineEditable {
  16. constructor() {
  17. //--------------------------------------------------------------------------
  18. //
  19. // Props
  20. //
  21. //--------------------------------------------------------------------------
  22. /** specify whether editing can be enabled */
  23. this.disabled = false;
  24. /** specify whether the wrapped input element is editable, defaults to false */
  25. this.editingEnabled = false;
  26. /** specify whether the confirm button should display a loading state, defaults to false */
  27. this.loading = false;
  28. /** specify whether save/cancel controls should be displayed when editingEnabled is true, defaults to false */
  29. this.controls = false;
  30. /** specify text to be user for the enable editing button's aria-label, defaults to `Click to edit`
  31. * @default "Click to edit"
  32. */
  33. this.intlEnableEditing = TEXT.intlEnablingEditing;
  34. /** specify text to be user for the cancel editing button's aria-label, defaults to `Cancel`
  35. * @default "Cancel"
  36. */
  37. this.intlCancelEditing = TEXT.intlCancelEditing;
  38. /** specify text to be user for the confirm changes button's aria-label, defaults to `Save`
  39. * @default "Save"
  40. */
  41. this.intlConfirmChanges = TEXT.intlConfirmChanges;
  42. this.mutationObserver = createObserver("mutation", () => this.mutationObserverCallback());
  43. this.enableEditing = () => {
  44. var _a, _b;
  45. this.valuePriorToEditing = (_a = this.inputElement) === null || _a === void 0 ? void 0 : _a.value;
  46. this.editingEnabled = true;
  47. (_b = this.inputElement) === null || _b === void 0 ? void 0 : _b.setFocus();
  48. this.calciteInlineEditableEnableEditingChange.emit();
  49. };
  50. this.disableEditing = () => {
  51. this.editingEnabled = false;
  52. };
  53. this.cancelEditing = () => {
  54. if (this.inputElement) {
  55. this.inputElement.value = this.valuePriorToEditing;
  56. }
  57. this.disableEditing();
  58. this.enableEditingButton.setFocus();
  59. if (!this.editingEnabled && !!this.shouldEmitCancel) {
  60. this.calciteInlineEditableEditCancel.emit();
  61. }
  62. };
  63. this.escapeKeyHandler = async (e) => {
  64. var _a;
  65. if (e.key !== "Escape") {
  66. if (e.key === "Tab" && this.shouldShowControls) {
  67. if (!e.shiftKey && e.target === this.inputElement) {
  68. e.preventDefault();
  69. this.cancelEditingButton.setFocus();
  70. }
  71. if (!!e.shiftKey && e.target === this.cancelEditingButton) {
  72. e.preventDefault();
  73. (_a = this.inputElement) === null || _a === void 0 ? void 0 : _a.setFocus();
  74. }
  75. }
  76. return;
  77. }
  78. this.cancelEditing();
  79. };
  80. this.cancelEditingHandler = async (e) => {
  81. e.preventDefault();
  82. this.cancelEditing();
  83. };
  84. this.enableEditingHandler = async (e) => {
  85. if (this.disabled ||
  86. e.target === this.cancelEditingButton ||
  87. e.target === this.confirmEditingButton) {
  88. return;
  89. }
  90. e.preventDefault();
  91. if (!this.editingEnabled) {
  92. this.enableEditing();
  93. }
  94. };
  95. this.confirmChangesHandler = async (e) => {
  96. e.preventDefault();
  97. this.calciteInlineEditableEditConfirm.emit();
  98. try {
  99. if (this.afterConfirm) {
  100. this.loading = true;
  101. await this.afterConfirm();
  102. this.disableEditing();
  103. this.enableEditingButton.setFocus();
  104. }
  105. }
  106. catch (e) {
  107. }
  108. finally {
  109. this.loading = false;
  110. }
  111. };
  112. }
  113. disabledWatcher(disabled) {
  114. if (this.inputElement) {
  115. this.inputElement.disabled = disabled;
  116. }
  117. }
  118. editingEnabledWatcher(newValue, oldValue) {
  119. if (this.inputElement) {
  120. this.inputElement.editingEnabled = newValue;
  121. }
  122. if (!newValue && !!oldValue) {
  123. this.shouldEmitCancel = true;
  124. }
  125. }
  126. //--------------------------------------------------------------------------
  127. //
  128. // Lifecycle
  129. //
  130. //--------------------------------------------------------------------------
  131. connectedCallback() {
  132. var _a;
  133. connectLabel(this);
  134. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.observe(this.el, { childList: true });
  135. this.mutationObserverCallback();
  136. }
  137. disconnectedCallback() {
  138. var _a;
  139. disconnectLabel(this);
  140. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
  141. }
  142. componentDidRender() {
  143. updateHostInteraction(this);
  144. }
  145. render() {
  146. return (h("div", { class: CSS.wrapper, onClick: this.enableEditingHandler, onKeyDown: this.escapeKeyHandler },
  147. h("div", { class: CSS.inputWrapper },
  148. h("slot", null)),
  149. h("div", { class: CSS.controlsWrapper },
  150. h("calcite-button", { appearance: "transparent", class: CSS.enableEditingButton, color: "neutral", disabled: this.disabled, iconStart: "pencil", label: this.intlEnableEditing, onClick: this.enableEditingHandler, ref: (el) => (this.enableEditingButton = el), scale: this.scale, style: {
  151. opacity: this.editingEnabled ? "0" : "1",
  152. width: this.editingEnabled ? "0" : "inherit"
  153. }, type: "button" }),
  154. this.shouldShowControls && [
  155. h("div", { class: CSS.cancelEditingButtonWrapper },
  156. h("calcite-button", { appearance: "transparent", class: CSS.cancelEditingButton, color: "neutral", disabled: this.disabled, iconStart: "x", label: this.intlCancelEditing, onClick: this.cancelEditingHandler, ref: (el) => (this.cancelEditingButton = el), scale: this.scale, type: "button" })),
  157. h("calcite-button", { appearance: "solid", class: CSS.confirmChangesButton, color: "blue", disabled: this.disabled, iconStart: "check", label: this.intlConfirmChanges, loading: this.loading, onClick: this.confirmChangesHandler, ref: (el) => (this.confirmEditingButton = el), scale: this.scale, type: "button" })
  158. ])));
  159. }
  160. //--------------------------------------------------------------------------
  161. //
  162. // Event Listeners
  163. //
  164. //--------------------------------------------------------------------------
  165. blurHandler() {
  166. if (!this.controls) {
  167. this.disableEditing();
  168. }
  169. }
  170. //--------------------------------------------------------------------------
  171. //
  172. // Public Methods
  173. //
  174. //--------------------------------------------------------------------------
  175. async setFocus() {
  176. var _a, _b;
  177. if (this.editingEnabled) {
  178. (_a = this.inputElement) === null || _a === void 0 ? void 0 : _a.setFocus();
  179. }
  180. else {
  181. (_b = this.enableEditingButton) === null || _b === void 0 ? void 0 : _b.setFocus();
  182. }
  183. }
  184. //--------------------------------------------------------------------------
  185. //
  186. // Private Methods
  187. //
  188. //--------------------------------------------------------------------------
  189. mutationObserverCallback() {
  190. var _a;
  191. this.updateSlottedInput();
  192. this.scale =
  193. this.scale || ((_a = this.inputElement) === null || _a === void 0 ? void 0 : _a.scale) || getElementProp(this.el, "scale", undefined);
  194. }
  195. onLabelClick() {
  196. this.setFocus();
  197. }
  198. updateSlottedInput() {
  199. const inputElement = getSlotted(this.el, {
  200. matches: "calcite-input"
  201. });
  202. this.inputElement = inputElement;
  203. if (!inputElement) {
  204. return;
  205. }
  206. this.inputElement.disabled = this.disabled;
  207. this.inputElement.label = this.inputElement.label || getLabelText(this);
  208. }
  209. get shouldShowControls() {
  210. return this.editingEnabled && this.controls;
  211. }
  212. static get is() { return "calcite-inline-editable"; }
  213. static get encapsulation() { return "shadow"; }
  214. static get originalStyleUrls() { return {
  215. "$": ["inline-editable.scss"]
  216. }; }
  217. static get styleUrls() { return {
  218. "$": ["inline-editable.css"]
  219. }; }
  220. static get properties() { return {
  221. "disabled": {
  222. "type": "boolean",
  223. "mutable": false,
  224. "complexType": {
  225. "original": "boolean",
  226. "resolved": "boolean",
  227. "references": {}
  228. },
  229. "required": false,
  230. "optional": false,
  231. "docs": {
  232. "tags": [],
  233. "text": "specify whether editing can be enabled"
  234. },
  235. "attribute": "disabled",
  236. "reflect": true,
  237. "defaultValue": "false"
  238. },
  239. "editingEnabled": {
  240. "type": "boolean",
  241. "mutable": true,
  242. "complexType": {
  243. "original": "boolean",
  244. "resolved": "boolean",
  245. "references": {}
  246. },
  247. "required": false,
  248. "optional": false,
  249. "docs": {
  250. "tags": [],
  251. "text": "specify whether the wrapped input element is editable, defaults to false"
  252. },
  253. "attribute": "editing-enabled",
  254. "reflect": true,
  255. "defaultValue": "false"
  256. },
  257. "loading": {
  258. "type": "boolean",
  259. "mutable": true,
  260. "complexType": {
  261. "original": "boolean",
  262. "resolved": "boolean",
  263. "references": {}
  264. },
  265. "required": false,
  266. "optional": false,
  267. "docs": {
  268. "tags": [],
  269. "text": "specify whether the confirm button should display a loading state, defaults to false"
  270. },
  271. "attribute": "loading",
  272. "reflect": true,
  273. "defaultValue": "false"
  274. },
  275. "controls": {
  276. "type": "boolean",
  277. "mutable": false,
  278. "complexType": {
  279. "original": "boolean",
  280. "resolved": "boolean",
  281. "references": {}
  282. },
  283. "required": false,
  284. "optional": false,
  285. "docs": {
  286. "tags": [],
  287. "text": "specify whether save/cancel controls should be displayed when editingEnabled is true, defaults to false"
  288. },
  289. "attribute": "controls",
  290. "reflect": true,
  291. "defaultValue": "false"
  292. },
  293. "intlEnableEditing": {
  294. "type": "string",
  295. "mutable": false,
  296. "complexType": {
  297. "original": "string",
  298. "resolved": "string",
  299. "references": {}
  300. },
  301. "required": false,
  302. "optional": false,
  303. "docs": {
  304. "tags": [{
  305. "name": "default",
  306. "text": "\"Click to edit\""
  307. }],
  308. "text": "specify text to be user for the enable editing button's aria-label, defaults to `Click to edit`"
  309. },
  310. "attribute": "intl-enable-editing",
  311. "reflect": true,
  312. "defaultValue": "TEXT.intlEnablingEditing"
  313. },
  314. "intlCancelEditing": {
  315. "type": "string",
  316. "mutable": false,
  317. "complexType": {
  318. "original": "string",
  319. "resolved": "string",
  320. "references": {}
  321. },
  322. "required": false,
  323. "optional": false,
  324. "docs": {
  325. "tags": [{
  326. "name": "default",
  327. "text": "\"Cancel\""
  328. }],
  329. "text": "specify text to be user for the cancel editing button's aria-label, defaults to `Cancel`"
  330. },
  331. "attribute": "intl-cancel-editing",
  332. "reflect": true,
  333. "defaultValue": "TEXT.intlCancelEditing"
  334. },
  335. "intlConfirmChanges": {
  336. "type": "string",
  337. "mutable": false,
  338. "complexType": {
  339. "original": "string",
  340. "resolved": "string",
  341. "references": {}
  342. },
  343. "required": false,
  344. "optional": false,
  345. "docs": {
  346. "tags": [{
  347. "name": "default",
  348. "text": "\"Save\""
  349. }],
  350. "text": "specify text to be user for the confirm changes button's aria-label, defaults to `Save`"
  351. },
  352. "attribute": "intl-confirm-changes",
  353. "reflect": true,
  354. "defaultValue": "TEXT.intlConfirmChanges"
  355. },
  356. "scale": {
  357. "type": "string",
  358. "mutable": true,
  359. "complexType": {
  360. "original": "Scale",
  361. "resolved": "\"l\" | \"m\" | \"s\"",
  362. "references": {
  363. "Scale": {
  364. "location": "import",
  365. "path": "../interfaces"
  366. }
  367. }
  368. },
  369. "required": false,
  370. "optional": true,
  371. "docs": {
  372. "tags": [],
  373. "text": "specify the scale of the inline-editable component, defaults to the scale of the wrapped calcite-input or the scale of the closest wrapping component with a set scale"
  374. },
  375. "attribute": "scale",
  376. "reflect": true
  377. },
  378. "afterConfirm": {
  379. "type": "unknown",
  380. "mutable": false,
  381. "complexType": {
  382. "original": "() => Promise<void>",
  383. "resolved": "() => Promise<void>",
  384. "references": {
  385. "Promise": {
  386. "location": "global"
  387. }
  388. }
  389. },
  390. "required": false,
  391. "optional": true,
  392. "docs": {
  393. "tags": [],
  394. "text": "when controls, specify a callback to be executed prior to disabling editing. when provided, loading state will be handled automatically."
  395. }
  396. }
  397. }; }
  398. static get events() { return [{
  399. "method": "calciteInlineEditableEditCancel",
  400. "name": "calciteInlineEditableEditCancel",
  401. "bubbles": true,
  402. "cancelable": true,
  403. "composed": true,
  404. "docs": {
  405. "tags": [],
  406. "text": "Emitted when the cancel button gets clicked."
  407. },
  408. "complexType": {
  409. "original": "void",
  410. "resolved": "void",
  411. "references": {}
  412. }
  413. }, {
  414. "method": "calciteInlineEditableEditConfirm",
  415. "name": "calciteInlineEditableEditConfirm",
  416. "bubbles": true,
  417. "cancelable": true,
  418. "composed": true,
  419. "docs": {
  420. "tags": [],
  421. "text": "Emitted when the check button gets clicked."
  422. },
  423. "complexType": {
  424. "original": "void",
  425. "resolved": "void",
  426. "references": {}
  427. }
  428. }, {
  429. "method": "calciteInlineEditableEnableEditingChange",
  430. "name": "calciteInlineEditableEnableEditingChange",
  431. "bubbles": true,
  432. "cancelable": true,
  433. "composed": true,
  434. "docs": {
  435. "tags": [{
  436. "name": "internal",
  437. "text": undefined
  438. }],
  439. "text": ""
  440. },
  441. "complexType": {
  442. "original": "any",
  443. "resolved": "any",
  444. "references": {}
  445. }
  446. }]; }
  447. static get methods() { return {
  448. "setFocus": {
  449. "complexType": {
  450. "signature": "() => Promise<void>",
  451. "parameters": [],
  452. "references": {
  453. "Promise": {
  454. "location": "global"
  455. }
  456. },
  457. "return": "Promise<void>"
  458. },
  459. "docs": {
  460. "text": "",
  461. "tags": []
  462. }
  463. }
  464. }; }
  465. static get elementRef() { return "el"; }
  466. static get watchers() { return [{
  467. "propName": "disabled",
  468. "methodName": "disabledWatcher"
  469. }, {
  470. "propName": "editingEnabled",
  471. "methodName": "editingEnabledWatcher"
  472. }]; }
  473. static get listeners() { return [{
  474. "name": "calciteInputBlur",
  475. "method": "blurHandler",
  476. "target": undefined,
  477. "capture": false,
  478. "passive": false
  479. }]; }
  480. }