calcite-inline-editable.entry.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 { r as registerInstance, c as createEvent, h, g as getElement } from './index-1f9b54dc.js';
  7. import { e as getElementProp, g as getSlotted } from './dom-8f0a9ff2.js';
  8. import { c as connectLabel, d as disconnectLabel, g as getLabelText } from './label-333c4a4c.js';
  9. import { c as createObserver } from './observers-9f44e9b3.js';
  10. import { u as updateHostInteraction } from './interactive-5db230e8.js';
  11. import './resources-9c476cb6.js';
  12. import './guid-9f15e57a.js';
  13. const CSS = {
  14. wrapper: "wrapper",
  15. confirmChangesButton: "confirm-changes-button",
  16. cancelEditingButton: "cancel-editing-button",
  17. inputWrapper: "input-wrapper",
  18. cancelEditingButtonWrapper: "cancel-editing-button-wrapper",
  19. enableEditingButton: "enable-editing-button",
  20. controlsWrapper: "controls-wrapper"
  21. };
  22. const TEXT = {
  23. intlEnablingEditing: "Click to edit",
  24. intlCancelEditing: "Cancel",
  25. intlConfirmChanges: "Save"
  26. };
  27. const inlineEditableCss = "@keyframes in{0%{opacity:0}100%{opacity:1}}@keyframes in-down{0%{opacity:0;transform:translate3D(0, -5px, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-up{0%{opacity:0;transform:translate3D(0, 5px, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-scale{0%{opacity:0;transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;transform:scale3D(1, 1, 1)}}:root{--calcite-animation-timing:calc(150ms * var(--calcite-internal-duration-factor));--calcite-internal-duration-factor:var(--calcite-duration-factor, 1);--calcite-internal-animation-timing-fast:calc(100ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-medium:calc(200ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-slow:calc(300ms * var(--calcite-internal-duration-factor))}.calcite-animate{opacity:0;animation-fill-mode:both;animation-duration:var(--calcite-animation-timing)}.calcite-animate__in{animation-name:in}.calcite-animate__in-down{animation-name:in-down}.calcite-animate__in-up{animation-name:in-up}.calcite-animate__in-scale{animation-name:in-scale}@media (prefers-reduced-motion: reduce){:root{--calcite-internal-duration-factor:0.01}}:root{--calcite-floating-ui-transition:var(--calcite-animation-timing)}:host([hidden]){display:none}:host([disabled]){pointer-events:none;cursor:default;-webkit-user-select:none;user-select:none;opacity:var(--calcite-ui-opacity-disabled)}:host{display:block}:host([scale=s]) .controls-wrapper{block-size:1.5rem}:host([scale=m]) .controls-wrapper{block-size:2rem}:host([scale=l]) .controls-wrapper{block-size:2.75rem}:host(:not([editing-enabled]):not([disabled])) .wrapper:hover{background-color:var(--calcite-ui-foreground-2)}.wrapper{box-sizing:border-box;display:flex;justify-content:space-between;background-color:var(--calcite-ui-foreground-1);transition:all var(--calcite-animation-timing) ease-in-out 0s, outline 0s, outline-offset 0s}.wrapper .input-wrapper{flex:1 1 0%}.controls-wrapper{display:flex}:host([disabled]) .cancel-editing-button-wrapper{border-color:var(--calcite-ui-border-2)}:host([disabled]) ::slotted([calcite-hydrated][disabled]),:host([disabled]) [calcite-hydrated][disabled]{opacity:1}";
  28. const InlineEditable = class {
  29. constructor(hostRef) {
  30. registerInstance(this, hostRef);
  31. this.calciteInlineEditableEditCancel = createEvent(this, "calciteInlineEditableEditCancel", 6);
  32. this.calciteInlineEditableEditConfirm = createEvent(this, "calciteInlineEditableEditConfirm", 6);
  33. this.calciteInternalInlineEditableEnableEditingChange = createEvent(this, "calciteInternalInlineEditableEnableEditingChange", 6);
  34. //--------------------------------------------------------------------------
  35. //
  36. // Props
  37. //
  38. //--------------------------------------------------------------------------
  39. /** specify whether editing can be enabled */
  40. this.disabled = false;
  41. /** specify whether the wrapped input element is editable, defaults to false */
  42. this.editingEnabled = false;
  43. /** specify whether the confirm button should display a loading state, defaults to false */
  44. this.loading = false;
  45. /** specify whether save/cancel controls should be displayed when editingEnabled is true, defaults to false */
  46. this.controls = false;
  47. /**
  48. * specify text to be user for the enable editing button's aria-label, defaults to `Click to edit`
  49. *
  50. * @default "Click to edit"
  51. */
  52. this.intlEnableEditing = TEXT.intlEnablingEditing;
  53. /**
  54. * specify text to be user for the cancel editing button's aria-label, defaults to `Cancel`
  55. *
  56. * @default "Cancel"
  57. */
  58. this.intlCancelEditing = TEXT.intlCancelEditing;
  59. /**
  60. * specify text to be user for the confirm changes button's aria-label, defaults to `Save`
  61. *
  62. * @default "Save"
  63. */
  64. this.intlConfirmChanges = TEXT.intlConfirmChanges;
  65. this.mutationObserver = createObserver("mutation", () => this.mutationObserverCallback());
  66. this.enableEditing = () => {
  67. var _a, _b;
  68. this.valuePriorToEditing = (_a = this.inputElement) === null || _a === void 0 ? void 0 : _a.value;
  69. this.editingEnabled = true;
  70. (_b = this.inputElement) === null || _b === void 0 ? void 0 : _b.setFocus();
  71. this.calciteInternalInlineEditableEnableEditingChange.emit();
  72. };
  73. this.disableEditing = () => {
  74. this.editingEnabled = false;
  75. };
  76. this.cancelEditing = () => {
  77. if (this.inputElement) {
  78. this.inputElement.value = this.valuePriorToEditing;
  79. }
  80. this.disableEditing();
  81. this.enableEditingButton.setFocus();
  82. if (!this.editingEnabled && !!this.shouldEmitCancel) {
  83. this.calciteInlineEditableEditCancel.emit();
  84. }
  85. };
  86. this.escapeKeyHandler = async (event) => {
  87. var _a;
  88. if (event.defaultPrevented) {
  89. return;
  90. }
  91. if (event.key === "Escape") {
  92. event.preventDefault();
  93. this.cancelEditing();
  94. }
  95. if (event.key === "Tab" && this.shouldShowControls) {
  96. if (!event.shiftKey && event.target === this.inputElement) {
  97. event.preventDefault();
  98. this.cancelEditingButton.setFocus();
  99. }
  100. if (!!event.shiftKey && event.target === this.cancelEditingButton) {
  101. event.preventDefault();
  102. (_a = this.inputElement) === null || _a === void 0 ? void 0 : _a.setFocus();
  103. }
  104. }
  105. };
  106. this.cancelEditingHandler = async (event) => {
  107. event.preventDefault();
  108. this.cancelEditing();
  109. };
  110. this.enableEditingHandler = async (event) => {
  111. if (this.disabled ||
  112. event.target === this.cancelEditingButton ||
  113. event.target === this.confirmEditingButton) {
  114. return;
  115. }
  116. event.preventDefault();
  117. if (!this.editingEnabled) {
  118. this.enableEditing();
  119. }
  120. };
  121. this.confirmChangesHandler = async (event) => {
  122. event.preventDefault();
  123. this.calciteInlineEditableEditConfirm.emit();
  124. try {
  125. if (this.afterConfirm) {
  126. this.loading = true;
  127. await this.afterConfirm();
  128. this.disableEditing();
  129. this.enableEditingButton.setFocus();
  130. }
  131. }
  132. catch (error) {
  133. }
  134. finally {
  135. this.loading = false;
  136. }
  137. };
  138. }
  139. disabledWatcher(disabled) {
  140. if (this.inputElement) {
  141. this.inputElement.disabled = disabled;
  142. }
  143. }
  144. editingEnabledWatcher(newValue, oldValue) {
  145. if (this.inputElement) {
  146. this.inputElement.editingEnabled = newValue;
  147. }
  148. if (!newValue && !!oldValue) {
  149. this.shouldEmitCancel = true;
  150. }
  151. }
  152. //--------------------------------------------------------------------------
  153. //
  154. // Lifecycle
  155. //
  156. //--------------------------------------------------------------------------
  157. connectedCallback() {
  158. var _a;
  159. connectLabel(this);
  160. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.observe(this.el, { childList: true });
  161. this.mutationObserverCallback();
  162. }
  163. disconnectedCallback() {
  164. var _a;
  165. disconnectLabel(this);
  166. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
  167. }
  168. componentDidRender() {
  169. updateHostInteraction(this);
  170. }
  171. render() {
  172. 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: {
  173. opacity: this.editingEnabled ? "0" : "1",
  174. width: this.editingEnabled ? "0" : "inherit"
  175. }, type: "button" }), this.shouldShowControls && [
  176. 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" })),
  177. 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" })
  178. ])));
  179. }
  180. //--------------------------------------------------------------------------
  181. //
  182. // Event Listeners
  183. //
  184. //--------------------------------------------------------------------------
  185. blurHandler() {
  186. if (!this.controls) {
  187. this.disableEditing();
  188. }
  189. }
  190. //--------------------------------------------------------------------------
  191. //
  192. // Public Methods
  193. //
  194. //--------------------------------------------------------------------------
  195. async setFocus() {
  196. var _a, _b;
  197. if (this.editingEnabled) {
  198. (_a = this.inputElement) === null || _a === void 0 ? void 0 : _a.setFocus();
  199. }
  200. else {
  201. (_b = this.enableEditingButton) === null || _b === void 0 ? void 0 : _b.setFocus();
  202. }
  203. }
  204. //--------------------------------------------------------------------------
  205. //
  206. // Private Methods
  207. //
  208. //--------------------------------------------------------------------------
  209. mutationObserverCallback() {
  210. var _a;
  211. this.updateSlottedInput();
  212. this.scale =
  213. this.scale || ((_a = this.inputElement) === null || _a === void 0 ? void 0 : _a.scale) || getElementProp(this.el, "scale", undefined);
  214. }
  215. onLabelClick() {
  216. this.setFocus();
  217. }
  218. updateSlottedInput() {
  219. const inputElement = getSlotted(this.el, {
  220. matches: "calcite-input"
  221. });
  222. this.inputElement = inputElement;
  223. if (!inputElement) {
  224. return;
  225. }
  226. this.inputElement.disabled = this.disabled;
  227. this.inputElement.label = this.inputElement.label || getLabelText(this);
  228. }
  229. get shouldShowControls() {
  230. return this.editingEnabled && this.controls;
  231. }
  232. get el() { return getElement(this); }
  233. static get watchers() { return {
  234. "disabled": ["disabledWatcher"],
  235. "editingEnabled": ["editingEnabledWatcher"]
  236. }; }
  237. };
  238. InlineEditable.style = inlineEditableCss;
  239. export { InlineEditable as calcite_inline_editable };