calcite-option_3.entry.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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, F as Fragment } from './index-1f9b54dc.js';
  7. import { c as createObserver } from './observers-9f44e9b3.js';
  8. import { f as focusElement } from './dom-8f0a9ff2.js';
  9. import { c as connectLabel, d as disconnectLabel } from './label-333c4a4c.js';
  10. import { c as connectForm, d as disconnectForm, a as afterConnectDefaultValueSet, H as HiddenFormInputSlot } from './form-80dbd90e.js';
  11. import { u as updateHostInteraction } from './interactive-5db230e8.js';
  12. import './resources-9c476cb6.js';
  13. import './guid-9f15e57a.js';
  14. const optionCss = "@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{display:block}";
  15. const Option = class {
  16. constructor(hostRef) {
  17. registerInstance(this, hostRef);
  18. this.calciteInternalOptionChange = createEvent(this, "calciteInternalOptionChange", 6);
  19. //--------------------------------------------------------------------------
  20. //
  21. // Properties
  22. //
  23. //--------------------------------------------------------------------------
  24. /**
  25. * When `true`, interaction is prevented and the component is displayed with lower opacity.
  26. */
  27. this.disabled = false;
  28. this.mutationObserver = createObserver("mutation", () => {
  29. this.ensureTextContentDependentProps();
  30. this.calciteInternalOptionChange.emit();
  31. });
  32. }
  33. handlePropChange(_newValue, _oldValue, propName) {
  34. if (propName === "label" || propName === "value") {
  35. this.ensureTextContentDependentProps();
  36. }
  37. this.calciteInternalOptionChange.emit();
  38. }
  39. //--------------------------------------------------------------------------
  40. //
  41. // Private Methods
  42. //
  43. //--------------------------------------------------------------------------
  44. ensureTextContentDependentProps() {
  45. const { el: { textContent } } = this;
  46. if (!this.label || this.label === this.internallySetLabel) {
  47. this.label = textContent;
  48. this.internallySetLabel = textContent;
  49. }
  50. if (!this.value || this.value === this.internallySetValue) {
  51. this.value = textContent;
  52. this.internallySetValue = textContent;
  53. }
  54. }
  55. //--------------------------------------------------------------------------
  56. //
  57. // Lifecycle
  58. //
  59. //--------------------------------------------------------------------------
  60. connectedCallback() {
  61. var _a;
  62. this.ensureTextContentDependentProps();
  63. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.observe(this.el, {
  64. attributeFilter: ["label", "value"],
  65. characterData: true,
  66. childList: true,
  67. subtree: true
  68. });
  69. }
  70. disconnectedCallback() {
  71. var _a;
  72. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
  73. }
  74. //--------------------------------------------------------------------------
  75. //
  76. // Render Methods
  77. //
  78. //--------------------------------------------------------------------------
  79. render() {
  80. return h("slot", null, this.label);
  81. }
  82. get el() { return getElement(this); }
  83. static get watchers() { return {
  84. "disabled": ["handlePropChange"],
  85. "label": ["handlePropChange"],
  86. "selected": ["handlePropChange"],
  87. "value": ["handlePropChange"]
  88. }; }
  89. };
  90. Option.style = optionCss;
  91. const optionGroupCss = "@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{display:block}";
  92. const OptionGroup = class {
  93. constructor(hostRef) {
  94. registerInstance(this, hostRef);
  95. this.calciteInternalOptionGroupChange = createEvent(this, "calciteInternalOptionGroupChange", 6);
  96. //--------------------------------------------------------------------------
  97. //
  98. // Properties
  99. //
  100. //--------------------------------------------------------------------------
  101. /**
  102. * When `true`, interaction is prevented and the component is displayed with lower opacity.
  103. */
  104. this.disabled = false;
  105. }
  106. handlePropChange() {
  107. this.calciteInternalOptionGroupChange.emit();
  108. }
  109. //--------------------------------------------------------------------------
  110. //
  111. // Render Methods
  112. //
  113. //--------------------------------------------------------------------------
  114. render() {
  115. return (h(Fragment, null, h("div", null, this.label), h("slot", null)));
  116. }
  117. static get watchers() { return {
  118. "disabled": ["handlePropChange"],
  119. "label": ["handlePropChange"]
  120. }; }
  121. };
  122. OptionGroup.style = optionGroupCss;
  123. const CSS = {
  124. icon: "icon",
  125. iconContainer: "icon-container",
  126. select: "select"
  127. };
  128. const selectCss = "@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}}:host{--calcite-icon-size:1rem;--calcite-spacing-eighth:0.125rem;--calcite-spacing-quarter:0.25rem;--calcite-spacing-half:0.5rem;--calcite-spacing-three-quarters:0.75rem;--calcite-spacing:1rem;--calcite-spacing-plus-quarter:1.25rem;--calcite-spacing-plus-half:1.5rem;--calcite-spacing-double:2rem;--calcite-menu-min-width:10rem;--calcite-header-min-height:3rem;--calcite-footer-min-height:3rem}: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{position:relative;display:flex;align-items:stretch;inline-size:var(--select-width)}:host([disabled]) ::slotted([calcite-hydrated][disabled]),:host([disabled]) [calcite-hydrated][disabled]{opacity:1}:host([scale=s]){block-size:1.5rem;--calcite-select-font-size:var(--calcite-font-size--2);--calcite-select-spacing-inline:0.5rem 2rem}:host([scale=s]) .icon-container{padding-inline:0.5rem}:host([scale=m]){block-size:2rem;--calcite-select-font-size:var(--calcite-font-size--1);--calcite-select-spacing-inline:0.75rem 2.5rem}:host([scale=m]) .icon-container{padding-inline:0.75rem}:host([scale=l]){block-size:44px;--calcite-select-font-size:var(--calcite-font-size-0);--calcite-select-spacing-inline:1rem 3rem}:host([scale=l]) .icon-container{padding-inline:1rem}:host([width=auto]){inline-size:auto}:host([width=half]){inline-size:50%}:host([width=full]){inline-size:100%}.select{margin:0px;box-sizing:border-box;inline-size:100%;cursor:pointer;-webkit-appearance:none;appearance:none;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;border-radius:0px;border-width:1px;border-style:solid;border-color:var(--calcite-ui-border-input);background-color:var(--calcite-ui-foreground-1);font-family:inherit;color:var(--calcite-ui-text-2);outline-color:transparent;font-size:var(--calcite-select-font-size);padding-inline:var(--calcite-select-spacing-inline);border-inline-end-width:0px}.select:focus{outline:2px solid var(--calcite-ui-brand);outline-offset:-2px}.select:hover{background-color:var(--calcite-ui-foreground-2)}select:disabled{border-color:var(--calcite-ui-border-input);--tw-bg-opacity:1}.icon-container{pointer-events:none;position:absolute;inset-block:0px;display:flex;align-items:center;border-width:0px;border-style:solid;border-color:var(--calcite-ui-border-input);background-color:transparent;color:var(--calcite-ui-text-2);inset-inline-end:0px;border-inline-width:0px 1px}.select:focus~.icon-container{border-color:transparent}::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}";
  129. function isOption(optionOrGroup) {
  130. return optionOrGroup.tagName === "CALCITE-OPTION";
  131. }
  132. function isOptionGroup(optionOrGroup) {
  133. return optionOrGroup.tagName === "CALCITE-OPTION-GROUP";
  134. }
  135. const Select = class {
  136. constructor(hostRef) {
  137. registerInstance(this, hostRef);
  138. this.calciteSelectChange = createEvent(this, "calciteSelectChange", 6);
  139. //--------------------------------------------------------------------------
  140. //
  141. // Properties
  142. //
  143. //--------------------------------------------------------------------------
  144. /**
  145. * When `true`, interaction is prevented and the component is displayed with lower opacity.
  146. */
  147. this.disabled = false;
  148. /**
  149. * When `true`, the component must have a value in order for the form to submit.
  150. *
  151. * @internal
  152. */
  153. this.required = false;
  154. /**
  155. * Specifies the size of the component.
  156. */
  157. this.scale = "m";
  158. /** The component's `selectedOption` value. */
  159. this.value = null;
  160. /**
  161. * Specifies the width of the component.
  162. */
  163. this.width = "auto";
  164. this.componentToNativeEl = new Map();
  165. this.mutationObserver = createObserver("mutation", () => this.populateInternalSelect());
  166. this.handleInternalSelectChange = () => {
  167. const selected = this.selectEl.selectedOptions[0];
  168. this.selectFromNativeOption(selected);
  169. requestAnimationFrame(() => this.emitChangeEvent());
  170. };
  171. this.populateInternalSelect = () => {
  172. const optionsAndGroups = Array.from(this.el.children).filter((child) => child.tagName === "CALCITE-OPTION" || child.tagName === "CALCITE-OPTION-GROUP");
  173. this.clearInternalSelect();
  174. optionsAndGroups.forEach((optionOrGroup) => { var _a; return (_a = this.selectEl) === null || _a === void 0 ? void 0 : _a.append(this.toNativeElement(optionOrGroup)); });
  175. };
  176. this.storeSelectRef = (node) => {
  177. this.selectEl = node;
  178. this.populateInternalSelect();
  179. const selected = this.selectEl.selectedOptions[0];
  180. this.selectFromNativeOption(selected);
  181. };
  182. this.emitChangeEvent = () => {
  183. this.calciteSelectChange.emit();
  184. };
  185. }
  186. valueHandler(value) {
  187. const items = this.el.querySelectorAll("calcite-option");
  188. items.forEach((item) => (item.selected = item.value === value));
  189. }
  190. selectedOptionHandler(selectedOption) {
  191. this.value = selectedOption === null || selectedOption === void 0 ? void 0 : selectedOption.value;
  192. }
  193. //--------------------------------------------------------------------------
  194. //
  195. // Lifecycle
  196. //
  197. //--------------------------------------------------------------------------
  198. connectedCallback() {
  199. var _a;
  200. const { el } = this;
  201. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.observe(el, {
  202. subtree: true,
  203. childList: true
  204. });
  205. connectLabel(this);
  206. connectForm(this);
  207. }
  208. disconnectedCallback() {
  209. var _a;
  210. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
  211. disconnectLabel(this);
  212. disconnectForm(this);
  213. }
  214. componentDidLoad() {
  215. var _a, _b;
  216. afterConnectDefaultValueSet(this, (_b = (_a = this.selectedOption) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : "");
  217. }
  218. componentDidRender() {
  219. updateHostInteraction(this);
  220. }
  221. //--------------------------------------------------------------------------
  222. //
  223. // Public Methods
  224. //
  225. //--------------------------------------------------------------------------
  226. /** Sets focus on the component. */
  227. async setFocus() {
  228. focusElement(this.selectEl);
  229. }
  230. handleOptionOrGroupChange(event) {
  231. event.stopPropagation();
  232. const optionOrGroup = event.target;
  233. const nativeEl = this.componentToNativeEl.get(optionOrGroup);
  234. if (!nativeEl) {
  235. return;
  236. }
  237. this.updateNativeElement(optionOrGroup, nativeEl);
  238. if (isOption(optionOrGroup) && optionOrGroup.selected) {
  239. this.deselectAllExcept(optionOrGroup);
  240. this.selectedOption = optionOrGroup;
  241. }
  242. }
  243. //--------------------------------------------------------------------------
  244. //
  245. // Private Methods
  246. //
  247. //--------------------------------------------------------------------------
  248. onLabelClick() {
  249. this.setFocus();
  250. }
  251. updateNativeElement(optionOrGroup, nativeOptionOrGroup) {
  252. nativeOptionOrGroup.disabled = optionOrGroup.disabled;
  253. nativeOptionOrGroup.label = optionOrGroup.label;
  254. if (isOption(optionOrGroup)) {
  255. const option = nativeOptionOrGroup;
  256. option.selected = optionOrGroup.selected;
  257. option.value = optionOrGroup.value;
  258. // need to set innerText for mobile
  259. // see https://stackoverflow.com/questions/35021620/ios-safari-not-showing-all-options-for-select-menu/41749701
  260. option.innerText = optionOrGroup.label;
  261. }
  262. }
  263. clearInternalSelect() {
  264. this.componentToNativeEl.forEach((value) => value.remove());
  265. this.componentToNativeEl.clear();
  266. }
  267. selectFromNativeOption(nativeOption) {
  268. if (!nativeOption) {
  269. return;
  270. }
  271. let futureSelected;
  272. this.componentToNativeEl.forEach((nativeOptionOrGroup, optionOrGroup) => {
  273. if (isOption(optionOrGroup) && nativeOptionOrGroup === nativeOption) {
  274. optionOrGroup.selected = true;
  275. futureSelected = optionOrGroup;
  276. this.deselectAllExcept(optionOrGroup);
  277. }
  278. });
  279. if (futureSelected) {
  280. this.selectedOption = futureSelected;
  281. }
  282. }
  283. toNativeElement(optionOrGroup) {
  284. if (isOption(optionOrGroup)) {
  285. const option = document.createElement("option");
  286. this.updateNativeElement(optionOrGroup, option);
  287. this.componentToNativeEl.set(optionOrGroup, option);
  288. return option;
  289. }
  290. if (isOptionGroup(optionOrGroup)) {
  291. const group = document.createElement("optgroup");
  292. this.updateNativeElement(optionOrGroup, group);
  293. Array.from(optionOrGroup.children).forEach((option) => {
  294. const nativeOption = this.toNativeElement(option);
  295. group.append(nativeOption);
  296. this.componentToNativeEl.set(optionOrGroup, nativeOption);
  297. });
  298. this.componentToNativeEl.set(optionOrGroup, group);
  299. return group;
  300. }
  301. throw new Error("unsupported element child provided");
  302. }
  303. deselectAllExcept(except) {
  304. this.el.querySelectorAll("calcite-option").forEach((option) => {
  305. if (option === except) {
  306. return;
  307. }
  308. option.selected = false;
  309. });
  310. }
  311. //--------------------------------------------------------------------------
  312. //
  313. // Render Methods
  314. //
  315. //--------------------------------------------------------------------------
  316. renderChevron() {
  317. return (h("div", { class: CSS.iconContainer }, h("calcite-icon", { class: CSS.icon, icon: "chevron-down", scale: "s" })));
  318. }
  319. render() {
  320. return (h(Fragment, null, h("select", { "aria-label": this.label, class: CSS.select, disabled: this.disabled, onChange: this.handleInternalSelectChange, ref: this.storeSelectRef }, h("slot", null)), this.renderChevron(), h(HiddenFormInputSlot, { component: this })));
  321. }
  322. get el() { return getElement(this); }
  323. static get watchers() { return {
  324. "value": ["valueHandler"],
  325. "selectedOption": ["selectedOptionHandler"]
  326. }; }
  327. };
  328. Select.style = selectCss;
  329. export { Option as calcite_option, OptionGroup as calcite_option_group, Select as calcite_select };