calcite-radio-button.cjs.entry.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 guid = require('./guid-f4f03a7a.js');
  10. const dom = require('./dom-2ec8c9ed.js');
  11. const label = require('./label-28060b83.js');
  12. const form = require('./form-1d831023.js');
  13. const array = require('./array-e708c699.js');
  14. const interactive = require('./interactive-32293bca.js');
  15. require('./resources-b5a5f8a7.js');
  16. const CSS = {
  17. container: "container"
  18. };
  19. const radioButtonCss = "@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:block;cursor:pointer}:host .container{position:relative;outline:2px solid transparent;outline-offset:2px}:host .radio{cursor:pointer;border-radius:9999px;background-color:var(--calcite-ui-foreground-1);outline-color:transparent;transition:all var(--calcite-animation-timing) ease-in-out 0s, outline 0s, outline-offset 0s;box-shadow:inset 0 0 0 1px var(--calcite-ui-border-input)}:host([hovered]) .radio,:host(:not([checked])[focused]:not([disabled])) .radio{box-shadow:inset 0 0 0 2px var(--calcite-ui-brand)}:host([focused]) .radio{outline:2px solid var(--calcite-ui-brand);outline-offset:2px}:host([disabled]) .radio{cursor:default;opacity:var(--calcite-ui-opacity-disabled)}:host([disabled]) ::slotted([calcite-hydrated][disabled]),:host([disabled]) [calcite-hydrated][disabled]{opacity:1}:host([hovered][disabled]) .radio{box-shadow:inset 0 0 0 1px var(--calcite-ui-border-input)}:host([scale=s]){--calcite-radio-size:var(--calcite-font-size--2)}:host([scale=m]){--calcite-radio-size:var(--calcite-font-size--1)}:host([scale=l]){--calcite-radio-size:var(--calcite-font-size-0)}.radio{block-size:var(--calcite-radio-size);max-inline-size:var(--calcite-radio-size);min-inline-size:var(--calcite-radio-size)}:host([scale=s][checked]) .radio,:host([hovered][scale=s][checked][disabled]) .radio{box-shadow:inset 0 0 0 4px var(--calcite-ui-brand)}:host([scale=s][focused][checked]:not([disabled])) .radio{box-shadow:inset 0 0 0 4px var(--calcite-ui-brand), 0 0 0 2px var(--calcite-ui-foreground-1)}:host([scale=m][checked]) .radio,:host([hovered][scale=m][checked][disabled]) .radio{box-shadow:inset 0 0 0 5px var(--calcite-ui-brand)}:host([scale=m][focused][checked]:not([disabled])) .radio{box-shadow:inset 0 0 0 5px var(--calcite-ui-brand), 0 0 0 2px var(--calcite-ui-foreground-1)}:host([scale=l][checked]) .radio,:host([hovered][scale=l][checked][disabled]) .radio{box-shadow:inset 0 0 0 6px var(--calcite-ui-brand)}:host([scale=l][focused][checked]:not([disabled])) .radio{box-shadow:inset 0 0 0 6px var(--calcite-ui-brand), 0 0 0 2px var(--calcite-ui-foreground-1)}@media (forced-colors: active){:host([checked]) .radio::after,:host([checked][disabled]) .radio::after{content:\"\";inline-size:var(--calcite-radio-size);block-size:var(--calcite-radio-size);background-color:windowText;display:block}}::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}";
  20. const RadioButton = class {
  21. constructor(hostRef) {
  22. index.registerInstance(this, hostRef);
  23. this.calciteInternalRadioButtonBlur = index.createEvent(this, "calciteInternalRadioButtonBlur", 6);
  24. this.calciteRadioButtonChange = index.createEvent(this, "calciteRadioButtonChange", 6);
  25. this.calciteInternalRadioButtonCheckedChange = index.createEvent(this, "calciteInternalRadioButtonCheckedChange", 6);
  26. this.calciteInternalRadioButtonFocus = index.createEvent(this, "calciteInternalRadioButtonFocus", 6);
  27. //--------------------------------------------------------------------------
  28. //
  29. // Properties
  30. //
  31. //--------------------------------------------------------------------------
  32. /** When `true`, the component is checked. */
  33. this.checked = false;
  34. /** When `true`, interaction is prevented and the component is displayed with lower opacity. */
  35. this.disabled = false;
  36. /**
  37. * The focused state of the component.
  38. *
  39. * @internal
  40. */
  41. this.focused = false;
  42. /** When `true`, the component is not displayed and is not focusable or checkable. */
  43. this.hidden = false;
  44. /**
  45. * The hovered state of the component.
  46. *
  47. * @internal
  48. */
  49. this.hovered = false;
  50. /** When `true`, the component must have a value selected from the `calcite-radio-button-group` in order for the form to submit. */
  51. this.required = false;
  52. /** Specifies the size of the component inherited from the `calcite-radio-button-group`. */
  53. this.scale = "m";
  54. //--------------------------------------------------------------------------
  55. //
  56. // Private Methods
  57. //
  58. //--------------------------------------------------------------------------
  59. this.selectItem = (items, selectedIndex) => {
  60. items[selectedIndex].click();
  61. };
  62. this.queryButtons = () => {
  63. return Array.from(this.rootNode.querySelectorAll("calcite-radio-button:not([hidden])")).filter((radioButton) => radioButton.name === this.name);
  64. };
  65. this.isDefaultSelectable = () => {
  66. const radioButtons = this.queryButtons();
  67. return !radioButtons.some((radioButton) => radioButton.checked) && radioButtons[0] === this.el;
  68. };
  69. this.check = () => {
  70. if (this.disabled) {
  71. return;
  72. }
  73. this.uncheckAllRadioButtonsInGroup();
  74. this.checked = true;
  75. this.focused = true;
  76. this.calciteRadioButtonChange.emit();
  77. this.setFocus();
  78. };
  79. this.clickHandler = () => {
  80. this.check();
  81. };
  82. this.setContainerEl = (el) => {
  83. this.containerEl = el;
  84. };
  85. this.handleKeyDown = (event) => {
  86. const keys = ["ArrowLeft", "ArrowUp", "ArrowRight", "ArrowDown", " "];
  87. const { key } = event;
  88. const { el } = this;
  89. if (keys.indexOf(key) === -1) {
  90. return;
  91. }
  92. if (key === " ") {
  93. this.check();
  94. event.preventDefault();
  95. return;
  96. }
  97. let adjustedKey = key;
  98. if (dom.getElementDir(el) === "rtl") {
  99. if (key === "ArrowRight") {
  100. adjustedKey = "ArrowLeft";
  101. }
  102. if (key === "ArrowLeft") {
  103. adjustedKey = "ArrowRight";
  104. }
  105. }
  106. const radioButtons = Array.from(this.rootNode.querySelectorAll("calcite-radio-button:not([hidden]")).filter((radioButton) => radioButton.name === this.name);
  107. let currentIndex = 0;
  108. const radioButtonsLength = radioButtons.length;
  109. radioButtons.some((item, index) => {
  110. if (item.checked) {
  111. currentIndex = index;
  112. return true;
  113. }
  114. });
  115. switch (adjustedKey) {
  116. case "ArrowLeft":
  117. case "ArrowUp":
  118. event.preventDefault();
  119. this.selectItem(radioButtons, array.getRoundRobinIndex(Math.max(currentIndex - 1, -1), radioButtonsLength));
  120. return;
  121. case "ArrowRight":
  122. case "ArrowDown":
  123. event.preventDefault();
  124. this.selectItem(radioButtons, array.getRoundRobinIndex(currentIndex + 1, radioButtonsLength));
  125. return;
  126. default:
  127. return;
  128. }
  129. };
  130. this.onContainerBlur = () => {
  131. this.focused = false;
  132. this.calciteInternalRadioButtonBlur.emit();
  133. };
  134. this.onContainerFocus = () => {
  135. if (!this.disabled) {
  136. this.focused = true;
  137. this.calciteInternalRadioButtonFocus.emit();
  138. }
  139. };
  140. }
  141. checkedChanged(newChecked) {
  142. if (newChecked) {
  143. this.uncheckOtherRadioButtonsInGroup();
  144. }
  145. this.calciteInternalRadioButtonCheckedChange.emit();
  146. }
  147. nameChanged() {
  148. this.checkLastRadioButton();
  149. }
  150. //--------------------------------------------------------------------------
  151. //
  152. // Public Methods
  153. //
  154. //--------------------------------------------------------------------------
  155. /** Sets focus on the component. */
  156. async setFocus() {
  157. if (!this.disabled) {
  158. dom.focusElement(this.containerEl);
  159. }
  160. }
  161. onLabelClick(event) {
  162. if (!this.disabled && !this.hidden) {
  163. this.uncheckOtherRadioButtonsInGroup();
  164. const label = event.currentTarget;
  165. const radioButton = label.for
  166. ? this.rootNode.querySelector(`calcite-radio-button[id="${label.for}"]`)
  167. : label.querySelector(`calcite-radio-button[name="${this.name}"]`);
  168. if (radioButton) {
  169. radioButton.checked = true;
  170. radioButton.focused = true;
  171. }
  172. this.calciteRadioButtonChange.emit();
  173. this.setFocus();
  174. }
  175. }
  176. checkLastRadioButton() {
  177. const radioButtons = this.queryButtons();
  178. const checkedRadioButtons = radioButtons.filter((radioButton) => radioButton.checked);
  179. if ((checkedRadioButtons === null || checkedRadioButtons === void 0 ? void 0 : checkedRadioButtons.length) > 1) {
  180. const lastCheckedRadioButton = checkedRadioButtons[checkedRadioButtons.length - 1];
  181. checkedRadioButtons
  182. .filter((checkedRadioButton) => checkedRadioButton !== lastCheckedRadioButton)
  183. .forEach((checkedRadioButton) => {
  184. checkedRadioButton.checked = false;
  185. checkedRadioButton.emitCheckedChange();
  186. });
  187. }
  188. }
  189. /** @internal */
  190. async emitCheckedChange() {
  191. this.calciteInternalRadioButtonCheckedChange.emit();
  192. }
  193. uncheckAllRadioButtonsInGroup() {
  194. const radioButtons = this.queryButtons();
  195. radioButtons.forEach((radioButton) => {
  196. if (radioButton.checked) {
  197. radioButton.checked = false;
  198. radioButton.focused = false;
  199. }
  200. });
  201. }
  202. uncheckOtherRadioButtonsInGroup() {
  203. const radioButtons = this.queryButtons();
  204. const otherRadioButtons = radioButtons.filter((radioButton) => radioButton.guid !== this.guid);
  205. otherRadioButtons.forEach((otherRadioButton) => {
  206. if (otherRadioButton.checked) {
  207. otherRadioButton.checked = false;
  208. otherRadioButton.focused = false;
  209. }
  210. });
  211. }
  212. getTabIndex() {
  213. if (this.disabled) {
  214. return undefined;
  215. }
  216. return this.checked || this.isDefaultSelectable() ? 0 : -1;
  217. }
  218. //--------------------------------------------------------------------------
  219. //
  220. // Event Listeners
  221. //
  222. //--------------------------------------------------------------------------
  223. mouseenter() {
  224. this.hovered = true;
  225. }
  226. mouseleave() {
  227. this.hovered = false;
  228. }
  229. //--------------------------------------------------------------------------
  230. //
  231. // Lifecycle
  232. //
  233. //--------------------------------------------------------------------------
  234. connectedCallback() {
  235. this.rootNode = this.el.getRootNode();
  236. this.guid = this.el.id || `calcite-radio-button-${guid.guid()}`;
  237. if (this.name) {
  238. this.checkLastRadioButton();
  239. }
  240. label.connectLabel(this);
  241. form.connectForm(this);
  242. }
  243. componentDidLoad() {
  244. if (this.focused && !this.disabled) {
  245. this.setFocus();
  246. }
  247. }
  248. disconnectedCallback() {
  249. label.disconnectLabel(this);
  250. form.disconnectForm(this);
  251. }
  252. componentDidRender() {
  253. interactive.updateHostInteraction(this);
  254. }
  255. // --------------------------------------------------------------------------
  256. //
  257. // Render Methods
  258. //
  259. // --------------------------------------------------------------------------
  260. render() {
  261. const tabIndex = this.getTabIndex();
  262. return (index.h(index.Host, { onClick: this.clickHandler, onKeyDown: this.handleKeyDown }, index.h("div", { "aria-checked": dom.toAriaBoolean(this.checked), "aria-label": label.getLabelText(this), class: CSS.container, onBlur: this.onContainerBlur, onFocus: this.onContainerFocus, ref: this.setContainerEl, role: "radio", tabIndex: tabIndex }, index.h("div", { class: "radio" })), index.h(form.HiddenFormInputSlot, { component: this })));
  263. }
  264. get el() { return index.getElement(this); }
  265. static get watchers() { return {
  266. "checked": ["checkedChanged"],
  267. "name": ["nameChanged"]
  268. }; }
  269. };
  270. RadioButton.style = radioButtonCss;
  271. exports.calcite_radio_button = RadioButton;