inline-editable.js 16 KB

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