calcite-input-text.entry.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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, H as Host, g as getElement } from './index-1f9b54dc.js';
  7. import { g as getSlotted, s as setRequestedIcon, e as getElementProp, a as getElementDir } from './dom-8f0a9ff2.js';
  8. import { c as connectLabel, d as disconnectLabel, g as getLabelText } from './label-333c4a4c.js';
  9. import { s as submitForm, c as connectForm, d as disconnectForm, H as HiddenFormInputSlot } from './form-80dbd90e.js';
  10. import { T as TEXT$1, C as CSS_UTILITY } from './resources-9c476cb6.js';
  11. import { c as createObserver } from './observers-9f44e9b3.js';
  12. import { u as updateHostInteraction } from './interactive-5db230e8.js';
  13. import './guid-9f15e57a.js';
  14. const CSS = {
  15. loader: "loader",
  16. clearButton: "clear-button",
  17. editingEnabled: "editing-enabled",
  18. inlineChild: "inline-child",
  19. inputIcon: "icon",
  20. prefix: "prefix",
  21. suffix: "suffix",
  22. wrapper: "element-wrapper",
  23. inputWrapper: "wrapper",
  24. actionWrapper: "action-wrapper",
  25. resizeIconWrapper: "resize-icon-wrapper"
  26. };
  27. const SLOTS = {
  28. action: "action"
  29. };
  30. const TEXT = {
  31. clear: "Clear value"
  32. };
  33. const inputTextCss = "@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]) input,:host([scale=s]) .prefix,:host([scale=s]) .suffix{block-size:1.5rem;padding-inline:0.5rem;font-size:var(--calcite-font-size--2);line-height:1rem}:host([scale=s]) .action-wrapper calcite-button,:host([scale=s]) .action-wrapper calcite-button button{block-size:1.5rem}:host([scale=s]) .clear-button{min-block-size:1.5rem;min-inline-size:1.5rem}:host([scale=m]) input,:host([scale=m]) .prefix,:host([scale=m]) .suffix{block-size:2rem;padding-inline:0.75rem;font-size:var(--calcite-font-size--1);line-height:1rem}:host([scale=m]) .action-wrapper calcite-button,:host([scale=m]) .action-wrapper calcite-button button{block-size:2rem}:host([scale=m]) .clear-button{min-block-size:2rem;min-inline-size:2rem}:host([scale=l]) input,:host([scale=l]) .prefix,:host([scale=l]) .suffix{block-size:2.75rem;padding-inline:1rem;font-size:var(--calcite-font-size-0);line-height:1.25rem}:host([scale=l]) .action-wrapper calcite-button,:host([scale=l]) .action-wrapper calcite-button button{block-size:2.75rem}:host([scale=l]) .clear-button{min-block-size:2.75rem;min-inline-size:2.75rem}:host([disabled]) ::slotted([calcite-hydrated][disabled]),:host([disabled]) [calcite-hydrated][disabled]{opacity:1}input{transition:var(--calcite-animation-timing), block-size 0, outline-offset 0s;-webkit-appearance:none;position:relative;margin:0px;box-sizing:border-box;display:flex;max-block-size:100%;inline-size:100%;max-inline-size:100%;flex:1 1 0%;border-radius:0px;background-color:var(--calcite-ui-foreground-1);font-family:inherit;font-weight:var(--calcite-font-weight-normal);color:var(--calcite-ui-text-1)}:host input{border-width:1px;border-style:solid;border-color:var(--calcite-ui-border-input);color:var(--calcite-ui-text-1)}:host input::placeholder,:host input:-ms-input-placeholder,:host input::-ms-input-placeholder{font-weight:var(--calcite-font-weight-normal);color:var(--calcite-ui-text-3)}:host input:focus{border-color:var(--calcite-ui-brand);color:var(--calcite-ui-text-1)}:host input[readonly]{background-color:var(--calcite-ui-background);font-weight:var(--calcite-font-weight-medium)}:host input[readonly]:focus{color:var(--calcite-ui-text-1)}:host calcite-icon{color:var(--calcite-ui-text-3)}input{outline-color:transparent}input:focus{outline:2px solid var(--calcite-ui-brand);outline-offset:-2px}:host([status=invalid]) input{border-color:var(--calcite-ui-danger)}:host([status=invalid]) input:focus{outline:2px solid var(--calcite-ui-danger);outline-offset:-2px}:host([scale=s]) .icon{inset-inline-start:0.5rem}:host([scale=m]) .icon{inset-inline-start:0.75rem}:host([scale=l]) .icon{inset-inline-start:1rem}:host([icon][scale=s]) input{padding-inline-start:2rem}:host([icon][scale=m]) input{padding-inline-start:2.5rem}:host([icon][scale=l]) input{padding-inline-start:3rem}.element-wrapper{position:relative;order:3;display:inline-flex;flex:1 1 0%;align-items:center}.icon{pointer-events:none;position:absolute;z-index:1;display:block;transition:all var(--calcite-animation-timing) ease-in-out 0s, outline 0s, outline-offset 0s}input[type=text]::-ms-clear,input[type=text]::-ms-reveal{display:none;block-size:0px;inline-size:0px}.clear-button{pointer-events:initial;order:4;margin:0px;box-sizing:border-box;display:flex;min-block-size:100%;cursor:pointer;align-items:center;justify-content:center;align-self:stretch;border-width:1px;border-style:solid;border-color:var(--calcite-ui-border-input);background-color:var(--calcite-ui-foreground-1);outline-color:transparent;border-inline-start-width:0px}.clear-button:hover{background-color:var(--calcite-ui-foreground-2);transition:all var(--calcite-animation-timing) ease-in-out 0s, outline 0s, outline-offset 0s}.clear-button:hover calcite-icon{color:var(--calcite-ui-text-1);transition:all var(--calcite-animation-timing) ease-in-out 0s, outline 0s, outline-offset 0s}.clear-button:active{background-color:var(--calcite-ui-foreground-3)}.clear-button:active calcite-icon{color:var(--calcite-ui-text-1)}.clear-button:focus{outline:2px solid var(--calcite-ui-brand);outline-offset:-2px}.clear-button:disabled{opacity:var(--calcite-ui-opacity-disabled)}.loader{inset-block-start:1px;inset-inline:1px;pointer-events:none;position:absolute;display:block}.action-wrapper{order:7;display:flex}.prefix,.suffix{box-sizing:border-box;display:flex;block-size:auto;min-block-size:100%;-webkit-user-select:none;user-select:none;align-content:center;align-items:center;overflow-wrap:break-word;border-width:1px;border-style:solid;border-color:var(--calcite-ui-border-input);background-color:var(--calcite-ui-background);font-weight:var(--calcite-font-weight-medium);line-height:1;color:var(--calcite-ui-text-2)}.prefix{order:2;border-inline-end-width:0px}.suffix{order:5;border-inline-start-width:0px}:host([alignment=start]) input{text-align:start}:host([alignment=end]) input{text-align:end}.wrapper{position:relative;display:flex;flex-direction:row;align-items:center}:host(.no-bottom-border) input{border-block-end-width:0px}:host(.border-top-color-one) input{border-block-start-color:var(--calcite-ui-border-1)}.inline-child{background-color:transparent;transition:all var(--calcite-animation-timing) ease-in-out 0s, outline 0s, outline-offset 0s}.inline-child .editing-enabled{background-color:inherit}.inline-child:not(.editing-enabled){display:flex;cursor:pointer;border-color:transparent;padding-inline-start:0}::slotted(input[slot=hidden-form-input]){margin:0 !important;opacity:0 !important;outline:none !important;padding:0 !important;position:absolute !important;inset:0 !important;transform:none !important;-webkit-appearance:none !important;z-index:-1 !important}";
  34. const InputText = class {
  35. constructor(hostRef) {
  36. registerInstance(this, hostRef);
  37. this.calciteInternalInputTextFocus = createEvent(this, "calciteInternalInputTextFocus", 7);
  38. this.calciteInternalInputTextBlur = createEvent(this, "calciteInternalInputTextBlur", 7);
  39. this.calciteInputTextInput = createEvent(this, "calciteInputTextInput", 7);
  40. this.calciteInputTextChange = createEvent(this, "calciteInputTextChange", 7);
  41. //--------------------------------------------------------------------------
  42. //
  43. // Properties
  44. //
  45. //--------------------------------------------------------------------------
  46. /** Specifies the text alignment of the component's value. */
  47. this.alignment = "start";
  48. /**
  49. * When `true`, the component is focused on page load.
  50. *
  51. * @mdn [autofocus](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/autofocus)
  52. */
  53. this.autofocus = false;
  54. /**
  55. * When `true`, a clear button is displayed when the component has a value.
  56. */
  57. this.clearable = false;
  58. /**
  59. * When `true`, interaction is prevented and the component is displayed with lower opacity.
  60. *
  61. * @mdn [disabled](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/disabled)
  62. */
  63. this.disabled = false;
  64. /**
  65. * When `true`, the component will not be visible.
  66. *
  67. * @mdn [hidden](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/hidden)
  68. */
  69. this.hidden = false;
  70. /**
  71. * Accessible name that will appear while loading.
  72. *
  73. * @default "Loading"
  74. */
  75. this.intlLoading = TEXT$1.loading;
  76. /** When `true`, the icon will be flipped when the element direction is right-to-left (`"rtl"`). */
  77. this.iconFlipRtl = false;
  78. /** When `true`, the component is in the loading state and `calcite-progress` is displayed. */
  79. this.loading = false;
  80. /**
  81. * When `true`, the component's value can be read, but cannot be modified.
  82. *
  83. * @mdn [readOnly](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/readonly)
  84. */
  85. this.readOnly = false;
  86. /** When `true`, the component must have a value in order for the form to submit. */
  87. this.required = false;
  88. /** Specifies the size of the component. */
  89. this.scale = "m";
  90. /** Specifies the status of the input field, which determines message and icons. */
  91. this.status = "idle";
  92. /**
  93. * @internal
  94. */
  95. this.editingEnabled = false;
  96. /** The component's value. */
  97. this.value = "";
  98. this.previousValueOrigin = "initial";
  99. this.mutationObserver = createObserver("mutation", () => this.setDisabledAction());
  100. this.userChangedValue = false;
  101. //--------------------------------------------------------------------------
  102. //
  103. // Private Methods
  104. //
  105. //--------------------------------------------------------------------------
  106. this.keyDownHandler = (event) => {
  107. if (this.readOnly || this.disabled) {
  108. return;
  109. }
  110. if (this.isClearable && event.key === "Escape") {
  111. this.clearInputTextValue(event);
  112. event.preventDefault();
  113. }
  114. if (event.key === "Enter" && !event.defaultPrevented) {
  115. if (submitForm(this)) {
  116. event.preventDefault();
  117. }
  118. }
  119. };
  120. this.clearInputTextValue = (nativeEvent) => {
  121. this.setValue({
  122. committing: true,
  123. nativeEvent,
  124. origin: "user",
  125. value: ""
  126. });
  127. };
  128. this.emitChangeIfUserModified = () => {
  129. if (this.previousValueOrigin === "user" && this.value !== this.previousEmittedValue) {
  130. this.calciteInputTextChange.emit();
  131. }
  132. this.previousEmittedValue = this.value;
  133. };
  134. this.inputTextBlurHandler = () => {
  135. this.calciteInternalInputTextBlur.emit({
  136. element: this.childEl,
  137. value: this.value
  138. });
  139. this.emitChangeIfUserModified();
  140. };
  141. this.inputTextFocusHandler = (event) => {
  142. const slottedActionEl = getSlotted(this.el, "action");
  143. if (event.target !== slottedActionEl) {
  144. this.setFocus();
  145. }
  146. this.calciteInternalInputTextFocus.emit({
  147. element: this.childEl,
  148. value: this.value
  149. });
  150. };
  151. this.inputTextInputHandler = (nativeEvent) => {
  152. if (this.disabled || this.readOnly) {
  153. return;
  154. }
  155. this.setValue({
  156. nativeEvent,
  157. origin: "user",
  158. value: nativeEvent.target.value
  159. });
  160. };
  161. this.inputTextKeyDownHandler = (event) => {
  162. if (this.disabled || this.readOnly) {
  163. return;
  164. }
  165. if (event.key === "Enter") {
  166. this.emitChangeIfUserModified();
  167. }
  168. };
  169. this.hiddenInputChangeHandler = (event) => {
  170. if (event.target.name === this.name) {
  171. this.setValue({
  172. value: event.target.value,
  173. origin: "direct"
  174. });
  175. }
  176. event.stopPropagation();
  177. };
  178. this.setChildElRef = (el) => {
  179. this.childEl = el;
  180. };
  181. this.setInputValue = (newInputValue) => {
  182. if (!this.childEl) {
  183. return;
  184. }
  185. this.childEl.value = newInputValue;
  186. };
  187. this.setPreviousEmittedValue = (newPreviousEmittedValue) => {
  188. this.previousEmittedValue = newPreviousEmittedValue;
  189. };
  190. this.setPreviousValue = (newPreviousValue) => {
  191. this.previousValue = newPreviousValue;
  192. };
  193. this.setValue = ({ committing = false, nativeEvent, origin, previousValue, value }) => {
  194. this.setPreviousValue(previousValue || this.value);
  195. this.previousValueOrigin = origin;
  196. this.userChangedValue = origin === "user" && value !== this.value;
  197. this.value = value;
  198. if (origin === "direct") {
  199. this.setInputValue(value);
  200. }
  201. if (nativeEvent) {
  202. const calciteInputTextInputEvent = this.calciteInputTextInput.emit({
  203. element: this.childEl,
  204. nativeEvent,
  205. value: this.value
  206. });
  207. if (calciteInputTextInputEvent.defaultPrevented) {
  208. this.value = this.previousValue;
  209. }
  210. else if (committing) {
  211. this.emitChangeIfUserModified();
  212. }
  213. }
  214. };
  215. }
  216. disabledWatcher() {
  217. this.setDisabledAction();
  218. }
  219. valueWatcher(newValue, previousValue) {
  220. if (!this.userChangedValue) {
  221. this.setValue({
  222. origin: "direct",
  223. previousValue,
  224. value: !newValue ? "" : newValue
  225. });
  226. }
  227. this.userChangedValue = false;
  228. }
  229. updateRequestedIcon() {
  230. this.requestedIcon = setRequestedIcon({}, this.icon, "text");
  231. }
  232. get isClearable() {
  233. return this.clearable && this.value.length > 0;
  234. }
  235. //--------------------------------------------------------------------------
  236. //
  237. // Lifecycle
  238. //
  239. //--------------------------------------------------------------------------
  240. connectedCallback() {
  241. var _a;
  242. this.scale = getElementProp(this.el, "scale", this.scale);
  243. this.status = getElementProp(this.el, "status", this.status);
  244. this.inlineEditableEl = this.el.closest("calcite-inline-editable");
  245. if (this.inlineEditableEl) {
  246. this.editingEnabled = this.inlineEditableEl.editingEnabled || false;
  247. }
  248. this.setPreviousEmittedValue(this.value);
  249. this.setPreviousValue(this.value);
  250. connectLabel(this);
  251. connectForm(this);
  252. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.observe(this.el, { childList: true });
  253. this.setDisabledAction();
  254. this.el.addEventListener("calciteInternalHiddenInputChange", this.hiddenInputChangeHandler);
  255. }
  256. disconnectedCallback() {
  257. var _a;
  258. disconnectLabel(this);
  259. disconnectForm(this);
  260. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
  261. this.el.removeEventListener("calciteInternalHiddenInputChange", this.hiddenInputChangeHandler);
  262. }
  263. componentWillLoad() {
  264. this.requestedIcon = setRequestedIcon({}, this.icon, "text");
  265. }
  266. componentDidRender() {
  267. updateHostInteraction(this);
  268. }
  269. //--------------------------------------------------------------------------
  270. //
  271. // Public Methods
  272. //
  273. //--------------------------------------------------------------------------
  274. /** Sets focus on the component. */
  275. async setFocus() {
  276. var _a;
  277. (_a = this.childEl) === null || _a === void 0 ? void 0 : _a.focus();
  278. }
  279. /** Selects all text of the component's `value`. */
  280. async selectText() {
  281. var _a;
  282. (_a = this.childEl) === null || _a === void 0 ? void 0 : _a.select();
  283. }
  284. onLabelClick() {
  285. this.setFocus();
  286. }
  287. onFormReset() {
  288. this.setValue({
  289. origin: "reset",
  290. value: this.defaultValue
  291. });
  292. }
  293. syncHiddenFormInput(input) {
  294. if (this.minLength != null) {
  295. input.minLength = this.minLength;
  296. }
  297. if (this.maxLength != null) {
  298. input.maxLength = this.maxLength;
  299. }
  300. }
  301. setDisabledAction() {
  302. const slottedActionEl = getSlotted(this.el, "action");
  303. if (!slottedActionEl) {
  304. return;
  305. }
  306. this.disabled
  307. ? slottedActionEl.setAttribute("disabled", "")
  308. : slottedActionEl.removeAttribute("disabled");
  309. }
  310. // --------------------------------------------------------------------------
  311. //
  312. // Render Methods
  313. //
  314. // --------------------------------------------------------------------------
  315. render() {
  316. const dir = getElementDir(this.el);
  317. const loader = (h("div", { class: CSS.loader }, h("calcite-progress", { label: this.intlLoading, type: "indeterminate" })));
  318. const inputClearButton = (h("button", { "aria-label": this.intlClear || TEXT.clear, class: CSS.clearButton, disabled: this.disabled || this.readOnly, onClick: this.clearInputTextValue, tabIndex: -1, type: "button" }, h("calcite-icon", { icon: "x", scale: "s" })));
  319. const iconEl = (h("calcite-icon", { class: CSS.inputIcon, flipRtl: this.iconFlipRtl, icon: this.requestedIcon, scale: "s" }));
  320. const prefixText = h("div", { class: CSS.prefix }, this.prefixText);
  321. const suffixText = h("div", { class: CSS.suffix }, this.suffixText);
  322. const childEl = (h("input", { "aria-label": getLabelText(this), autofocus: this.autofocus ? true : null, class: {
  323. [CSS.editingEnabled]: this.editingEnabled,
  324. [CSS.inlineChild]: !!this.inlineEditableEl
  325. }, defaultValue: this.defaultValue, disabled: this.disabled ? true : null, enterKeyHint: this.el.enterKeyHint, inputMode: this.el.inputMode, maxLength: this.maxLength, minLength: this.minLength, name: this.name, onBlur: this.inputTextBlurHandler, onFocus: this.inputTextFocusHandler, onInput: this.inputTextInputHandler, onKeyDown: this.inputTextKeyDownHandler, placeholder: this.placeholder || "", readOnly: this.readOnly, ref: this.setChildElRef, required: this.required ? true : null, tabIndex: this.disabled || (this.inlineEditableEl && !this.editingEnabled) ? -1 : null, type: "text", value: this.value }));
  326. return (h(Host, { onClick: this.inputTextFocusHandler, onKeyDown: this.keyDownHandler }, h("div", { class: { [CSS.inputWrapper]: true, [CSS_UTILITY.rtl]: dir === "rtl" } }, this.prefixText ? prefixText : null, h("div", { class: CSS.wrapper }, childEl, this.isClearable ? inputClearButton : null, this.requestedIcon ? iconEl : null, this.loading ? loader : null), h("div", { class: CSS.actionWrapper }, h("slot", { name: SLOTS.action })), this.suffixText ? suffixText : null, h(HiddenFormInputSlot, { component: this }))));
  327. }
  328. get el() { return getElement(this); }
  329. static get watchers() { return {
  330. "disabled": ["disabledWatcher"],
  331. "value": ["valueWatcher"],
  332. "icon": ["updateRequestedIcon"]
  333. }; }
  334. };
  335. InputText.style = inputTextCss;
  336. export { InputText as calcite_input_text };