radio-group.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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 { Build, h, Host } from "@stencil/core";
  7. import { getElementDir } from "../../utils/dom";
  8. import { connectLabel, disconnectLabel } from "../../utils/label";
  9. import { afterConnectDefaultValueSet, connectForm, disconnectForm, HiddenFormInputSlot } from "../../utils/form";
  10. import { updateHostInteraction } from "../../utils/interactive";
  11. /**
  12. * @slot - A slot for adding `calcite-radio-group-item`s.
  13. */
  14. export class RadioGroup {
  15. constructor() {
  16. //--------------------------------------------------------------------------
  17. //
  18. // Properties
  19. //
  20. //--------------------------------------------------------------------------
  21. /** Specifies the appearance style of the component. */
  22. this.appearance = "solid";
  23. /** When `true`, interaction is prevented and the component is displayed with lower opacity. */
  24. this.disabled = false;
  25. /**
  26. * When `true`, the component must have a value in order for the form to submit.
  27. *
  28. * @internal
  29. */
  30. this.required = false;
  31. /** Defines the layout of the component. */
  32. this.layout = "horizontal";
  33. /** Specifies the size of the component. */
  34. this.scale = "m";
  35. /** The component's `selectedItem` value. */
  36. this.value = null;
  37. /** Specifies the width of the component. */
  38. this.width = "auto";
  39. //--------------------------------------------------------------------------
  40. //
  41. // Event Listeners
  42. //
  43. //--------------------------------------------------------------------------
  44. this.handleClick = (event) => {
  45. if (event.target.localName === "calcite-radio-group-item") {
  46. this.selectItem(event.target, true);
  47. }
  48. };
  49. }
  50. valueHandler(value) {
  51. const items = this.getItems();
  52. items.forEach((item) => (item.checked = item.value === value));
  53. }
  54. handleSelectedItemChange(newItem, oldItem) {
  55. this.value = newItem === null || newItem === void 0 ? void 0 : newItem.value;
  56. if (newItem === oldItem) {
  57. return;
  58. }
  59. const items = this.getItems();
  60. const match = Array.from(items)
  61. .filter((item) => item === newItem)
  62. .pop();
  63. if (match) {
  64. this.selectItem(match);
  65. }
  66. else if (items[0]) {
  67. items[0].tabIndex = 0;
  68. }
  69. }
  70. //--------------------------------------------------------------------------
  71. //
  72. // Lifecycle
  73. //
  74. //--------------------------------------------------------------------------
  75. componentWillLoad() {
  76. const items = this.getItems();
  77. const lastChecked = Array.from(items)
  78. .filter((item) => item.checked)
  79. .pop();
  80. if (lastChecked) {
  81. this.selectItem(lastChecked);
  82. }
  83. else if (items[0]) {
  84. items[0].tabIndex = 0;
  85. }
  86. }
  87. componentDidLoad() {
  88. afterConnectDefaultValueSet(this, this.value);
  89. }
  90. connectedCallback() {
  91. connectLabel(this);
  92. connectForm(this);
  93. }
  94. disconnectedCallback() {
  95. disconnectLabel(this);
  96. disconnectForm(this);
  97. }
  98. componentDidRender() {
  99. updateHostInteraction(this);
  100. }
  101. render() {
  102. return (h(Host, { onClick: this.handleClick, role: "radiogroup" }, h("slot", null), h(HiddenFormInputSlot, { component: this })));
  103. }
  104. handleSelected(event) {
  105. event.preventDefault();
  106. this.selectItem(event.target);
  107. event.stopPropagation();
  108. }
  109. handleKeyDown(event) {
  110. const keys = ["ArrowLeft", "ArrowUp", "ArrowRight", "ArrowDown", " "];
  111. const { key } = event;
  112. const { el, selectedItem } = this;
  113. if (keys.indexOf(key) === -1) {
  114. return;
  115. }
  116. let adjustedKey = key;
  117. if (getElementDir(el) === "rtl") {
  118. if (key === "ArrowRight") {
  119. adjustedKey = "ArrowLeft";
  120. }
  121. if (key === "ArrowLeft") {
  122. adjustedKey = "ArrowRight";
  123. }
  124. }
  125. const items = this.getItems();
  126. let selectedIndex = -1;
  127. items.forEach((item, index) => {
  128. if (item === selectedItem) {
  129. selectedIndex = index;
  130. }
  131. });
  132. switch (adjustedKey) {
  133. case "ArrowLeft":
  134. case "ArrowUp":
  135. event.preventDefault();
  136. const previous = selectedIndex < 1 ? items.item(items.length - 1) : items.item(selectedIndex - 1);
  137. this.selectItem(previous, true);
  138. return;
  139. case "ArrowRight":
  140. case "ArrowDown":
  141. event.preventDefault();
  142. const next = selectedIndex === -1 ? items.item(1) : items.item(selectedIndex + 1) || items.item(0);
  143. this.selectItem(next, true);
  144. return;
  145. case " ":
  146. event.preventDefault();
  147. this.selectItem(event.target, true);
  148. return;
  149. default:
  150. return;
  151. }
  152. }
  153. // --------------------------------------------------------------------------
  154. //
  155. // Methods
  156. //
  157. // --------------------------------------------------------------------------
  158. /** Sets focus on the component. */
  159. async setFocus() {
  160. var _a;
  161. (_a = (this.selectedItem || this.getItems()[0])) === null || _a === void 0 ? void 0 : _a.focus();
  162. }
  163. //--------------------------------------------------------------------------
  164. //
  165. // Private Methods
  166. //
  167. //--------------------------------------------------------------------------
  168. onLabelClick() {
  169. this.setFocus();
  170. }
  171. getItems() {
  172. return this.el.querySelectorAll("calcite-radio-group-item");
  173. }
  174. selectItem(selected, emit = false) {
  175. if (selected === this.selectedItem) {
  176. return;
  177. }
  178. const items = this.getItems();
  179. let match = null;
  180. items.forEach((item) => {
  181. const matches = item.value === selected.value;
  182. if ((matches && !item.checked) || (!matches && item.checked)) {
  183. item.checked = matches;
  184. }
  185. item.tabIndex = matches ? 0 : -1;
  186. if (matches) {
  187. match = item;
  188. if (emit) {
  189. this.calciteRadioGroupChange.emit(match.value);
  190. }
  191. }
  192. });
  193. this.selectedItem = match;
  194. if (Build.isBrowser && match) {
  195. match.focus();
  196. }
  197. }
  198. static get is() { return "calcite-radio-group"; }
  199. static get encapsulation() { return "shadow"; }
  200. static get originalStyleUrls() {
  201. return {
  202. "$": ["radio-group.scss"]
  203. };
  204. }
  205. static get styleUrls() {
  206. return {
  207. "$": ["radio-group.css"]
  208. };
  209. }
  210. static get properties() {
  211. return {
  212. "appearance": {
  213. "type": "string",
  214. "mutable": false,
  215. "complexType": {
  216. "original": "RadioAppearance",
  217. "resolved": "\"outline\" | \"solid\"",
  218. "references": {
  219. "RadioAppearance": {
  220. "location": "import",
  221. "path": "./interfaces"
  222. }
  223. }
  224. },
  225. "required": false,
  226. "optional": false,
  227. "docs": {
  228. "tags": [],
  229. "text": "Specifies the appearance style of the component."
  230. },
  231. "attribute": "appearance",
  232. "reflect": true,
  233. "defaultValue": "\"solid\""
  234. },
  235. "disabled": {
  236. "type": "boolean",
  237. "mutable": false,
  238. "complexType": {
  239. "original": "boolean",
  240. "resolved": "boolean",
  241. "references": {}
  242. },
  243. "required": false,
  244. "optional": false,
  245. "docs": {
  246. "tags": [],
  247. "text": "When `true`, interaction is prevented and the component is displayed with lower opacity."
  248. },
  249. "attribute": "disabled",
  250. "reflect": true,
  251. "defaultValue": "false"
  252. },
  253. "required": {
  254. "type": "boolean",
  255. "mutable": false,
  256. "complexType": {
  257. "original": "boolean",
  258. "resolved": "boolean",
  259. "references": {}
  260. },
  261. "required": false,
  262. "optional": false,
  263. "docs": {
  264. "tags": [{
  265. "name": "internal",
  266. "text": undefined
  267. }],
  268. "text": "When `true`, the component must have a value in order for the form to submit."
  269. },
  270. "attribute": "required",
  271. "reflect": true,
  272. "defaultValue": "false"
  273. },
  274. "layout": {
  275. "type": "string",
  276. "mutable": false,
  277. "complexType": {
  278. "original": "Layout",
  279. "resolved": "\"grid\" | \"horizontal\" | \"vertical\"",
  280. "references": {
  281. "Layout": {
  282. "location": "import",
  283. "path": "../interfaces"
  284. }
  285. }
  286. },
  287. "required": false,
  288. "optional": false,
  289. "docs": {
  290. "tags": [],
  291. "text": "Defines the layout of the component."
  292. },
  293. "attribute": "layout",
  294. "reflect": true,
  295. "defaultValue": "\"horizontal\""
  296. },
  297. "name": {
  298. "type": "string",
  299. "mutable": false,
  300. "complexType": {
  301. "original": "string",
  302. "resolved": "string",
  303. "references": {}
  304. },
  305. "required": false,
  306. "optional": false,
  307. "docs": {
  308. "tags": [],
  309. "text": "Specifies the name of the component on form submission."
  310. },
  311. "attribute": "name",
  312. "reflect": true
  313. },
  314. "scale": {
  315. "type": "string",
  316. "mutable": false,
  317. "complexType": {
  318. "original": "Scale",
  319. "resolved": "\"l\" | \"m\" | \"s\"",
  320. "references": {
  321. "Scale": {
  322. "location": "import",
  323. "path": "../interfaces"
  324. }
  325. }
  326. },
  327. "required": false,
  328. "optional": false,
  329. "docs": {
  330. "tags": [],
  331. "text": "Specifies the size of the component."
  332. },
  333. "attribute": "scale",
  334. "reflect": true,
  335. "defaultValue": "\"m\""
  336. },
  337. "value": {
  338. "type": "string",
  339. "mutable": true,
  340. "complexType": {
  341. "original": "string",
  342. "resolved": "string",
  343. "references": {}
  344. },
  345. "required": false,
  346. "optional": false,
  347. "docs": {
  348. "tags": [],
  349. "text": "The component's `selectedItem` value."
  350. },
  351. "attribute": "value",
  352. "reflect": false,
  353. "defaultValue": "null"
  354. },
  355. "selectedItem": {
  356. "type": "unknown",
  357. "mutable": true,
  358. "complexType": {
  359. "original": "HTMLCalciteRadioGroupItemElement",
  360. "resolved": "HTMLCalciteRadioGroupItemElement",
  361. "references": {
  362. "HTMLCalciteRadioGroupItemElement": {
  363. "location": "global"
  364. }
  365. }
  366. },
  367. "required": false,
  368. "optional": false,
  369. "docs": {
  370. "tags": [{
  371. "name": "readonly",
  372. "text": undefined
  373. }],
  374. "text": "The component's selected item `HTMLElement`."
  375. }
  376. },
  377. "width": {
  378. "type": "string",
  379. "mutable": false,
  380. "complexType": {
  381. "original": "Extract<\"auto\" | \"full\", Width>",
  382. "resolved": "\"auto\" | \"full\"",
  383. "references": {
  384. "Extract": {
  385. "location": "global"
  386. },
  387. "Width": {
  388. "location": "import",
  389. "path": "../interfaces"
  390. }
  391. }
  392. },
  393. "required": false,
  394. "optional": false,
  395. "docs": {
  396. "tags": [],
  397. "text": "Specifies the width of the component."
  398. },
  399. "attribute": "width",
  400. "reflect": true,
  401. "defaultValue": "\"auto\""
  402. }
  403. };
  404. }
  405. static get events() {
  406. return [{
  407. "method": "calciteRadioGroupChange",
  408. "name": "calciteRadioGroupChange",
  409. "bubbles": true,
  410. "cancelable": false,
  411. "composed": true,
  412. "docs": {
  413. "tags": [],
  414. "text": "Fires when the selected option changes, where the event detail is the new value."
  415. },
  416. "complexType": {
  417. "original": "string",
  418. "resolved": "string",
  419. "references": {}
  420. }
  421. }];
  422. }
  423. static get methods() {
  424. return {
  425. "setFocus": {
  426. "complexType": {
  427. "signature": "() => Promise<void>",
  428. "parameters": [],
  429. "references": {
  430. "Promise": {
  431. "location": "global"
  432. }
  433. },
  434. "return": "Promise<void>"
  435. },
  436. "docs": {
  437. "text": "Sets focus on the component.",
  438. "tags": []
  439. }
  440. }
  441. };
  442. }
  443. static get elementRef() { return "el"; }
  444. static get watchers() {
  445. return [{
  446. "propName": "value",
  447. "methodName": "valueHandler"
  448. }, {
  449. "propName": "selectedItem",
  450. "methodName": "handleSelectedItemChange"
  451. }];
  452. }
  453. static get listeners() {
  454. return [{
  455. "name": "calciteInternalRadioGroupItemChange",
  456. "method": "handleSelected",
  457. "target": undefined,
  458. "capture": false,
  459. "passive": false
  460. }, {
  461. "name": "keydown",
  462. "method": "handleKeyDown",
  463. "target": undefined,
  464. "capture": false,
  465. "passive": false
  466. }];
  467. }
  468. }