calcite-inline-editable.js 13 KB

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