select-popover.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*!
  2. * (C) Ionic http://ionicframework.com - MIT License
  3. */
  4. import { proxyCustomElement, HTMLElement, h, forceUpdate, Host } from '@stencil/core/internal/client';
  5. import { s as safeCall } from './overlays.js';
  6. import { g as getClassMap } from './theme.js';
  7. import { b as getIonMode } from './ionic-global.js';
  8. import { d as defineCustomElement$9 } from './checkbox.js';
  9. import { d as defineCustomElement$8 } from './icon.js';
  10. import { d as defineCustomElement$7 } from './item.js';
  11. import { d as defineCustomElement$6 } from './label.js';
  12. import { d as defineCustomElement$5 } from './list.js';
  13. import { d as defineCustomElement$4 } from './list-header.js';
  14. import { d as defineCustomElement$3 } from './radio.js';
  15. import { d as defineCustomElement$2 } from './radio-group.js';
  16. import { d as defineCustomElement$1 } from './ripple-effect.js';
  17. const selectPopoverIosCss = ".sc-ion-select-popover-ios-h ion-list.sc-ion-select-popover-ios{margin-left:0;margin-right:0;margin-top:0;margin-bottom:0}ion-list-header.sc-ion-select-popover-ios,ion-label.sc-ion-select-popover-ios{margin-left:0;margin-right:0;margin-top:0;margin-bottom:0}.sc-ion-select-popover-ios-h{overflow-y:auto}";
  18. const IonSelectPopoverIosStyle0 = selectPopoverIosCss;
  19. const selectPopoverMdCss = ".sc-ion-select-popover-md-h ion-list.sc-ion-select-popover-md{margin-left:0;margin-right:0;margin-top:0;margin-bottom:0}ion-list-header.sc-ion-select-popover-md,ion-label.sc-ion-select-popover-md{margin-left:0;margin-right:0;margin-top:0;margin-bottom:0}.sc-ion-select-popover-md-h{overflow-y:auto}ion-list.sc-ion-select-popover-md ion-radio.sc-ion-select-popover-md::part(container){display:none}ion-list.sc-ion-select-popover-md ion-radio.sc-ion-select-popover-md::part(label){margin-left:0;margin-right:0;margin-top:0;margin-bottom:0}ion-item.sc-ion-select-popover-md{--inner-border-width:0}.item-radio-checked.sc-ion-select-popover-md{--background:rgba(var(--ion-color-primary-rgb, 0, 84, 233), 0.08);--background-focused:var(--ion-color-primary, #0054e9);--background-focused-opacity:0.2;--background-hover:var(--ion-color-primary, #0054e9);--background-hover-opacity:0.12}.item-checkbox-checked.sc-ion-select-popover-md{--background-activated:var(--ion-item-color, var(--ion-text-color, #000));--background-focused:var(--ion-item-color, var(--ion-text-color, #000));--background-hover:var(--ion-item-color, var(--ion-text-color, #000));--color:var(--ion-color-primary, #0054e9)}";
  20. const IonSelectPopoverMdStyle0 = selectPopoverMdCss;
  21. const SelectPopover = /*@__PURE__*/ proxyCustomElement(class SelectPopover extends HTMLElement {
  22. constructor() {
  23. super();
  24. this.__registerHost();
  25. this.header = undefined;
  26. this.subHeader = undefined;
  27. this.message = undefined;
  28. this.multiple = undefined;
  29. this.options = [];
  30. }
  31. findOptionFromEvent(ev) {
  32. const { options } = this;
  33. return options.find((o) => o.value === ev.target.value);
  34. }
  35. /**
  36. * When an option is selected we need to get the value(s)
  37. * of the selected option(s) and return it in the option
  38. * handler
  39. */
  40. callOptionHandler(ev) {
  41. const option = this.findOptionFromEvent(ev);
  42. const values = this.getValues(ev);
  43. if (option === null || option === void 0 ? void 0 : option.handler) {
  44. safeCall(option.handler, values);
  45. }
  46. }
  47. /**
  48. * Dismisses the host popover that the `ion-select-popover`
  49. * is rendered within.
  50. */
  51. dismissParentPopover() {
  52. const popover = this.el.closest('ion-popover');
  53. if (popover) {
  54. popover.dismiss();
  55. }
  56. }
  57. setChecked(ev) {
  58. const { multiple } = this;
  59. const option = this.findOptionFromEvent(ev);
  60. // this is a popover with checkboxes (multiple value select)
  61. // we need to set the checked value for this option
  62. if (multiple && option) {
  63. option.checked = ev.detail.checked;
  64. }
  65. }
  66. getValues(ev) {
  67. const { multiple, options } = this;
  68. if (multiple) {
  69. // this is a popover with checkboxes (multiple value select)
  70. // return an array of all the checked values
  71. return options.filter((o) => o.checked).map((o) => o.value);
  72. }
  73. // this is a popover with radio buttons (single value select)
  74. // return the value that was clicked, otherwise undefined
  75. const option = this.findOptionFromEvent(ev);
  76. return option ? option.value : undefined;
  77. }
  78. renderOptions(options) {
  79. const { multiple } = this;
  80. switch (multiple) {
  81. case true:
  82. return this.renderCheckboxOptions(options);
  83. default:
  84. return this.renderRadioOptions(options);
  85. }
  86. }
  87. renderCheckboxOptions(options) {
  88. return options.map((option) => (h("ion-item", { class: Object.assign({
  89. // TODO FW-4784
  90. 'item-checkbox-checked': option.checked
  91. }, getClassMap(option.cssClass)) }, h("ion-checkbox", { value: option.value, disabled: option.disabled, checked: option.checked, justify: "start", labelPlacement: "end", onIonChange: (ev) => {
  92. this.setChecked(ev);
  93. this.callOptionHandler(ev);
  94. // TODO FW-4784
  95. forceUpdate(this);
  96. } }, option.text))));
  97. }
  98. renderRadioOptions(options) {
  99. const checked = options.filter((o) => o.checked).map((o) => o.value)[0];
  100. return (h("ion-radio-group", { value: checked, onIonChange: (ev) => this.callOptionHandler(ev) }, options.map((option) => (h("ion-item", { class: Object.assign({
  101. // TODO FW-4784
  102. 'item-radio-checked': option.value === checked
  103. }, getClassMap(option.cssClass)) }, h("ion-radio", { value: option.value, disabled: option.disabled, onClick: () => this.dismissParentPopover(), onKeyUp: (ev) => {
  104. if (ev.key === ' ') {
  105. /**
  106. * Selecting a radio option with keyboard navigation,
  107. * either through the Enter or Space keys, should
  108. * dismiss the popover.
  109. */
  110. this.dismissParentPopover();
  111. }
  112. } }, option.text))))));
  113. }
  114. render() {
  115. const { header, message, options, subHeader } = this;
  116. const hasSubHeaderOrMessage = subHeader !== undefined || message !== undefined;
  117. return (h(Host, { key: '542367ab8fb72bfebf7e65630b91017d68827fd6', class: getIonMode(this) }, h("ion-list", { key: 'f2f0f37e1365cd7780b02de1a1698700d0df48a7' }, header !== undefined && h("ion-list-header", { key: '4b8800a68e800f19277a44b7074ca24b70218daf' }, header), hasSubHeaderOrMessage && (h("ion-item", { key: '932b7903daf97d5a57d289b7ee49e868bb9b0cf5' }, h("ion-label", { key: 'fc3f1b69aa2a0bc6125d35692dcad3a8a99fd160', class: "ion-text-wrap" }, subHeader !== undefined && h("h3", { key: 'eceab2f47afa95f04b138342b0bdbfa1f50919a8' }, subHeader), message !== undefined && h("p", { key: '70f4e27ad1316318efd0c17efce31e5e45c8fa02' }, message)))), this.renderOptions(options))));
  118. }
  119. get el() { return this; }
  120. static get style() { return {
  121. ios: IonSelectPopoverIosStyle0,
  122. md: IonSelectPopoverMdStyle0
  123. }; }
  124. }, [34, "ion-select-popover", {
  125. "header": [1],
  126. "subHeader": [1, "sub-header"],
  127. "message": [1],
  128. "multiple": [4],
  129. "options": [16]
  130. }]);
  131. function defineCustomElement() {
  132. if (typeof customElements === "undefined") {
  133. return;
  134. }
  135. const components = ["ion-select-popover", "ion-checkbox", "ion-icon", "ion-item", "ion-label", "ion-list", "ion-list-header", "ion-radio", "ion-radio-group", "ion-ripple-effect"];
  136. components.forEach(tagName => { switch (tagName) {
  137. case "ion-select-popover":
  138. if (!customElements.get(tagName)) {
  139. customElements.define(tagName, SelectPopover);
  140. }
  141. break;
  142. case "ion-checkbox":
  143. if (!customElements.get(tagName)) {
  144. defineCustomElement$9();
  145. }
  146. break;
  147. case "ion-icon":
  148. if (!customElements.get(tagName)) {
  149. defineCustomElement$8();
  150. }
  151. break;
  152. case "ion-item":
  153. if (!customElements.get(tagName)) {
  154. defineCustomElement$7();
  155. }
  156. break;
  157. case "ion-label":
  158. if (!customElements.get(tagName)) {
  159. defineCustomElement$6();
  160. }
  161. break;
  162. case "ion-list":
  163. if (!customElements.get(tagName)) {
  164. defineCustomElement$5();
  165. }
  166. break;
  167. case "ion-list-header":
  168. if (!customElements.get(tagName)) {
  169. defineCustomElement$4();
  170. }
  171. break;
  172. case "ion-radio":
  173. if (!customElements.get(tagName)) {
  174. defineCustomElement$3();
  175. }
  176. break;
  177. case "ion-radio-group":
  178. if (!customElements.get(tagName)) {
  179. defineCustomElement$2();
  180. }
  181. break;
  182. case "ion-ripple-effect":
  183. if (!customElements.get(tagName)) {
  184. defineCustomElement$1();
  185. }
  186. break;
  187. } });
  188. }
  189. export { SelectPopover as S, defineCustomElement as d };