calcite-radio-group_2.cjs.entry.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. 'use strict';
  7. Object.defineProperty(exports, '__esModule', { value: true });
  8. const index = require('./index-a0010f96.js');
  9. const dom = require('./dom-2ec8c9ed.js');
  10. const label = require('./label-28060b83.js');
  11. const form = require('./form-1d831023.js');
  12. const interactive = require('./interactive-32293bca.js');
  13. require('./resources-b5a5f8a7.js');
  14. require('./guid-f4f03a7a.js');
  15. const radioGroupCss = "@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:flex;background-color:var(--calcite-ui-foreground-1);inline-size:-moz-fit-content;inline-size:fit-content;outline:1px solid var(--calcite-ui-border-input);outline-offset:-1px}:host([disabled]) ::slotted([calcite-hydrated][disabled]),:host([disabled]) [calcite-hydrated][disabled]{opacity:1}:host([layout=vertical]){flex-direction:column;align-items:flex-start;align-self:flex-start}:host([width=full]){inline-size:100%;min-inline-size:-moz-fit-content;min-inline-size:fit-content}:host([width=full]) ::slotted(calcite-radio-group-item){flex:1 1 auto}:host([width=full][layout=vertical]) ::slotted(calcite-radio-group-item){justify-content:flex-start}::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}";
  16. const RadioGroup = class {
  17. constructor(hostRef) {
  18. index.registerInstance(this, hostRef);
  19. this.calciteRadioGroupChange = index.createEvent(this, "calciteRadioGroupChange", 6);
  20. //--------------------------------------------------------------------------
  21. //
  22. // Properties
  23. //
  24. //--------------------------------------------------------------------------
  25. /** Specifies the appearance style of the component. */
  26. this.appearance = "solid";
  27. /** When `true`, interaction is prevented and the component is displayed with lower opacity. */
  28. this.disabled = false;
  29. /**
  30. * When `true`, the component must have a value in order for the form to submit.
  31. *
  32. * @internal
  33. */
  34. this.required = false;
  35. /** Defines the layout of the component. */
  36. this.layout = "horizontal";
  37. /** Specifies the size of the component. */
  38. this.scale = "m";
  39. /** The component's `selectedItem` value. */
  40. this.value = null;
  41. /** Specifies the width of the component. */
  42. this.width = "auto";
  43. //--------------------------------------------------------------------------
  44. //
  45. // Event Listeners
  46. //
  47. //--------------------------------------------------------------------------
  48. this.handleClick = (event) => {
  49. if (event.target.localName === "calcite-radio-group-item") {
  50. this.selectItem(event.target, true);
  51. }
  52. };
  53. }
  54. valueHandler(value) {
  55. const items = this.getItems();
  56. items.forEach((item) => (item.checked = item.value === value));
  57. }
  58. handleSelectedItemChange(newItem, oldItem) {
  59. this.value = newItem === null || newItem === void 0 ? void 0 : newItem.value;
  60. if (newItem === oldItem) {
  61. return;
  62. }
  63. const items = this.getItems();
  64. const match = Array.from(items)
  65. .filter((item) => item === newItem)
  66. .pop();
  67. if (match) {
  68. this.selectItem(match);
  69. }
  70. else if (items[0]) {
  71. items[0].tabIndex = 0;
  72. }
  73. }
  74. //--------------------------------------------------------------------------
  75. //
  76. // Lifecycle
  77. //
  78. //--------------------------------------------------------------------------
  79. componentWillLoad() {
  80. const items = this.getItems();
  81. const lastChecked = Array.from(items)
  82. .filter((item) => item.checked)
  83. .pop();
  84. if (lastChecked) {
  85. this.selectItem(lastChecked);
  86. }
  87. else if (items[0]) {
  88. items[0].tabIndex = 0;
  89. }
  90. }
  91. componentDidLoad() {
  92. form.afterConnectDefaultValueSet(this, this.value);
  93. }
  94. connectedCallback() {
  95. label.connectLabel(this);
  96. form.connectForm(this);
  97. }
  98. disconnectedCallback() {
  99. label.disconnectLabel(this);
  100. form.disconnectForm(this);
  101. }
  102. componentDidRender() {
  103. interactive.updateHostInteraction(this);
  104. }
  105. render() {
  106. return (index.h(index.Host, { onClick: this.handleClick, role: "radiogroup" }, index.h("slot", null), index.h(form.HiddenFormInputSlot, { component: this })));
  107. }
  108. handleSelected(event) {
  109. event.preventDefault();
  110. this.selectItem(event.target);
  111. event.stopPropagation();
  112. }
  113. handleKeyDown(event) {
  114. const keys = ["ArrowLeft", "ArrowUp", "ArrowRight", "ArrowDown", " "];
  115. const { key } = event;
  116. const { el, selectedItem } = this;
  117. if (keys.indexOf(key) === -1) {
  118. return;
  119. }
  120. let adjustedKey = key;
  121. if (dom.getElementDir(el) === "rtl") {
  122. if (key === "ArrowRight") {
  123. adjustedKey = "ArrowLeft";
  124. }
  125. if (key === "ArrowLeft") {
  126. adjustedKey = "ArrowRight";
  127. }
  128. }
  129. const items = this.getItems();
  130. let selectedIndex = -1;
  131. items.forEach((item, index) => {
  132. if (item === selectedItem) {
  133. selectedIndex = index;
  134. }
  135. });
  136. switch (adjustedKey) {
  137. case "ArrowLeft":
  138. case "ArrowUp":
  139. event.preventDefault();
  140. const previous = selectedIndex < 1 ? items.item(items.length - 1) : items.item(selectedIndex - 1);
  141. this.selectItem(previous, true);
  142. return;
  143. case "ArrowRight":
  144. case "ArrowDown":
  145. event.preventDefault();
  146. const next = selectedIndex === -1 ? items.item(1) : items.item(selectedIndex + 1) || items.item(0);
  147. this.selectItem(next, true);
  148. return;
  149. case " ":
  150. event.preventDefault();
  151. this.selectItem(event.target, true);
  152. return;
  153. default:
  154. return;
  155. }
  156. }
  157. // --------------------------------------------------------------------------
  158. //
  159. // Methods
  160. //
  161. // --------------------------------------------------------------------------
  162. /** Sets focus on the component. */
  163. async setFocus() {
  164. var _a;
  165. (_a = (this.selectedItem || this.getItems()[0])) === null || _a === void 0 ? void 0 : _a.focus();
  166. }
  167. //--------------------------------------------------------------------------
  168. //
  169. // Private Methods
  170. //
  171. //--------------------------------------------------------------------------
  172. onLabelClick() {
  173. this.setFocus();
  174. }
  175. getItems() {
  176. return this.el.querySelectorAll("calcite-radio-group-item");
  177. }
  178. selectItem(selected, emit = false) {
  179. if (selected === this.selectedItem) {
  180. return;
  181. }
  182. const items = this.getItems();
  183. let match = null;
  184. items.forEach((item) => {
  185. const matches = item.value === selected.value;
  186. if ((matches && !item.checked) || (!matches && item.checked)) {
  187. item.checked = matches;
  188. }
  189. item.tabIndex = matches ? 0 : -1;
  190. if (matches) {
  191. match = item;
  192. if (emit) {
  193. this.calciteRadioGroupChange.emit(match.value);
  194. }
  195. }
  196. });
  197. this.selectedItem = match;
  198. if (match) {
  199. match.focus();
  200. }
  201. }
  202. get el() { return index.getElement(this); }
  203. static get watchers() { return {
  204. "value": ["valueHandler"],
  205. "selectedItem": ["handleSelectedItemChange"]
  206. }; }
  207. };
  208. RadioGroup.style = radioGroupCss;
  209. const SLOTS = {
  210. input: "input"
  211. };
  212. const CSS = {
  213. radioGroupItemIcon: "radio-group-item-icon"
  214. };
  215. const radioGroupItemCss = "@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:flex;cursor:pointer;align-self:stretch;font-weight:var(--calcite-font-weight-normal);transition:background-color var(--calcite-internal-animation-timing-fast) ease-in-out, border-color var(--calcite-animation-timing) ease-in-out}:host label{pointer-events:none;margin:0.125rem;box-sizing:border-box;display:flex;flex:1 1 0%;align-items:center;color:var(--calcite-ui-text-3);transition:background-color var(--calcite-internal-animation-timing-fast) ease-in-out, border-color var(--calcite-internal-animation-timing-fast) ease-in-out, color var(--calcite-internal-animation-timing-fast) ease-in-out}.label--horizontal{justify-content:center}:host{outline-color:transparent}:host(:focus){outline:2px solid var(--calcite-ui-brand);outline-offset:-1px}.label--scale-s{padding-inline:0.5rem;font-size:var(--calcite-font-size--2);line-height:1rem;padding-block:0.125rem}.label--scale-m{padding-inline:0.75rem;font-size:var(--calcite-font-size--1);line-height:1rem;padding-block:0.375rem}.label--scale-l{padding-inline:1rem;padding-block:0.625rem;font-size:var(--calcite-font-size-0);line-height:1.25rem}:host(:hover) label{background-color:var(--calcite-ui-foreground-2);color:var(--calcite-ui-text-1)}:host(:active) label{background-color:var(--calcite-ui-foreground-3)}:host([checked]) label{cursor:default;border-color:var(--calcite-ui-brand);background-color:var(--calcite-ui-brand);color:var(--calcite-ui-background)}:host([checked]) .label--outline{border-color:var(--calcite-ui-brand);background-color:var(--calcite-ui-foreground-1);box-shadow:inset 0 0 0 1px var(--calcite-ui-brand);color:var(--calcite-ui-brand)}::slotted(input){display:none}@media (forced-colors: active){:host([checked]) label{background-color:highlight}:host([checked]) .label--outline{outline:2px solid transparent;outline-offset:2px}:host([checked]) label:not([class~=label--outline]) .radio-group-item-icon{color:highlightText}}.radio-group-item-icon{position:relative;margin:0px;display:inline-flex;line-height:inherit}:host([icon-position=start]) .label--scale-s .radio-group-item-icon{margin-inline-end:0.5rem}:host([icon-position=end]) .label--scale-s .radio-group-item-icon{margin-inline-end:unset;margin-inline-start:0.5rem}:host([icon-position=start]) .label--scale-m .radio-group-item-icon{margin-inline-end:0.75rem}:host([icon-position=end]) .label--scale-m .radio-group-item-icon{margin-inline-end:unset;margin-inline-start:0.75rem}:host([icon-position=start]) .label--scale-l .radio-group-item-icon{margin-inline-end:1rem}:host([icon-position=end]) .label--scale-l .radio-group-item-icon{margin-inline-end:unset;margin-inline-start:1rem}:host([icon-start]) .label--scale-s .radio-group-item-icon{margin-inline-end:0.5rem}:host([icon-end]) .label--scale-s .radio-group-item-icon{margin-inline-start:0.5rem}:host([icon-start]) .label--scale-m .radio-group-item-icon{margin-inline-end:0.75rem}:host([icon-end]) .label--scale-m .radio-group-item-icon{margin-inline-start:0.75rem}:host([icon-start]) .label--scale-l .radio-group-item-icon{margin-inline-end:1rem}:host([icon-end]) .label--scale-l .radio-group-item-icon{margin-inline-start:1rem}";
  216. const RadioGroupItem = class {
  217. constructor(hostRef) {
  218. index.registerInstance(this, hostRef);
  219. this.calciteInternalRadioGroupItemChange = index.createEvent(this, "calciteInternalRadioGroupItemChange", 6);
  220. //--------------------------------------------------------------------------
  221. //
  222. // Properties
  223. //
  224. //--------------------------------------------------------------------------
  225. /** When `true`, the component is checked. */
  226. this.checked = false;
  227. /** When `true`, the icon will be flipped when the element direction is right-to-left (`"rtl"`). */
  228. this.iconFlipRtl = false;
  229. /**
  230. * Specifies the placement of the icon.
  231. *
  232. * @deprecated Use either `iconStart` or `iconEnd` but do not combine them with `icon` and `iconPosition`.
  233. */
  234. this.iconPosition = "start";
  235. }
  236. handleCheckedChange() {
  237. this.calciteInternalRadioGroupItemChange.emit();
  238. }
  239. render() {
  240. const { checked, value } = this;
  241. const scale = dom.getElementProp(this.el, "scale", "m");
  242. const appearance = dom.getElementProp(this.el, "appearance", "solid");
  243. const layout = dom.getElementProp(this.el, "layout", "horizontal");
  244. const iconStartEl = this.iconStart ? (index.h("calcite-icon", { class: CSS.radioGroupItemIcon, flipRtl: this.iconFlipRtl, icon: this.iconStart, key: "icon-start", scale: "s" })) : null;
  245. const iconEndEl = this.iconEnd ? (index.h("calcite-icon", { class: CSS.radioGroupItemIcon, flipRtl: this.iconFlipRtl, icon: this.iconEnd, key: "icon-end", scale: "s" })) : null;
  246. const iconEl = (index.h("calcite-icon", { class: CSS.radioGroupItemIcon, flipRtl: this.iconFlipRtl, icon: this.icon, key: "icon", scale: "s" }));
  247. const iconAtStart = this.icon && this.iconPosition === "start" && !this.iconStart ? iconEl : null;
  248. const iconAtEnd = this.icon && this.iconPosition === "end" && !this.iconEnd ? iconEl : null;
  249. return (index.h(index.Host, { "aria-checked": dom.toAriaBoolean(checked), "aria-label": value, role: "radio" }, index.h("label", { class: {
  250. "label--scale-s": scale === "s",
  251. "label--scale-m": scale === "m",
  252. "label--scale-l": scale === "l",
  253. "label--horizontal": layout === "horizontal",
  254. "label--outline": appearance === "outline"
  255. } }, iconAtStart, this.iconStart ? iconStartEl : null, index.h("slot", null, value), index.h("slot", { name: SLOTS.input }), iconAtEnd, this.iconEnd ? iconEndEl : null)));
  256. }
  257. get el() { return index.getElement(this); }
  258. static get watchers() { return {
  259. "checked": ["handleCheckedChange"]
  260. }; }
  261. };
  262. RadioGroupItem.style = radioGroupItemCss;
  263. exports.calcite_radio_group = RadioGroup;
  264. exports.calcite_radio_group_item = RadioGroupItem;