calcite-select.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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, Fragment } from '@stencil/core/internal/client/index.js';
  7. import { f as focusElement } from './dom.js';
  8. import { c as connectLabel, d as disconnectLabel } from './label2.js';
  9. import { c as connectForm, d as disconnectForm, a as afterConnectDefaultValueSet, H as HiddenFormInputSlot } from './form.js';
  10. import { c as createObserver } from './observers.js';
  11. import { u as updateHostInteraction } from './interactive.js';
  12. import { d as defineCustomElement$2 } from './icon.js';
  13. const CSS = {
  14. icon: "icon",
  15. iconContainer: "icon-container",
  16. select: "select"
  17. };
  18. 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}";
  19. function isOption(optionOrGroup) {
  20. return optionOrGroup.tagName === "CALCITE-OPTION";
  21. }
  22. function isOptionGroup(optionOrGroup) {
  23. return optionOrGroup.tagName === "CALCITE-OPTION-GROUP";
  24. }
  25. const Select = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  26. constructor() {
  27. super();
  28. this.__registerHost();
  29. this.__attachShadow();
  30. this.calciteSelectChange = createEvent(this, "calciteSelectChange", 6);
  31. //--------------------------------------------------------------------------
  32. //
  33. // Properties
  34. //
  35. //--------------------------------------------------------------------------
  36. /**
  37. * When `true`, interaction is prevented and the component is displayed with lower opacity.
  38. */
  39. this.disabled = false;
  40. /**
  41. * When `true`, the component must have a value in order for the form to submit.
  42. *
  43. * @internal
  44. */
  45. this.required = false;
  46. /**
  47. * Specifies the size of the component.
  48. */
  49. this.scale = "m";
  50. /** The component's `selectedOption` value. */
  51. this.value = null;
  52. /**
  53. * Specifies the width of the component.
  54. */
  55. this.width = "auto";
  56. this.componentToNativeEl = new Map();
  57. this.mutationObserver = createObserver("mutation", () => this.populateInternalSelect());
  58. this.handleInternalSelectChange = () => {
  59. const selected = this.selectEl.selectedOptions[0];
  60. this.selectFromNativeOption(selected);
  61. requestAnimationFrame(() => this.emitChangeEvent());
  62. };
  63. this.populateInternalSelect = () => {
  64. const optionsAndGroups = Array.from(this.el.children).filter((child) => child.tagName === "CALCITE-OPTION" || child.tagName === "CALCITE-OPTION-GROUP");
  65. this.clearInternalSelect();
  66. optionsAndGroups.forEach((optionOrGroup) => { var _a; return (_a = this.selectEl) === null || _a === void 0 ? void 0 : _a.append(this.toNativeElement(optionOrGroup)); });
  67. };
  68. this.storeSelectRef = (node) => {
  69. this.selectEl = node;
  70. this.populateInternalSelect();
  71. const selected = this.selectEl.selectedOptions[0];
  72. this.selectFromNativeOption(selected);
  73. };
  74. this.emitChangeEvent = () => {
  75. this.calciteSelectChange.emit();
  76. };
  77. }
  78. valueHandler(value) {
  79. const items = this.el.querySelectorAll("calcite-option");
  80. items.forEach((item) => (item.selected = item.value === value));
  81. }
  82. selectedOptionHandler(selectedOption) {
  83. this.value = selectedOption === null || selectedOption === void 0 ? void 0 : selectedOption.value;
  84. }
  85. //--------------------------------------------------------------------------
  86. //
  87. // Lifecycle
  88. //
  89. //--------------------------------------------------------------------------
  90. connectedCallback() {
  91. var _a;
  92. const { el } = this;
  93. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.observe(el, {
  94. subtree: true,
  95. childList: true
  96. });
  97. connectLabel(this);
  98. connectForm(this);
  99. }
  100. disconnectedCallback() {
  101. var _a;
  102. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
  103. disconnectLabel(this);
  104. disconnectForm(this);
  105. }
  106. componentDidLoad() {
  107. var _a, _b;
  108. afterConnectDefaultValueSet(this, (_b = (_a = this.selectedOption) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : "");
  109. }
  110. componentDidRender() {
  111. updateHostInteraction(this);
  112. }
  113. //--------------------------------------------------------------------------
  114. //
  115. // Public Methods
  116. //
  117. //--------------------------------------------------------------------------
  118. /** Sets focus on the component. */
  119. async setFocus() {
  120. focusElement(this.selectEl);
  121. }
  122. handleOptionOrGroupChange(event) {
  123. event.stopPropagation();
  124. const optionOrGroup = event.target;
  125. const nativeEl = this.componentToNativeEl.get(optionOrGroup);
  126. if (!nativeEl) {
  127. return;
  128. }
  129. this.updateNativeElement(optionOrGroup, nativeEl);
  130. if (isOption(optionOrGroup) && optionOrGroup.selected) {
  131. this.deselectAllExcept(optionOrGroup);
  132. this.selectedOption = optionOrGroup;
  133. }
  134. }
  135. //--------------------------------------------------------------------------
  136. //
  137. // Private Methods
  138. //
  139. //--------------------------------------------------------------------------
  140. onLabelClick() {
  141. this.setFocus();
  142. }
  143. updateNativeElement(optionOrGroup, nativeOptionOrGroup) {
  144. nativeOptionOrGroup.disabled = optionOrGroup.disabled;
  145. nativeOptionOrGroup.label = optionOrGroup.label;
  146. if (isOption(optionOrGroup)) {
  147. const option = nativeOptionOrGroup;
  148. option.selected = optionOrGroup.selected;
  149. option.value = optionOrGroup.value;
  150. // need to set innerText for mobile
  151. // see https://stackoverflow.com/questions/35021620/ios-safari-not-showing-all-options-for-select-menu/41749701
  152. option.innerText = optionOrGroup.label;
  153. }
  154. }
  155. clearInternalSelect() {
  156. this.componentToNativeEl.forEach((value) => value.remove());
  157. this.componentToNativeEl.clear();
  158. }
  159. selectFromNativeOption(nativeOption) {
  160. if (!nativeOption) {
  161. return;
  162. }
  163. let futureSelected;
  164. this.componentToNativeEl.forEach((nativeOptionOrGroup, optionOrGroup) => {
  165. if (isOption(optionOrGroup) && nativeOptionOrGroup === nativeOption) {
  166. optionOrGroup.selected = true;
  167. futureSelected = optionOrGroup;
  168. this.deselectAllExcept(optionOrGroup);
  169. }
  170. });
  171. if (futureSelected) {
  172. this.selectedOption = futureSelected;
  173. }
  174. }
  175. toNativeElement(optionOrGroup) {
  176. if (isOption(optionOrGroup)) {
  177. const option = document.createElement("option");
  178. this.updateNativeElement(optionOrGroup, option);
  179. this.componentToNativeEl.set(optionOrGroup, option);
  180. return option;
  181. }
  182. if (isOptionGroup(optionOrGroup)) {
  183. const group = document.createElement("optgroup");
  184. this.updateNativeElement(optionOrGroup, group);
  185. Array.from(optionOrGroup.children).forEach((option) => {
  186. const nativeOption = this.toNativeElement(option);
  187. group.append(nativeOption);
  188. this.componentToNativeEl.set(optionOrGroup, nativeOption);
  189. });
  190. this.componentToNativeEl.set(optionOrGroup, group);
  191. return group;
  192. }
  193. throw new Error("unsupported element child provided");
  194. }
  195. deselectAllExcept(except) {
  196. this.el.querySelectorAll("calcite-option").forEach((option) => {
  197. if (option === except) {
  198. return;
  199. }
  200. option.selected = false;
  201. });
  202. }
  203. //--------------------------------------------------------------------------
  204. //
  205. // Render Methods
  206. //
  207. //--------------------------------------------------------------------------
  208. renderChevron() {
  209. return (h("div", { class: CSS.iconContainer }, h("calcite-icon", { class: CSS.icon, icon: "chevron-down", scale: "s" })));
  210. }
  211. render() {
  212. 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 })));
  213. }
  214. get el() { return this; }
  215. static get watchers() { return {
  216. "value": ["valueHandler"],
  217. "selectedOption": ["selectedOptionHandler"]
  218. }; }
  219. static get style() { return selectCss; }
  220. }, [1, "calcite-select", {
  221. "disabled": [516],
  222. "label": [1],
  223. "name": [513],
  224. "required": [516],
  225. "scale": [513],
  226. "value": [1025],
  227. "selectedOption": [1040],
  228. "width": [513],
  229. "setFocus": [64]
  230. }, [[0, "calciteInternalOptionChange", "handleOptionOrGroupChange"], [0, "calciteInternalOptionGroupChange", "handleOptionOrGroupChange"]]]);
  231. function defineCustomElement$1() {
  232. if (typeof customElements === "undefined") {
  233. return;
  234. }
  235. const components = ["calcite-select", "calcite-icon"];
  236. components.forEach(tagName => { switch (tagName) {
  237. case "calcite-select":
  238. if (!customElements.get(tagName)) {
  239. customElements.define(tagName, Select);
  240. }
  241. break;
  242. case "calcite-icon":
  243. if (!customElements.get(tagName)) {
  244. defineCustomElement$2();
  245. }
  246. break;
  247. } });
  248. }
  249. defineCustomElement$1();
  250. const CalciteSelect = Select;
  251. const defineCustomElement = defineCustomElement$1;
  252. export { CalciteSelect, defineCustomElement };