calcite-inline-editable.entry.js 12 KB

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