ion-datetime-button.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*!
  2. * (C) Ionic http://ionicframework.com - MIT License
  3. */
  4. import { proxyCustomElement, HTMLElement, h, Host } from '@stencil/core/internal/client';
  5. import { a as addEventListener, c as componentOnReady } from './helpers.js';
  6. import { p as printIonError } from './index4.js';
  7. import { c as createColorClasses } from './theme.js';
  8. import { b as getIonMode } from './ionic-global.js';
  9. import { s as parseDate, x as getToday, L as getHourCycle, N as getLocalizedDateTime, M as getLocalizedTime } from './data.js';
  10. import { d as defineCustomElement$2 } from './ripple-effect.js';
  11. const datetimeButtonIosCss = ":host{display:-ms-flexbox;display:flex;-ms-flex-wrap:wrap;flex-wrap:wrap;-ms-flex-align:center;align-items:center;-ms-flex-pack:center;justify-content:center}:host button{border-radius:8px;-webkit-margin-start:2px;margin-inline-start:2px;-webkit-margin-end:2px;margin-inline-end:2px;margin-top:0px;margin-bottom:0px;position:relative;-webkit-transition:150ms color ease-in-out;transition:150ms color ease-in-out;border:none;background:var(--ion-color-step-300, var(--ion-background-color-step-300, #edeef0));color:var(--ion-text-color, #000);font-family:inherit;font-size:1rem;cursor:pointer;overflow:hidden;-webkit-appearance:none;-moz-appearance:none;appearance:none}:host(.time-active) #time-button,:host(.date-active) #date-button{color:var(--ion-color-base)}:host(.datetime-button-disabled){pointer-events:none}:host(.datetime-button-disabled) button{opacity:0.4}:host button{-webkit-padding-start:13px;padding-inline-start:13px;-webkit-padding-end:13px;padding-inline-end:13px;padding-top:7px;padding-bottom:7px}:host button.ion-activated{color:var(--ion-color-step-600, var(--ion-text-color-step-400, #666666))}";
  12. const IonDatetimeButtonIosStyle0 = datetimeButtonIosCss;
  13. const datetimeButtonMdCss = ":host{display:-ms-flexbox;display:flex;-ms-flex-wrap:wrap;flex-wrap:wrap;-ms-flex-align:center;align-items:center;-ms-flex-pack:center;justify-content:center}:host button{border-radius:8px;-webkit-margin-start:2px;margin-inline-start:2px;-webkit-margin-end:2px;margin-inline-end:2px;margin-top:0px;margin-bottom:0px;position:relative;-webkit-transition:150ms color ease-in-out;transition:150ms color ease-in-out;border:none;background:var(--ion-color-step-300, var(--ion-background-color-step-300, #edeef0));color:var(--ion-text-color, #000);font-family:inherit;font-size:1rem;cursor:pointer;overflow:hidden;-webkit-appearance:none;-moz-appearance:none;appearance:none}:host(.time-active) #time-button,:host(.date-active) #date-button{color:var(--ion-color-base)}:host(.datetime-button-disabled){pointer-events:none}:host(.datetime-button-disabled) button{opacity:0.4}:host button{-webkit-padding-start:12px;padding-inline-start:12px;-webkit-padding-end:12px;padding-inline-end:12px;padding-top:6px;padding-bottom:6px}";
  14. const IonDatetimeButtonMdStyle0 = datetimeButtonMdCss;
  15. const DatetimeButton = /*@__PURE__*/ proxyCustomElement(class DatetimeButton extends HTMLElement {
  16. constructor() {
  17. super();
  18. this.__registerHost();
  19. this.__attachShadow();
  20. this.datetimeEl = null;
  21. this.overlayEl = null;
  22. /**
  23. * Accepts one or more string values and converts
  24. * them to DatetimeParts. This is done so datetime-button
  25. * can work with an array internally and not need
  26. * to keep checking if the datetime value is `string` or `string[]`.
  27. */
  28. this.getParsedDateValues = (value) => {
  29. if (value === undefined || value === null) {
  30. return [];
  31. }
  32. if (Array.isArray(value)) {
  33. return value;
  34. }
  35. return [value];
  36. };
  37. /**
  38. * Check the value property on the linked
  39. * ion-datetime and then format it according
  40. * to the locale specified on ion-datetime.
  41. */
  42. this.setDateTimeText = () => {
  43. var _a, _b, _c, _d, _e;
  44. const { datetimeEl, datetimePresentation } = this;
  45. if (!datetimeEl) {
  46. return;
  47. }
  48. const { value, locale, formatOptions, hourCycle, preferWheel, multiple, titleSelectedDatesFormatter } = datetimeEl;
  49. const parsedValues = this.getParsedDateValues(value);
  50. /**
  51. * Both ion-datetime and ion-datetime-button default
  52. * to today's date and time if no value is set.
  53. */
  54. const parsedDatetimes = parseDate(parsedValues.length > 0 ? parsedValues : [getToday()]);
  55. if (!parsedDatetimes) {
  56. return;
  57. }
  58. /**
  59. * If developers incorrectly use multiple="true"
  60. * with non "date" datetimes, then just select
  61. * the first value so the interface does
  62. * not appear broken. Datetime will provide a
  63. * warning in the console.
  64. */
  65. const firstParsedDatetime = parsedDatetimes[0];
  66. const computedHourCycle = getHourCycle(locale, hourCycle);
  67. this.dateText = this.timeText = undefined;
  68. switch (datetimePresentation) {
  69. case 'date-time':
  70. case 'time-date':
  71. const dateText = getLocalizedDateTime(locale, firstParsedDatetime, (_a = formatOptions === null || formatOptions === void 0 ? void 0 : formatOptions.date) !== null && _a !== void 0 ? _a : { month: 'short', day: 'numeric', year: 'numeric' });
  72. const timeText = getLocalizedTime(locale, firstParsedDatetime, computedHourCycle, formatOptions === null || formatOptions === void 0 ? void 0 : formatOptions.time);
  73. if (preferWheel) {
  74. this.dateText = `${dateText} ${timeText}`;
  75. }
  76. else {
  77. this.dateText = dateText;
  78. this.timeText = timeText;
  79. }
  80. break;
  81. case 'date':
  82. if (multiple && parsedValues.length !== 1) {
  83. let headerText = `${parsedValues.length} days`; // default/fallback for multiple selection
  84. if (titleSelectedDatesFormatter !== undefined) {
  85. try {
  86. headerText = titleSelectedDatesFormatter(parsedValues);
  87. }
  88. catch (e) {
  89. printIonError('[ion-datetime-button] - Exception in provided `titleSelectedDatesFormatter`:', e);
  90. }
  91. }
  92. this.dateText = headerText;
  93. }
  94. else {
  95. this.dateText = getLocalizedDateTime(locale, firstParsedDatetime, (_b = formatOptions === null || formatOptions === void 0 ? void 0 : formatOptions.date) !== null && _b !== void 0 ? _b : { month: 'short', day: 'numeric', year: 'numeric' });
  96. }
  97. break;
  98. case 'time':
  99. this.timeText = getLocalizedTime(locale, firstParsedDatetime, computedHourCycle, formatOptions === null || formatOptions === void 0 ? void 0 : formatOptions.time);
  100. break;
  101. case 'month-year':
  102. this.dateText = getLocalizedDateTime(locale, firstParsedDatetime, (_c = formatOptions === null || formatOptions === void 0 ? void 0 : formatOptions.date) !== null && _c !== void 0 ? _c : { month: 'long', year: 'numeric' });
  103. break;
  104. case 'month':
  105. this.dateText = getLocalizedDateTime(locale, firstParsedDatetime, (_d = formatOptions === null || formatOptions === void 0 ? void 0 : formatOptions.time) !== null && _d !== void 0 ? _d : { month: 'long' });
  106. break;
  107. case 'year':
  108. this.dateText = getLocalizedDateTime(locale, firstParsedDatetime, (_e = formatOptions === null || formatOptions === void 0 ? void 0 : formatOptions.time) !== null && _e !== void 0 ? _e : { year: 'numeric' });
  109. break;
  110. }
  111. };
  112. /**
  113. * Waits for the ion-datetime to re-render.
  114. * This is needed in order to correctly position
  115. * a popover relative to the trigger element.
  116. */
  117. this.waitForDatetimeChanges = async () => {
  118. const { datetimeEl } = this;
  119. if (!datetimeEl) {
  120. return Promise.resolve();
  121. }
  122. return new Promise((resolve) => {
  123. addEventListener(datetimeEl, 'ionRender', resolve, { once: true });
  124. });
  125. };
  126. this.handleDateClick = async (ev) => {
  127. const { datetimeEl, datetimePresentation } = this;
  128. if (!datetimeEl) {
  129. return;
  130. }
  131. let needsPresentationChange = false;
  132. /**
  133. * When clicking the date button,
  134. * we need to make sure that only a date
  135. * picker is displayed. For presentation styles
  136. * that display content other than a date picker,
  137. * we need to update the presentation style.
  138. */
  139. switch (datetimePresentation) {
  140. case 'date-time':
  141. case 'time-date':
  142. const needsChange = datetimeEl.presentation !== 'date';
  143. /**
  144. * The date+time wheel picker
  145. * shows date and time together,
  146. * so do not adjust the presentation
  147. * in that case.
  148. */
  149. if (!datetimeEl.preferWheel && needsChange) {
  150. datetimeEl.presentation = 'date';
  151. needsPresentationChange = true;
  152. }
  153. break;
  154. }
  155. /**
  156. * Track which button was clicked
  157. * so that it can have the correct
  158. * activated styles applied when
  159. * the modal/popover containing
  160. * the datetime is opened.
  161. */
  162. this.selectedButton = 'date';
  163. this.presentOverlay(ev, needsPresentationChange, this.dateTargetEl);
  164. };
  165. this.handleTimeClick = (ev) => {
  166. const { datetimeEl, datetimePresentation } = this;
  167. if (!datetimeEl) {
  168. return;
  169. }
  170. let needsPresentationChange = false;
  171. /**
  172. * When clicking the time button,
  173. * we need to make sure that only a time
  174. * picker is displayed. For presentation styles
  175. * that display content other than a time picker,
  176. * we need to update the presentation style.
  177. */
  178. switch (datetimePresentation) {
  179. case 'date-time':
  180. case 'time-date':
  181. const needsChange = datetimeEl.presentation !== 'time';
  182. if (needsChange) {
  183. datetimeEl.presentation = 'time';
  184. needsPresentationChange = true;
  185. }
  186. break;
  187. }
  188. /**
  189. * Track which button was clicked
  190. * so that it can have the correct
  191. * activated styles applied when
  192. * the modal/popover containing
  193. * the datetime is opened.
  194. */
  195. this.selectedButton = 'time';
  196. this.presentOverlay(ev, needsPresentationChange, this.timeTargetEl);
  197. };
  198. /**
  199. * If the datetime is presented in an
  200. * overlay, the datetime and overlay
  201. * should be appropriately sized.
  202. * These classes provide default sizing values
  203. * that developers can customize.
  204. * The goal is to provide an overlay that is
  205. * reasonably sized with a datetime that
  206. * fills the entire container.
  207. */
  208. this.presentOverlay = async (ev, needsPresentationChange, triggerEl) => {
  209. const { overlayEl } = this;
  210. if (!overlayEl) {
  211. return;
  212. }
  213. if (overlayEl.tagName === 'ION-POPOVER') {
  214. /**
  215. * When the presentation on datetime changes,
  216. * we need to wait for the component to re-render
  217. * otherwise the computed width/height of the
  218. * popover content will be wrong, causing
  219. * the popover to not align with the trigger element.
  220. */
  221. if (needsPresentationChange) {
  222. await this.waitForDatetimeChanges();
  223. }
  224. /**
  225. * We pass the trigger button element
  226. * so that the popover aligns with the individual
  227. * button that was clicked, not the component container.
  228. */
  229. overlayEl.present(Object.assign(Object.assign({}, ev), { detail: {
  230. ionShadowTarget: triggerEl,
  231. } }));
  232. }
  233. else {
  234. overlayEl.present();
  235. }
  236. };
  237. this.datetimePresentation = 'date-time';
  238. this.dateText = undefined;
  239. this.timeText = undefined;
  240. this.datetimeActive = false;
  241. this.selectedButton = undefined;
  242. this.color = 'primary';
  243. this.disabled = false;
  244. this.datetime = undefined;
  245. }
  246. async componentWillLoad() {
  247. const { datetime } = this;
  248. if (!datetime) {
  249. printIonError('[ion-datetime-button] - An ID associated with an ion-datetime instance is required to function properly.', this.el);
  250. return;
  251. }
  252. const datetimeEl = (this.datetimeEl = document.getElementById(datetime));
  253. if (!datetimeEl) {
  254. printIonError(`[ion-datetime-button] - No ion-datetime instance found for ID '${datetime}'.`, this.el);
  255. return;
  256. }
  257. /**
  258. * The element reference must be an ion-datetime. Print an error
  259. * if a non-datetime element was provided.
  260. */
  261. if (datetimeEl.tagName !== 'ION-DATETIME') {
  262. printIonError(`[ion-datetime-button] - Expected an ion-datetime instance for ID '${datetime}' but received '${datetimeEl.tagName.toLowerCase()}' instead.`, datetimeEl);
  263. return;
  264. }
  265. /**
  266. * Since the datetime can be used in any context (overlays, accordion, etc)
  267. * we track when it is visible to determine when it is active.
  268. * This informs which button is highlighted as well as the
  269. * aria-expanded state.
  270. */
  271. const io = new IntersectionObserver((entries) => {
  272. const ev = entries[0];
  273. this.datetimeActive = ev.isIntersecting;
  274. }, {
  275. threshold: 0.01,
  276. });
  277. io.observe(datetimeEl);
  278. /**
  279. * Get a reference to any modal/popover
  280. * the datetime is being used in so we can
  281. * correctly size it when it is presented.
  282. */
  283. const overlayEl = (this.overlayEl = datetimeEl.closest('ion-modal, ion-popover'));
  284. /**
  285. * The .ion-datetime-button-overlay class contains
  286. * styles that allow any modal/popover to be
  287. * sized according to the dimensions of the datetime.
  288. * If developers want a smaller/larger overlay all they need
  289. * to do is change the width/height of the datetime.
  290. * Additionally, this lets us avoid having to set
  291. * explicit widths on each variant of datetime.
  292. */
  293. if (overlayEl) {
  294. overlayEl.classList.add('ion-datetime-button-overlay');
  295. }
  296. componentOnReady(datetimeEl, () => {
  297. const datetimePresentation = (this.datetimePresentation = datetimeEl.presentation || 'date-time');
  298. /**
  299. * Set the initial display
  300. * in the rendered buttons.
  301. *
  302. * From there, we need to listen
  303. * for ionChange to be emitted
  304. * from datetime so we know when
  305. * to re-render the displayed
  306. * text in the buttons.
  307. */
  308. this.setDateTimeText();
  309. addEventListener(datetimeEl, 'ionValueChange', this.setDateTimeText);
  310. /**
  311. * Configure the initial selected button
  312. * in the event that the datetime is displayed
  313. * without clicking one of the datetime buttons.
  314. * For example, a datetime could be expanded
  315. * in an accordion. In this case users only
  316. * need to click the accordion header to show
  317. * the datetime.
  318. */
  319. switch (datetimePresentation) {
  320. case 'date-time':
  321. case 'date':
  322. case 'month-year':
  323. case 'month':
  324. case 'year':
  325. this.selectedButton = 'date';
  326. break;
  327. case 'time-date':
  328. case 'time':
  329. this.selectedButton = 'time';
  330. break;
  331. }
  332. });
  333. }
  334. render() {
  335. const { color, dateText, timeText, selectedButton, datetimeActive, disabled } = this;
  336. const mode = getIonMode(this);
  337. return (h(Host, { key: '11d037e6ab061e5116842970760b04850b42f2c7', class: createColorClasses(color, {
  338. [mode]: true,
  339. [`${selectedButton}-active`]: datetimeActive,
  340. ['datetime-button-disabled']: disabled,
  341. }) }, dateText && (h("button", { key: '08ecb62da0fcbf7466a1f2403276712a3ff17fbc', class: "ion-activatable", id: "date-button", "aria-expanded": datetimeActive ? 'true' : 'false', onClick: this.handleDateClick, disabled: disabled, part: "native", ref: (el) => (this.dateTargetEl = el) }, h("slot", { key: '1c04853d4d23c0f1a594602bde44511c98355644', name: "date-target" }, dateText), mode === 'md' && h("ion-ripple-effect", { key: '5fc566cd4bc885bcf983ce99e3dc65d7f485bf9b' }))), timeText && (h("button", { key: 'c9c5c34ac338badf8659da22bea5829d62c51169', class: "ion-activatable", id: "time-button", "aria-expanded": datetimeActive ? 'true' : 'false', onClick: this.handleTimeClick, disabled: disabled, part: "native", ref: (el) => (this.timeTargetEl = el) }, h("slot", { key: '147a9d2069dbf737f6fc64787823d6d5af5aa653', name: "time-target" }, timeText), mode === 'md' && h("ion-ripple-effect", { key: '70a5e25b75ed90ac6bba003468435f67aa9d8f0a' })))));
  342. }
  343. get el() { return this; }
  344. static get style() { return {
  345. ios: IonDatetimeButtonIosStyle0,
  346. md: IonDatetimeButtonMdStyle0
  347. }; }
  348. }, [33, "ion-datetime-button", {
  349. "color": [513],
  350. "disabled": [516],
  351. "datetime": [1],
  352. "datetimePresentation": [32],
  353. "dateText": [32],
  354. "timeText": [32],
  355. "datetimeActive": [32],
  356. "selectedButton": [32]
  357. }]);
  358. function defineCustomElement$1() {
  359. if (typeof customElements === "undefined") {
  360. return;
  361. }
  362. const components = ["ion-datetime-button", "ion-ripple-effect"];
  363. components.forEach(tagName => { switch (tagName) {
  364. case "ion-datetime-button":
  365. if (!customElements.get(tagName)) {
  366. customElements.define(tagName, DatetimeButton);
  367. }
  368. break;
  369. case "ion-ripple-effect":
  370. if (!customElements.get(tagName)) {
  371. defineCustomElement$2();
  372. }
  373. break;
  374. } });
  375. }
  376. const IonDatetimeButton = DatetimeButton;
  377. const defineCustomElement = defineCustomElement$1;
  378. export { IonDatetimeButton, defineCustomElement };