calcite-radio-group_2.entry.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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.82
  5. */
  6. import { r as registerInstance, c as createEvent, h, H as Host, g as getElement } from './index-8ece2564.js';
  7. import { a as getElementDir, d as getElementProp, t as toAriaBoolean } from './dom-da697a3f.js';
  8. import { c as connectLabel, d as disconnectLabel } from './label-50132b90.js';
  9. import { c as connectForm, d as disconnectForm, H as HiddenFormInputSlot } from './form-bca481e1.js';
  10. import { u as updateHostInteraction } from './interactive-cb5bf285.js';
  11. import './guid-b4461004.js';
  12. const radioGroupCss = "@-webkit-keyframes in{0%{opacity:0}100%{opacity:1}}@keyframes in{0%{opacity:0}100%{opacity:1}}@-webkit-keyframes in-down{0%{opacity:0;-webkit-transform:translate3D(0, -5px, 0);transform:translate3D(0, -5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@keyframes in-down{0%{opacity:0;-webkit-transform:translate3D(0, -5px, 0);transform:translate3D(0, -5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@-webkit-keyframes in-up{0%{opacity:0;-webkit-transform:translate3D(0, 5px, 0);transform:translate3D(0, 5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@keyframes in-up{0%{opacity:0;-webkit-transform:translate3D(0, 5px, 0);transform:translate3D(0, 5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@-webkit-keyframes in-scale{0%{opacity:0;-webkit-transform:scale3D(0.95, 0.95, 1);transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;-webkit-transform:scale3D(1, 1, 1);transform:scale3D(1, 1, 1)}}@keyframes in-scale{0%{opacity:0;-webkit-transform:scale3D(0.95, 0.95, 1);transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;-webkit-transform:scale3D(1, 1, 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;-webkit-animation-fill-mode:both;animation-fill-mode:both;-webkit-animation-duration:var(--calcite-animation-timing);animation-duration:var(--calcite-animation-timing)}.calcite-animate__in{-webkit-animation-name:in;animation-name:in}.calcite-animate__in-down{-webkit-animation-name:in-down;animation-name:in-down}.calcite-animate__in-up{-webkit-animation-name:in-up;animation-name:in-up}.calcite-animate__in-scale{-webkit-animation-name:in-scale;animation-name:in-scale}:root{--calcite-popper-transition:var(--calcite-animation-timing)}:host([hidden]){display:none}:host{display:-ms-flexbox;display:flex;background-color:var(--calcite-ui-foreground-1);width:-moz-fit-content;width:-webkit-fit-content;width:fit-content;outline:1px solid var(--calcite-ui-border-input);outline-offset:-1px}:host([disabled]){pointer-events:none;cursor:default;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;opacity:var(--calcite-ui-opacity-disabled)}:host([disabled]) ::slotted([calcite-hydrated][disabled]),:host([disabled]) [calcite-hydrated][disabled]{opacity:1}:host([layout=vertical]){-ms-flex-direction:column;flex-direction:column;-ms-flex-align:start;align-items:flex-start;-ms-flex-item-align:start;align-self:flex-start}:host([width=full]){width:100%;min-width:-moz-fit-content;min-width:-webkit-fit-content;min-width:fit-content}:host([width=full]) ::slotted(calcite-radio-group-item){-ms-flex:1 1 auto;flex:1 1 auto}:host([width=full][layout=vertical]) ::slotted(calcite-radio-group-item){-ms-flex-pack:start;justify-content:flex-start}::slotted(calcite-radio-group-item[checked]),::slotted(calcite-radio-group-item:focus){z-index:0}::slotted(input[slot=hidden-form-input]){bottom:0 !important;left:0 !important;margin:0 !important;opacity:0 !important;outline:none !important;padding:0 !important;position:absolute !important;right:0 !important;top:0 !important;-webkit-transform:none !important;transform:none !important;-webkit-appearance:none !important;z-index:-1 !important}";
  13. const RadioGroup = class {
  14. constructor(hostRef) {
  15. registerInstance(this, hostRef);
  16. this.calciteRadioGroupChange = createEvent(this, "calciteRadioGroupChange", 7);
  17. //--------------------------------------------------------------------------
  18. //
  19. // Properties
  20. //
  21. //--------------------------------------------------------------------------
  22. /** specify the appearance style of the radio group, defaults to solid. */
  23. this.appearance = "solid";
  24. /** is the radio group disabled */
  25. this.disabled = false;
  26. /**
  27. * When true, makes the component required for form-submission.
  28. *
  29. * @internal
  30. */
  31. this.required = false;
  32. /** specify the layout of the radio group, defaults to horizontal */
  33. this.layout = "horizontal";
  34. /** The scale of the radio group */
  35. this.scale = "m";
  36. /** The value of the selectedItem */
  37. this.value = null;
  38. /** specify the width of the group, defaults to auto */
  39. this.width = "auto";
  40. //--------------------------------------------------------------------------
  41. //
  42. // Event Listeners
  43. //
  44. //--------------------------------------------------------------------------
  45. this.handleClick = (event) => {
  46. if (event.target.localName === "calcite-radio-group-item") {
  47. this.selectItem(event.target, true);
  48. }
  49. };
  50. }
  51. valueHandler(value) {
  52. const items = this.getItems();
  53. items.forEach((item) => (item.checked = item.value === value));
  54. }
  55. handleSelectedItemChange(newItem, oldItem) {
  56. this.value = newItem === null || newItem === void 0 ? void 0 : newItem.value;
  57. if (newItem === oldItem) {
  58. return;
  59. }
  60. const items = this.getItems();
  61. const match = Array.from(items)
  62. .filter((item) => item === newItem)
  63. .pop();
  64. if (match) {
  65. this.selectItem(match);
  66. }
  67. else if (items[0]) {
  68. items[0].tabIndex = 0;
  69. }
  70. }
  71. //--------------------------------------------------------------------------
  72. //
  73. // Lifecycle
  74. //
  75. //--------------------------------------------------------------------------
  76. componentWillLoad() {
  77. const items = this.getItems();
  78. const lastChecked = Array.from(items)
  79. .filter((item) => item.checked)
  80. .pop();
  81. if (lastChecked) {
  82. this.selectItem(lastChecked);
  83. }
  84. else if (items[0]) {
  85. items[0].tabIndex = 0;
  86. }
  87. }
  88. connectedCallback() {
  89. connectLabel(this);
  90. connectForm(this);
  91. }
  92. disconnectedCallback() {
  93. disconnectLabel(this);
  94. disconnectForm(this);
  95. }
  96. componentDidRender() {
  97. updateHostInteraction(this);
  98. }
  99. render() {
  100. return (h(Host, { onClick: this.handleClick, role: "radiogroup" }, h("slot", null), h(HiddenFormInputSlot, { component: this })));
  101. }
  102. handleSelected(event) {
  103. event.stopPropagation();
  104. event.preventDefault();
  105. this.selectItem(event.target);
  106. }
  107. handleKeyDown(event) {
  108. const keys = ["ArrowLeft", "ArrowUp", "ArrowRight", "ArrowDown", " "];
  109. const key = event.key;
  110. const { el, selectedItem } = this;
  111. if (keys.indexOf(key) === -1) {
  112. return;
  113. }
  114. let adjustedKey = key;
  115. if (getElementDir(el) === "rtl") {
  116. if (key === "ArrowRight") {
  117. adjustedKey = "ArrowLeft";
  118. }
  119. if (key === "ArrowLeft") {
  120. adjustedKey = "ArrowRight";
  121. }
  122. }
  123. const items = this.getItems();
  124. let selectedIndex = -1;
  125. items.forEach((item, index) => {
  126. if (item === selectedItem) {
  127. selectedIndex = index;
  128. }
  129. });
  130. switch (adjustedKey) {
  131. case "ArrowLeft":
  132. case "ArrowUp":
  133. event.preventDefault();
  134. const previous = selectedIndex < 1 ? items.item(items.length - 1) : items.item(selectedIndex - 1);
  135. this.selectItem(previous, true);
  136. return;
  137. case "ArrowRight":
  138. case "ArrowDown":
  139. event.preventDefault();
  140. const next = selectedIndex === -1 ? items.item(1) : items.item(selectedIndex + 1) || items.item(0);
  141. this.selectItem(next, true);
  142. return;
  143. case " ":
  144. event.preventDefault();
  145. this.selectItem(event.target, true);
  146. return;
  147. default:
  148. return;
  149. }
  150. }
  151. // --------------------------------------------------------------------------
  152. //
  153. // Methods
  154. //
  155. // --------------------------------------------------------------------------
  156. /** Sets focus on the component. */
  157. async setFocus() {
  158. var _a;
  159. (_a = (this.selectedItem || this.getItems()[0])) === null || _a === void 0 ? void 0 : _a.focus();
  160. }
  161. //--------------------------------------------------------------------------
  162. //
  163. // Private Methods
  164. //
  165. //--------------------------------------------------------------------------
  166. onLabelClick() {
  167. this.setFocus();
  168. }
  169. getItems() {
  170. return this.el.querySelectorAll("calcite-radio-group-item");
  171. }
  172. selectItem(selected, emit = false) {
  173. if (selected === this.selectedItem) {
  174. return;
  175. }
  176. const items = this.getItems();
  177. let match = null;
  178. items.forEach((item) => {
  179. const matches = item.value === selected.value;
  180. if ((matches && !item.checked) || (!matches && item.checked)) {
  181. item.checked = matches;
  182. }
  183. item.tabIndex = matches ? 0 : -1;
  184. if (matches) {
  185. match = item;
  186. if (emit) {
  187. this.calciteRadioGroupChange.emit(match.value);
  188. }
  189. }
  190. });
  191. this.selectedItem = match;
  192. if (match) {
  193. match.focus();
  194. }
  195. }
  196. get el() { return getElement(this); }
  197. static get watchers() { return {
  198. "value": ["valueHandler"],
  199. "selectedItem": ["handleSelectedItemChange"]
  200. }; }
  201. };
  202. RadioGroup.style = radioGroupCss;
  203. const SLOTS = {
  204. input: "input"
  205. };
  206. const CSS = {
  207. radioGroupItemIcon: "radio-group-item-icon"
  208. };
  209. const radioGroupItemCss = "@-webkit-keyframes in{0%{opacity:0}100%{opacity:1}}@keyframes in{0%{opacity:0}100%{opacity:1}}@-webkit-keyframes in-down{0%{opacity:0;-webkit-transform:translate3D(0, -5px, 0);transform:translate3D(0, -5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@keyframes in-down{0%{opacity:0;-webkit-transform:translate3D(0, -5px, 0);transform:translate3D(0, -5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@-webkit-keyframes in-up{0%{opacity:0;-webkit-transform:translate3D(0, 5px, 0);transform:translate3D(0, 5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@keyframes in-up{0%{opacity:0;-webkit-transform:translate3D(0, 5px, 0);transform:translate3D(0, 5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@-webkit-keyframes in-scale{0%{opacity:0;-webkit-transform:scale3D(0.95, 0.95, 1);transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;-webkit-transform:scale3D(1, 1, 1);transform:scale3D(1, 1, 1)}}@keyframes in-scale{0%{opacity:0;-webkit-transform:scale3D(0.95, 0.95, 1);transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;-webkit-transform:scale3D(1, 1, 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;-webkit-animation-fill-mode:both;animation-fill-mode:both;-webkit-animation-duration:var(--calcite-animation-timing);animation-duration:var(--calcite-animation-timing)}.calcite-animate__in{-webkit-animation-name:in;animation-name:in}.calcite-animate__in-down{-webkit-animation-name:in-down;animation-name:in-down}.calcite-animate__in-up{-webkit-animation-name:in-up;animation-name:in-up}.calcite-animate__in-scale{-webkit-animation-name:in-scale;animation-name:in-scale}:root{--calcite-popper-transition:var(--calcite-animation-timing)}:host([hidden]){display:none}:host{display:-ms-flexbox;display:flex;cursor:pointer;-ms-flex-item-align:stretch;align-self:stretch;font-weight:var(--calcite-font-weight-normal);-webkit-transition:background-color var(--calcite-internal-animation-timing-fast) ease-in-out, border-color var(--calcite-animation-timing) ease-in-out;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;-webkit-box-sizing:border-box;box-sizing:border-box;display:-ms-flexbox;display:flex;-ms-flex:1 1 0%;flex:1 1 0%;-ms-flex-align:center;align-items:center;color:var(--calcite-ui-text-3);-webkit-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;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{-ms-flex-pack:center;justify-content:center}:host{outline-offset:0;outline-color:transparent;-webkit-transition:outline-offset 100ms ease-in-out, outline-color 100ms ease-in-out;transition:outline-offset 100ms ease-in-out, outline-color 100ms ease-in-out}:host(:focus){outline:2px solid var(--calcite-ui-brand);outline-offset:-1px}.label--scale-s{padding-left:0.5rem;padding-right:0.5rem;font-size:var(--calcite-font-size--2);line-height:1rem;padding-top:0.125rem;padding-bottom:0.125rem}.label--scale-m{padding-left:0.75rem;padding-right:0.75rem;font-size:var(--calcite-font-size--1);line-height:1rem;padding-top:0.375rem;padding-bottom:0.375rem}.label--scale-l{padding-left:1rem;padding-right:1rem;padding-top:0.625rem;padding-bottom: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);-webkit-box-shadow:inset 0 0 0 1px var(--calcite-ui-brand);box-shadow:inset 0 0 0 1px var(--calcite-ui-brand);color:var(--calcite-ui-brand)}::slotted(input){display:none}.radio-group-item-icon{position:relative;margin:0px;display:-ms-inline-flexbox;display:inline-flex;line-height:inherit}:host([icon-position=start]) .label--scale-s .radio-group-item-icon{-webkit-margin-end:0.5rem;margin-inline-end:0.5rem}:host([icon-position=end]) .label--scale-s .radio-group-item-icon{-webkit-margin-end:unset;margin-inline-end:unset;-webkit-margin-start:0.5rem;margin-inline-start:0.5rem}:host([icon-position=start]) .label--scale-m .radio-group-item-icon{-webkit-margin-end:0.75rem;margin-inline-end:0.75rem}:host([icon-position=end]) .label--scale-m .radio-group-item-icon{-webkit-margin-end:unset;margin-inline-end:unset;-webkit-margin-start:0.75rem;margin-inline-start:0.75rem}:host([icon-position=start]) .label--scale-l .radio-group-item-icon{-webkit-margin-end:1rem;margin-inline-end:1rem}:host([icon-position=end]) .label--scale-l .radio-group-item-icon{-webkit-margin-end:unset;margin-inline-end:unset;-webkit-margin-start:1rem;margin-inline-start:1rem}";
  210. const RadioGroupItem = class {
  211. constructor(hostRef) {
  212. registerInstance(this, hostRef);
  213. this.calciteRadioGroupItemChange = createEvent(this, "calciteRadioGroupItemChange", 7);
  214. //--------------------------------------------------------------------------
  215. //
  216. // Properties
  217. //
  218. //--------------------------------------------------------------------------
  219. /** Indicates whether the control is checked. */
  220. this.checked = false;
  221. /** flip the icon in rtl */
  222. this.iconFlipRtl = false;
  223. /** optionally used with icon, select where to position the icon */
  224. this.iconPosition = "start";
  225. }
  226. handleCheckedChange() {
  227. this.calciteRadioGroupItemChange.emit();
  228. }
  229. render() {
  230. const { checked, value } = this;
  231. const scale = getElementProp(this.el, "scale", "m");
  232. const appearance = getElementProp(this.el, "appearance", "solid");
  233. const layout = getElementProp(this.el, "layout", "horizontal");
  234. const iconEl = (h("calcite-icon", { class: CSS.radioGroupItemIcon, flipRtl: this.iconFlipRtl, icon: this.icon, scale: "s" }));
  235. return (h(Host, { "aria-checked": toAriaBoolean(checked), role: "radio" }, h("label", { class: {
  236. "label--scale-s": scale === "s",
  237. "label--scale-m": scale === "m",
  238. "label--scale-l": scale === "l",
  239. "label--horizontal": layout === "horizontal",
  240. "label--outline": appearance === "outline"
  241. } }, this.icon && this.iconPosition === "start" ? iconEl : null, h("slot", null, value), h("slot", { name: SLOTS.input }), this.icon && this.iconPosition === "end" ? iconEl : null)));
  242. }
  243. get el() { return getElement(this); }
  244. static get watchers() { return {
  245. "checked": ["handleCheckedChange"]
  246. }; }
  247. };
  248. RadioGroupItem.style = radioGroupItemCss;
  249. export { RadioGroup as calcite_radio_group, RadioGroupItem as calcite_radio_group_item };