calcite-radio-group.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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, Host, Build } from '@stencil/core/internal/client/index.js';
  7. import { c as getElementDir } from './dom.js';
  8. import { c as connectLabel, d as disconnectLabel } from './label2.js';
  9. import { a as afterConnectDefaultValueSet, c as connectForm, d as disconnectForm, H as HiddenFormInputSlot } from './form.js';
  10. import { u as updateHostInteraction } from './interactive.js';
  11. 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}";
  12. const RadioGroup = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  13. constructor() {
  14. super();
  15. this.__registerHost();
  16. this.__attachShadow();
  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 (Build.isBrowser && match) {
  197. match.focus();
  198. }
  199. }
  200. get el() { return this; }
  201. static get watchers() { return {
  202. "value": ["valueHandler"],
  203. "selectedItem": ["handleSelectedItemChange"]
  204. }; }
  205. static get style() { return radioGroupCss; }
  206. }, [1, "calcite-radio-group", {
  207. "appearance": [513],
  208. "disabled": [516],
  209. "required": [516],
  210. "layout": [513],
  211. "name": [513],
  212. "scale": [513],
  213. "value": [1025],
  214. "selectedItem": [1040],
  215. "width": [513],
  216. "setFocus": [64]
  217. }, [[0, "calciteInternalRadioGroupItemChange", "handleSelected"], [0, "keydown", "handleKeyDown"]]]);
  218. function defineCustomElement$1() {
  219. if (typeof customElements === "undefined") {
  220. return;
  221. }
  222. const components = ["calcite-radio-group"];
  223. components.forEach(tagName => { switch (tagName) {
  224. case "calcite-radio-group":
  225. if (!customElements.get(tagName)) {
  226. customElements.define(tagName, RadioGroup);
  227. }
  228. break;
  229. } });
  230. }
  231. defineCustomElement$1();
  232. const CalciteRadioGroup = RadioGroup;
  233. const defineCustomElement = defineCustomElement$1;
  234. export { CalciteRadioGroup, defineCustomElement };