calcite-radio-button.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 } from '@stencil/core/internal/client/index.js';
  7. import { g as guid } from './guid.js';
  8. import { c as getElementDir, f as focusElement, t as toAriaBoolean } from './dom.js';
  9. import { c as connectLabel, d as disconnectLabel, g as getLabelText } from './label2.js';
  10. import { c as connectForm, d as disconnectForm, H as HiddenFormInputSlot } from './form.js';
  11. import { g as getRoundRobinIndex } from './array.js';
  12. import { u as updateHostInteraction } from './interactive.js';
  13. const CSS = {
  14. container: "container"
  15. };
  16. 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}";
  17. const RadioButton = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  18. constructor() {
  19. super();
  20. this.__registerHost();
  21. this.__attachShadow();
  22. this.calciteInternalRadioButtonBlur = createEvent(this, "calciteInternalRadioButtonBlur", 6);
  23. this.calciteRadioButtonChange = createEvent(this, "calciteRadioButtonChange", 6);
  24. this.calciteInternalRadioButtonCheckedChange = createEvent(this, "calciteInternalRadioButtonCheckedChange", 6);
  25. this.calciteInternalRadioButtonFocus = createEvent(this, "calciteInternalRadioButtonFocus", 6);
  26. //--------------------------------------------------------------------------
  27. //
  28. // Properties
  29. //
  30. //--------------------------------------------------------------------------
  31. /** When `true`, the component is checked. */
  32. this.checked = false;
  33. /** When `true`, interaction is prevented and the component is displayed with lower opacity. */
  34. this.disabled = false;
  35. /**
  36. * The focused state of the component.
  37. *
  38. * @internal
  39. */
  40. this.focused = false;
  41. /** When `true`, the component is not displayed and is not focusable or checkable. */
  42. this.hidden = false;
  43. /**
  44. * The hovered state of the component.
  45. *
  46. * @internal
  47. */
  48. this.hovered = false;
  49. /** When `true`, the component must have a value selected from the `calcite-radio-button-group` in order for the form to submit. */
  50. this.required = false;
  51. /** Specifies the size of the component inherited from the `calcite-radio-button-group`. */
  52. this.scale = "m";
  53. //--------------------------------------------------------------------------
  54. //
  55. // Private Methods
  56. //
  57. //--------------------------------------------------------------------------
  58. this.selectItem = (items, selectedIndex) => {
  59. items[selectedIndex].click();
  60. };
  61. this.queryButtons = () => {
  62. return Array.from(this.rootNode.querySelectorAll("calcite-radio-button:not([hidden])")).filter((radioButton) => radioButton.name === this.name);
  63. };
  64. this.isDefaultSelectable = () => {
  65. const radioButtons = this.queryButtons();
  66. return !radioButtons.some((radioButton) => radioButton.checked) && radioButtons[0] === this.el;
  67. };
  68. this.check = () => {
  69. if (this.disabled) {
  70. return;
  71. }
  72. this.uncheckAllRadioButtonsInGroup();
  73. this.checked = true;
  74. this.focused = true;
  75. this.calciteRadioButtonChange.emit();
  76. this.setFocus();
  77. };
  78. this.clickHandler = () => {
  79. this.check();
  80. };
  81. this.setContainerEl = (el) => {
  82. this.containerEl = el;
  83. };
  84. this.handleKeyDown = (event) => {
  85. const keys = ["ArrowLeft", "ArrowUp", "ArrowRight", "ArrowDown", " "];
  86. const { key } = event;
  87. const { el } = this;
  88. if (keys.indexOf(key) === -1) {
  89. return;
  90. }
  91. if (key === " ") {
  92. this.check();
  93. event.preventDefault();
  94. return;
  95. }
  96. let adjustedKey = key;
  97. if (getElementDir(el) === "rtl") {
  98. if (key === "ArrowRight") {
  99. adjustedKey = "ArrowLeft";
  100. }
  101. if (key === "ArrowLeft") {
  102. adjustedKey = "ArrowRight";
  103. }
  104. }
  105. const radioButtons = Array.from(this.rootNode.querySelectorAll("calcite-radio-button:not([hidden]")).filter((radioButton) => radioButton.name === this.name);
  106. let currentIndex = 0;
  107. const radioButtonsLength = radioButtons.length;
  108. radioButtons.some((item, index) => {
  109. if (item.checked) {
  110. currentIndex = index;
  111. return true;
  112. }
  113. });
  114. switch (adjustedKey) {
  115. case "ArrowLeft":
  116. case "ArrowUp":
  117. event.preventDefault();
  118. this.selectItem(radioButtons, getRoundRobinIndex(Math.max(currentIndex - 1, -1), radioButtonsLength));
  119. return;
  120. case "ArrowRight":
  121. case "ArrowDown":
  122. event.preventDefault();
  123. this.selectItem(radioButtons, getRoundRobinIndex(currentIndex + 1, radioButtonsLength));
  124. return;
  125. default:
  126. return;
  127. }
  128. };
  129. this.onContainerBlur = () => {
  130. this.focused = false;
  131. this.calciteInternalRadioButtonBlur.emit();
  132. };
  133. this.onContainerFocus = () => {
  134. if (!this.disabled) {
  135. this.focused = true;
  136. this.calciteInternalRadioButtonFocus.emit();
  137. }
  138. };
  139. }
  140. checkedChanged(newChecked) {
  141. if (newChecked) {
  142. this.uncheckOtherRadioButtonsInGroup();
  143. }
  144. this.calciteInternalRadioButtonCheckedChange.emit();
  145. }
  146. nameChanged() {
  147. this.checkLastRadioButton();
  148. }
  149. //--------------------------------------------------------------------------
  150. //
  151. // Public Methods
  152. //
  153. //--------------------------------------------------------------------------
  154. /** Sets focus on the component. */
  155. async setFocus() {
  156. if (!this.disabled) {
  157. focusElement(this.containerEl);
  158. }
  159. }
  160. onLabelClick(event) {
  161. if (!this.disabled && !this.hidden) {
  162. this.uncheckOtherRadioButtonsInGroup();
  163. const label = event.currentTarget;
  164. const radioButton = label.for
  165. ? this.rootNode.querySelector(`calcite-radio-button[id="${label.for}"]`)
  166. : label.querySelector(`calcite-radio-button[name="${this.name}"]`);
  167. if (radioButton) {
  168. radioButton.checked = true;
  169. radioButton.focused = true;
  170. }
  171. this.calciteRadioButtonChange.emit();
  172. this.setFocus();
  173. }
  174. }
  175. checkLastRadioButton() {
  176. const radioButtons = this.queryButtons();
  177. const checkedRadioButtons = radioButtons.filter((radioButton) => radioButton.checked);
  178. if ((checkedRadioButtons === null || checkedRadioButtons === void 0 ? void 0 : checkedRadioButtons.length) > 1) {
  179. const lastCheckedRadioButton = checkedRadioButtons[checkedRadioButtons.length - 1];
  180. checkedRadioButtons
  181. .filter((checkedRadioButton) => checkedRadioButton !== lastCheckedRadioButton)
  182. .forEach((checkedRadioButton) => {
  183. checkedRadioButton.checked = false;
  184. checkedRadioButton.emitCheckedChange();
  185. });
  186. }
  187. }
  188. /** @internal */
  189. async emitCheckedChange() {
  190. this.calciteInternalRadioButtonCheckedChange.emit();
  191. }
  192. uncheckAllRadioButtonsInGroup() {
  193. const radioButtons = this.queryButtons();
  194. radioButtons.forEach((radioButton) => {
  195. if (radioButton.checked) {
  196. radioButton.checked = false;
  197. radioButton.focused = false;
  198. }
  199. });
  200. }
  201. uncheckOtherRadioButtonsInGroup() {
  202. const radioButtons = this.queryButtons();
  203. const otherRadioButtons = radioButtons.filter((radioButton) => radioButton.guid !== this.guid);
  204. otherRadioButtons.forEach((otherRadioButton) => {
  205. if (otherRadioButton.checked) {
  206. otherRadioButton.checked = false;
  207. otherRadioButton.focused = false;
  208. }
  209. });
  210. }
  211. getTabIndex() {
  212. if (this.disabled) {
  213. return undefined;
  214. }
  215. return this.checked || this.isDefaultSelectable() ? 0 : -1;
  216. }
  217. //--------------------------------------------------------------------------
  218. //
  219. // Event Listeners
  220. //
  221. //--------------------------------------------------------------------------
  222. mouseenter() {
  223. this.hovered = true;
  224. }
  225. mouseleave() {
  226. this.hovered = false;
  227. }
  228. //--------------------------------------------------------------------------
  229. //
  230. // Lifecycle
  231. //
  232. //--------------------------------------------------------------------------
  233. connectedCallback() {
  234. this.rootNode = this.el.getRootNode();
  235. this.guid = this.el.id || `calcite-radio-button-${guid()}`;
  236. if (this.name) {
  237. this.checkLastRadioButton();
  238. }
  239. connectLabel(this);
  240. connectForm(this);
  241. }
  242. componentDidLoad() {
  243. if (this.focused && !this.disabled) {
  244. this.setFocus();
  245. }
  246. }
  247. disconnectedCallback() {
  248. disconnectLabel(this);
  249. disconnectForm(this);
  250. }
  251. componentDidRender() {
  252. updateHostInteraction(this);
  253. }
  254. // --------------------------------------------------------------------------
  255. //
  256. // Render Methods
  257. //
  258. // --------------------------------------------------------------------------
  259. render() {
  260. const tabIndex = this.getTabIndex();
  261. return (h(Host, { onClick: this.clickHandler, onKeyDown: this.handleKeyDown }, h("div", { "aria-checked": toAriaBoolean(this.checked), "aria-label": getLabelText(this), class: CSS.container, onBlur: this.onContainerBlur, onFocus: this.onContainerFocus, ref: this.setContainerEl, role: "radio", tabIndex: tabIndex }, h("div", { class: "radio" })), h(HiddenFormInputSlot, { component: this })));
  262. }
  263. get el() { return this; }
  264. static get watchers() { return {
  265. "checked": ["checkedChanged"],
  266. "name": ["nameChanged"]
  267. }; }
  268. static get style() { return radioButtonCss; }
  269. }, [1, "calcite-radio-button", {
  270. "checked": [1540],
  271. "disabled": [516],
  272. "focused": [1540],
  273. "guid": [1537],
  274. "hidden": [516],
  275. "hovered": [1540],
  276. "label": [1],
  277. "name": [513],
  278. "required": [516],
  279. "scale": [513],
  280. "value": [1032],
  281. "setFocus": [64],
  282. "emitCheckedChange": [64]
  283. }, [[1, "pointerenter", "mouseenter"], [1, "pointerleave", "mouseleave"]]]);
  284. function defineCustomElement$1() {
  285. if (typeof customElements === "undefined") {
  286. return;
  287. }
  288. const components = ["calcite-radio-button"];
  289. components.forEach(tagName => { switch (tagName) {
  290. case "calcite-radio-button":
  291. if (!customElements.get(tagName)) {
  292. customElements.define(tagName, RadioButton);
  293. }
  294. break;
  295. } });
  296. }
  297. defineCustomElement$1();
  298. const CalciteRadioButton = RadioButton;
  299. const defineCustomElement = defineCustomElement$1;
  300. export { CalciteRadioButton, defineCustomElement };