DateRangePicker.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. <!--
  2. Date range picker, taken from the following non-maintained project https://github.com/praveenpuglia/vuetify-daterange-picker
  3. -->
  4. <template>
  5. <div class="v-date-range">
  6. <v-menu
  7. v-model="menu"
  8. :close-on-content-click="false"
  9. offset-y
  10. v-bind="menuProps"
  11. >
  12. <template v-slot:activator="{ on }">
  13. <v-text-field
  14. v-on="on"
  15. class="v-date-range__input-field"
  16. :value="inputValue"
  17. readonly
  18. clearable
  19. hide-details
  20. :disabled="disabled"
  21. @click:clear="reset"
  22. :label="placeholder"
  23. append-icon="mdi-calendar"
  24. v-bind="inputProps"
  25. ></v-text-field>
  26. </template>
  27. <v-card class="v-date-range__menu-content">
  28. <v-card-text>
  29. <div
  30. :data-days="highlightDates.length"
  31. :class="{
  32. 'v-date-range__pickers': true,
  33. 'v-date-range--highlighted': highlightDates.length
  34. }"
  35. >
  36. <v-card-title v-if="$slots.title">
  37. <slot name="title" v-if="$slots.title"></slot>
  38. </v-card-title>
  39. <v-card-text>
  40. <div class="v-date-range__content">
  41. <v-list v-if="!noPresets" class="mr-4">
  42. <v-subheader>{{ presetLabel }}</v-subheader>
  43. <v-list-item
  44. v-for="(preset, index) in presets"
  45. v-model="isPresetActive[index]"
  46. :key="index"
  47. @click="selectPreset(index)"
  48. >
  49. <v-list-item-content>
  50. {{ preset.label }}
  51. </v-list-item-content>
  52. </v-list-item>
  53. </v-list>
  54. <v-date-picker
  55. class="mr-4 v-date-range__picker--start v-date-range__picker"
  56. v-model="pickerStart"
  57. :locale="locale"
  58. :first-day-of-week="firstDayOfWeek"
  59. :min="min"
  60. :max="pickerEnd || max"
  61. :no-title="noTitle"
  62. :next-icon="nextIcon"
  63. :prev-icon="prevIcon"
  64. :events="highlightDates"
  65. :event-color="highlightClasses"
  66. ></v-date-picker>
  67. <v-date-picker
  68. class="v-date-range__picker--end v-date-range__picker"
  69. v-model="pickerEnd"
  70. :locale="locale"
  71. :first-day-of-week="firstDayOfWeek"
  72. :min="pickerStart || min"
  73. :max="max"
  74. :no-title="noTitle"
  75. :next-icon="nextIcon"
  76. :prev-icon="prevIcon"
  77. :events="highlightDates"
  78. :event-color="highlightClasses"
  79. ></v-date-picker>
  80. </div>
  81. </v-card-text>
  82. </div>
  83. </v-card-text>
  84. <v-card-actions>
  85. <v-spacer></v-spacer>
  86. <v-btn text @click="reset">{{ mergedActionLabels.reset }}</v-btn>
  87. <v-btn text @click="menu = false">{{
  88. mergedActionLabels.cancel
  89. }}</v-btn>
  90. <v-btn
  91. @click="applyRange"
  92. color="primary"
  93. :disabled="!bothSelected"
  94. >{{ mergedActionLabels.apply }}</v-btn
  95. >
  96. </v-card-actions>
  97. </v-card>
  98. </v-menu>
  99. </div>
  100. </template>
  101. <script lang="ts">
  102. import { format, parse, differenceInCalendarDays, addDays } from 'date-fns';
  103. import { formatIsoDate } from '@/services/utils/date'
  104. import Vue from "vue";
  105. interface ActionLabels { apply: string, cancel: string, reset: string }
  106. const ISO_FORMAT: string = 'yyyy-MM-dd';
  107. const DEFAULT_DATE: string = format(new Date(), ISO_FORMAT);
  108. const DEFAULT_ACTION_LABELS: ActionLabels = {
  109. apply: 'Apply',
  110. cancel: 'Cancel',
  111. reset: 'Reset'
  112. };
  113. export default Vue.extend({
  114. props: {
  115. // Take start and end as the input. Passable via v-model.
  116. value: {
  117. type: Object as () => DateRange,
  118. default: () => {
  119. return { start: '', end: '' };
  120. }
  121. },
  122. disabled: {
  123. type: Boolean as () => boolean,
  124. default: false
  125. },
  126. presets: {
  127. type: Array as () => Array<DateRangePreset>,
  128. default: () => {
  129. return [];
  130. }
  131. },
  132. noPresets: {
  133. type: Boolean as () => boolean,
  134. default: false
  135. },
  136. // Denotes the Placeholder string for start date.
  137. startLabel: {
  138. type: String as () => string,
  139. default: 'Start Date'
  140. },
  141. // Denotes the Placeholder string for start date.
  142. endLabel: {
  143. type: String as () => string,
  144. default: 'End Date'
  145. },
  146. // The string that gets placed between `startLabel` and `endLabel`
  147. separatorLabel: {
  148. type: String as () => string,
  149. default: 'To'
  150. },
  151. presetLabel: {
  152. type: String as () => string,
  153. default: 'Presets'
  154. },
  155. actionLabels: {
  156. type: Object as () => ActionLabels,
  157. default: () => {
  158. return DEFAULT_ACTION_LABELS;
  159. }
  160. },
  161. /**
  162. * Following values are all passable to vuetify's own datepicker
  163. * component.
  164. */
  165. // Min selectable date.
  166. min: {
  167. type: String as () => string,
  168. default: undefined
  169. },
  170. // Max selectable date
  171. max: {
  172. type: String as () => string,
  173. default: undefined
  174. },
  175. // Locale
  176. locale: {
  177. type: String as () => string,
  178. default: 'en-us'
  179. },
  180. firstDayOfWeek: {
  181. type: [String, Number] as [() => string, () => number],
  182. default: 0
  183. },
  184. noTitle: {
  185. type: Boolean as () => boolean,
  186. default: false
  187. },
  188. displayFormat: {
  189. type: String as () => string
  190. },
  191. highlightColor: {
  192. type: String as () => string,
  193. default: 'blue lighten-5'
  194. },
  195. showReset: {
  196. type: Boolean as () => boolean,
  197. default: true
  198. },
  199. /**
  200. * Icons
  201. */
  202. nextIcon: {
  203. type: String,
  204. default: '$vuetify.icons.next'
  205. },
  206. prevIcon: {
  207. type: String,
  208. default: '$vuetify.icons.prev'
  209. },
  210. inputProps: {
  211. type: Object,
  212. default: () => {
  213. return {};
  214. }
  215. },
  216. menuProps: {
  217. type: Object,
  218. default: () => {
  219. return {};
  220. }
  221. }
  222. },
  223. data() {
  224. return {
  225. menu: false,
  226. pickerStart: this.value.start as string,
  227. pickerEnd: this.value.end as string,
  228. highlightDates: [] as Array<string>,
  229. highlightClasses: {}
  230. };
  231. },
  232. computed: {
  233. inputValue(): string {
  234. if (this.isValueEmpty) {
  235. return '';
  236. }
  237. const start = this.displayFormat
  238. ? formatIsoDate(this.value.start, this.displayFormat)
  239. : this.value.start;
  240. const end = this.displayFormat
  241. ? formatIsoDate(this.value.end, this.displayFormat)
  242. : this.value.end;
  243. return `${start} ${this.separatorLabel} ${end}`;
  244. },
  245. placeholder(): string {
  246. return `${this.startLabel} ${this.separatorLabel} ${this.endLabel}`;
  247. },
  248. /**
  249. * If the value prop doesn't have any start value,
  250. * its most likely that an empty object was passed.
  251. */
  252. isValueEmpty(): boolean {
  253. return !this.value.start;
  254. },
  255. /**
  256. * If the user has selected both the dates or not
  257. */
  258. bothSelected(): boolean {
  259. return !!this.pickerStart && !!this.pickerEnd;
  260. },
  261. isPresetActive(): Array<boolean> {
  262. return this.presets.map(
  263. preset =>
  264. preset.range.start === this.pickerStart &&
  265. preset.range.end === this.pickerEnd
  266. );
  267. },
  268. mergedActionLabels(): ActionLabels {
  269. return { ...DEFAULT_ACTION_LABELS, ...this.actionLabels };
  270. }
  271. },
  272. methods: {
  273. /**
  274. * Emit the input event with the updated range data.
  275. * This makes v-model work.
  276. */
  277. applyRange() {
  278. this.menu = false;
  279. this.emitRange();
  280. },
  281. /**
  282. * Called every time the menu is closed.
  283. * It also emits an event to tell the parent
  284. * that the menu has closed.
  285. *
  286. * Upon closing the datepicker values are set
  287. * to the current selected value.
  288. */
  289. closeMenu() {
  290. // Reset the changed values for datepicker models.
  291. this.pickerStart = this.value.start;
  292. this.pickerEnd = this.value.end;
  293. this.highlight();
  294. this.$emit('menu-closed');
  295. },
  296. highlight() {
  297. if (!this.bothSelected) {
  298. return;
  299. }
  300. const dates = [];
  301. const classes = {} as Array<string>;
  302. const start = parse(this.pickerStart, ISO_FORMAT, new Date());
  303. const end = parse(this.pickerEnd, ISO_FORMAT, new Date());
  304. const diff = Math.abs(differenceInCalendarDays(start, end));
  305. // Loop though all the days in range.
  306. for (let i = 0; i <= diff; i++) {
  307. const date: string = format(addDays(start, i), ISO_FORMAT);
  308. dates.push(date);
  309. const classesArr = [];
  310. classesArr.push(`v-date-range__in-range`);
  311. classesArr.push(this.highlightColor);
  312. i === 0 && classesArr.push(`v-date-range__range-start`);
  313. i === diff && classesArr.push(`v-date-range__range-end`);
  314. classes[date as any] = classesArr.join(' ');
  315. }
  316. this.highlightDates = dates;
  317. this.highlightClasses = classes;
  318. },
  319. selectPreset(presetIndex: number) {
  320. this.pickerStart = this.presets[presetIndex].range.start;
  321. this.pickerEnd = this.presets[presetIndex].range.end;
  322. },
  323. reset() {
  324. // Reset Picker Values
  325. this.pickerStart = '';
  326. this.pickerEnd = '';
  327. this.highlightDates = [];
  328. this.highlightClasses = {};
  329. this.emitRange();
  330. },
  331. emitRange() {
  332. this.$emit('input', {
  333. start: this.pickerStart,
  334. end: this.pickerEnd
  335. });
  336. }
  337. },
  338. watch: {
  339. // Watching to see if the menu is closed.
  340. menu(isOpen: boolean) {
  341. if (!isOpen) {
  342. this.closeMenu();
  343. } else {
  344. this.highlight();
  345. }
  346. },
  347. pickerStart: 'highlight',
  348. pickerEnd: 'highlight'
  349. }
  350. })
  351. </script>
  352. <style>
  353. .v-date-range__input-field ::placeholder {
  354. color: #666666;
  355. opacity: 1; /* Firefox */
  356. }
  357. .v-menu__content {
  358. top: 144px !important;
  359. }
  360. .v-date-range__content > .v-date-picker-table .v-btn {
  361. border-radius: 0;
  362. }
  363. .v-date-range__pickers .v-date-picker-table table {
  364. width: auto;
  365. margin: auto;
  366. border-collapse: collapse;
  367. }
  368. .v-date-range__pickers .v-date-picker-table .v-btn {
  369. position: initial;
  370. }
  371. .v-date-range__pickers .v-date-range__content {
  372. display: flex;
  373. }
  374. .v-date-range__pickers .v-date-picker-table__events {
  375. height: 100%;
  376. width: 100%;
  377. top: 0;
  378. z-index: -1;
  379. }
  380. .v-date-range__pickers .v-date-picker-table__events .v-date-range__in-range {
  381. position: absolute;
  382. z-index: 0;
  383. width: 100%;
  384. height: 100%;
  385. left: 0;
  386. top: 0;
  387. bottom: 0;
  388. right: 0;
  389. border-radius: 0;
  390. }
  391. .v-date-range__pickers .v-date-picker-table__events .v-date-range__in-range.v-date-range__range-start {
  392. border-top-left-radius: 50%;
  393. border-bottom-left-radius: 50%;
  394. }
  395. .v-date-range__pickers .v-date-picker-table__events .v-date-range__in-range.v-date-range__range-end {
  396. border-top-right-radius: 50%;
  397. border-bottom-right-radius: 50%;
  398. width: calc(100% - 5px);
  399. }
  400. </style>