calcite-inline-editable.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 { proxyCustomElement, HTMLElement, createEvent, h } from '@stencil/core/internal/client';
  7. import { g as getElementProp, b as getSlotted } from './dom.js';
  8. import { c as connectLabel, d as disconnectLabel, g as getLabelText } from './label.js';
  9. import { c as createObserver } from './observers.js';
  10. import { u as updateHostInteraction } from './interactive.js';
  11. import { d as defineCustomElement$4 } from './button.js';
  12. import { d as defineCustomElement$3 } from './icon.js';
  13. import { d as defineCustomElement$2 } from './loader.js';
  14. const CSS = {
  15. wrapper: "wrapper",
  16. confirmChangesButton: "confirm-changes-button",
  17. cancelEditingButton: "cancel-editing-button",
  18. inputWrapper: "input-wrapper",
  19. cancelEditingButtonWrapper: "cancel-editing-button-wrapper",
  20. enableEditingButton: "enable-editing-button",
  21. controlsWrapper: "controls-wrapper"
  22. };
  23. const TEXT = {
  24. intlEnablingEditing: "Click to edit",
  25. intlCancelEditing: "Cancel",
  26. intlConfirmChanges: "Save"
  27. };
  28. 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}";
  29. const InlineEditable = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  30. constructor() {
  31. super();
  32. this.__registerHost();
  33. this.__attachShadow();
  34. this.calciteInlineEditableEditCancel = createEvent(this, "calciteInlineEditableEditCancel", 7);
  35. this.calciteInlineEditableEditConfirm = createEvent(this, "calciteInlineEditableEditConfirm", 7);
  36. this.calciteInlineEditableEnableEditingChange = createEvent(this, "calciteInlineEditableEnableEditingChange", 7);
  37. //--------------------------------------------------------------------------
  38. //
  39. // Props
  40. //
  41. //--------------------------------------------------------------------------
  42. /** specify whether editing can be enabled */
  43. this.disabled = false;
  44. /** specify whether the wrapped input element is editable, defaults to false */
  45. this.editingEnabled = false;
  46. /** specify whether the confirm button should display a loading state, defaults to false */
  47. this.loading = false;
  48. /** specify whether save/cancel controls should be displayed when editingEnabled is true, defaults to false */
  49. this.controls = false;
  50. /** specify text to be user for the enable editing button's aria-label, defaults to `Click to edit`
  51. * @default "Click to edit"
  52. */
  53. this.intlEnableEditing = TEXT.intlEnablingEditing;
  54. /** specify text to be user for the cancel editing button's aria-label, defaults to `Cancel`
  55. * @default "Cancel"
  56. */
  57. this.intlCancelEditing = TEXT.intlCancelEditing;
  58. /** specify text to be user for the confirm changes button's aria-label, defaults to `Save`
  59. * @default "Save"
  60. */
  61. this.intlConfirmChanges = TEXT.intlConfirmChanges;
  62. this.mutationObserver = createObserver("mutation", () => this.mutationObserverCallback());
  63. this.enableEditing = () => {
  64. var _a, _b;
  65. this.valuePriorToEditing = (_a = this.inputElement) === null || _a === void 0 ? void 0 : _a.value;
  66. this.editingEnabled = true;
  67. (_b = this.inputElement) === null || _b === void 0 ? void 0 : _b.setFocus();
  68. this.calciteInlineEditableEnableEditingChange.emit();
  69. };
  70. this.disableEditing = () => {
  71. this.editingEnabled = false;
  72. };
  73. this.cancelEditing = () => {
  74. if (this.inputElement) {
  75. this.inputElement.value = this.valuePriorToEditing;
  76. }
  77. this.disableEditing();
  78. this.enableEditingButton.setFocus();
  79. if (!this.editingEnabled && !!this.shouldEmitCancel) {
  80. this.calciteInlineEditableEditCancel.emit();
  81. }
  82. };
  83. this.escapeKeyHandler = async (e) => {
  84. var _a;
  85. if (e.key !== "Escape") {
  86. if (e.key === "Tab" && this.shouldShowControls) {
  87. if (!e.shiftKey && e.target === this.inputElement) {
  88. e.preventDefault();
  89. this.cancelEditingButton.setFocus();
  90. }
  91. if (!!e.shiftKey && e.target === this.cancelEditingButton) {
  92. e.preventDefault();
  93. (_a = this.inputElement) === null || _a === void 0 ? void 0 : _a.setFocus();
  94. }
  95. }
  96. return;
  97. }
  98. this.cancelEditing();
  99. };
  100. this.cancelEditingHandler = async (e) => {
  101. e.preventDefault();
  102. this.cancelEditing();
  103. };
  104. this.enableEditingHandler = async (e) => {
  105. if (this.disabled ||
  106. e.target === this.cancelEditingButton ||
  107. e.target === this.confirmEditingButton) {
  108. return;
  109. }
  110. e.preventDefault();
  111. if (!this.editingEnabled) {
  112. this.enableEditing();
  113. }
  114. };
  115. this.confirmChangesHandler = async (e) => {
  116. e.preventDefault();
  117. this.calciteInlineEditableEditConfirm.emit();
  118. try {
  119. if (this.afterConfirm) {
  120. this.loading = true;
  121. await this.afterConfirm();
  122. this.disableEditing();
  123. this.enableEditingButton.setFocus();
  124. }
  125. }
  126. catch (e) {
  127. }
  128. finally {
  129. this.loading = false;
  130. }
  131. };
  132. }
  133. disabledWatcher(disabled) {
  134. if (this.inputElement) {
  135. this.inputElement.disabled = disabled;
  136. }
  137. }
  138. editingEnabledWatcher(newValue, oldValue) {
  139. if (this.inputElement) {
  140. this.inputElement.editingEnabled = newValue;
  141. }
  142. if (!newValue && !!oldValue) {
  143. this.shouldEmitCancel = true;
  144. }
  145. }
  146. //--------------------------------------------------------------------------
  147. //
  148. // Lifecycle
  149. //
  150. //--------------------------------------------------------------------------
  151. connectedCallback() {
  152. var _a;
  153. connectLabel(this);
  154. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.observe(this.el, { childList: true });
  155. this.mutationObserverCallback();
  156. }
  157. disconnectedCallback() {
  158. var _a;
  159. disconnectLabel(this);
  160. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
  161. }
  162. componentDidRender() {
  163. updateHostInteraction(this);
  164. }
  165. render() {
  166. 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: {
  167. opacity: this.editingEnabled ? "0" : "1",
  168. width: this.editingEnabled ? "0" : "inherit"
  169. }, type: "button" }), this.shouldShowControls && [
  170. 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" })),
  171. 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" })
  172. ])));
  173. }
  174. //--------------------------------------------------------------------------
  175. //
  176. // Event Listeners
  177. //
  178. //--------------------------------------------------------------------------
  179. blurHandler() {
  180. if (!this.controls) {
  181. this.disableEditing();
  182. }
  183. }
  184. //--------------------------------------------------------------------------
  185. //
  186. // Public Methods
  187. //
  188. //--------------------------------------------------------------------------
  189. async setFocus() {
  190. var _a, _b;
  191. if (this.editingEnabled) {
  192. (_a = this.inputElement) === null || _a === void 0 ? void 0 : _a.setFocus();
  193. }
  194. else {
  195. (_b = this.enableEditingButton) === null || _b === void 0 ? void 0 : _b.setFocus();
  196. }
  197. }
  198. //--------------------------------------------------------------------------
  199. //
  200. // Private Methods
  201. //
  202. //--------------------------------------------------------------------------
  203. mutationObserverCallback() {
  204. var _a;
  205. this.updateSlottedInput();
  206. this.scale =
  207. this.scale || ((_a = this.inputElement) === null || _a === void 0 ? void 0 : _a.scale) || getElementProp(this.el, "scale", undefined);
  208. }
  209. onLabelClick() {
  210. this.setFocus();
  211. }
  212. updateSlottedInput() {
  213. const inputElement = getSlotted(this.el, {
  214. matches: "calcite-input"
  215. });
  216. this.inputElement = inputElement;
  217. if (!inputElement) {
  218. return;
  219. }
  220. this.inputElement.disabled = this.disabled;
  221. this.inputElement.label = this.inputElement.label || getLabelText(this);
  222. }
  223. get shouldShowControls() {
  224. return this.editingEnabled && this.controls;
  225. }
  226. get el() { return this; }
  227. static get watchers() { return {
  228. "disabled": ["disabledWatcher"],
  229. "editingEnabled": ["editingEnabledWatcher"]
  230. }; }
  231. static get style() { return inlineEditableCss; }
  232. }, [1, "calcite-inline-editable", {
  233. "disabled": [516],
  234. "editingEnabled": [1540, "editing-enabled"],
  235. "loading": [1540],
  236. "controls": [516],
  237. "intlEnableEditing": [513, "intl-enable-editing"],
  238. "intlCancelEditing": [513, "intl-cancel-editing"],
  239. "intlConfirmChanges": [513, "intl-confirm-changes"],
  240. "scale": [1537],
  241. "afterConfirm": [16],
  242. "setFocus": [64]
  243. }, [[0, "calciteInputBlur", "blurHandler"]]]);
  244. function defineCustomElement$1() {
  245. if (typeof customElements === "undefined") {
  246. return;
  247. }
  248. const components = ["calcite-inline-editable", "calcite-button", "calcite-icon", "calcite-loader"];
  249. components.forEach(tagName => { switch (tagName) {
  250. case "calcite-inline-editable":
  251. if (!customElements.get(tagName)) {
  252. customElements.define(tagName, InlineEditable);
  253. }
  254. break;
  255. case "calcite-button":
  256. if (!customElements.get(tagName)) {
  257. defineCustomElement$4();
  258. }
  259. break;
  260. case "calcite-icon":
  261. if (!customElements.get(tagName)) {
  262. defineCustomElement$3();
  263. }
  264. break;
  265. case "calcite-loader":
  266. if (!customElements.get(tagName)) {
  267. defineCustomElement$2();
  268. }
  269. break;
  270. } });
  271. }
  272. defineCustomElement$1();
  273. const CalciteInlineEditable = InlineEditable;
  274. const defineCustomElement = defineCustomElement$1;
  275. export { CalciteInlineEditable, defineCustomElement };