calcite-radio-group_2.entry.js 16 KB

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