diff --git a/src/components/DatePicker/RelativeDateRange.jsx b/src/components/DatePicker/RelativeDateRange.jsx index 8b1b1925..6cbfbbaf 100644 --- a/src/components/DatePicker/RelativeDateRange.jsx +++ b/src/components/DatePicker/RelativeDateRange.jsx @@ -3,7 +3,7 @@ import React from "react"; import { Button, Select } from "../.."; import { now, toDate } from "../../helpers/date"; -const options = { +export const rangeOptions = { // Day YESTERDAY: "P1D,yesterday", TODAY: "P1D,today", @@ -38,84 +38,119 @@ const options = { LEADING_YEAR: "P1Y,+1 year", }; +export const rangeLabels = { + // Day + [rangeOptions.YESTERDAY]: "Yesterday", + [rangeOptions.TODAY]: "Today", + [rangeOptions.NEXT_DAY]: "Next Day", + + // Week + [rangeOptions.LAST_WEEK]: "Last Week", + [rangeOptions.TRAILING_WEEK]: "Trailing Week", + [rangeOptions.THIS_WEEK]: "This Week", + [rangeOptions.NEXT_WEEK]: "Next Week", + [rangeOptions.LEADING_WEEK]: "Leading Week", + + // Month + [rangeOptions.LAST_MONTH]: "Last Month", + [rangeOptions.TRAILING_MONTH]: "Trailing Month", + [rangeOptions.THIS_MONTH]: "This Month", + [rangeOptions.NEXT_MONTH]: "Next Month", + [rangeOptions.LEADING_MONTH]: "Leading Month", + + // Quarter + [rangeOptions.LAST_QUARTER]: "Last Quarter", + [rangeOptions.TRAILING_QUARTER]: "Trailing Quarter", + [rangeOptions.THIS_QUARTER]: "This Quarter", + [rangeOptions.NEXT_QUARTER]: "Next Quarter", + [rangeOptions.LEADING_QUARTER]: "Leading Quarter", + + // Year + [rangeOptions.LAST_YEAR]: "Last Year", + [rangeOptions.TRAILING_YEAR]: "Trailing Year", + [rangeOptions.THIS_YEAR]: "This Year", + [rangeOptions.NEXT_YEAR]: "Next Year", + [rangeOptions.LEADING_YEAR]: "Leading Year", +}; + export const dateRanges = { day: { label: "Day", - options: [ - { value: options.YESTERDAY, label: "Yesterday" }, - { value: options.TODAY, label: "Today" }, - { value: options.NEXT_DAY, label: "Tomorrow" }, + rangeOptions: [ + { value: rangeOptions.YESTERDAY, label: "Yesterday" }, + { value: rangeOptions.TODAY, label: "Today" }, + { value: rangeOptions.NEXT_DAY, label: "Tomorrow" }, ], }, week: { label: "Week", - options: [ - { value: options.LAST_WEEK, label: "Last Week" }, - { value: options.TRAILING_WEEK, label: "Trailing Week" }, - { value: options.THIS_WEEK, label: "This Week" }, - { value: options.NEXT_WEEK, label: "Next Week" }, - { value: options.LEADING_WEEK, label: "Leading Week" }, + rangeOptions: [ + { value: rangeOptions.LAST_WEEK, label: "Last Week" }, + { value: rangeOptions.TRAILING_WEEK, label: "Trailing Week" }, + { value: rangeOptions.THIS_WEEK, label: "This Week" }, + { value: rangeOptions.NEXT_WEEK, label: "Next Week" }, + { value: rangeOptions.LEADING_WEEK, label: "Leading Week" }, ], }, month: { label: "Month", - options: [ - { value: options.LAST_MONTH, label: "Last Month" }, - { value: options.TRAILING_MONTH, label: "Trailing Month" }, - { value: options.THIS_MONTH, label: "This Month" }, - { value: options.NEXT_MONTH, label: "Next Month" }, - { value: options.LEADING_MONTH, label: "Leading Month" }, + rangeOptions: [ + { value: rangeOptions.LAST_MONTH, label: "Last Month" }, + { value: rangeOptions.TRAILING_MONTH, label: "Trailing Month" }, + { value: rangeOptions.THIS_MONTH, label: "This Month" }, + { value: rangeOptions.NEXT_MONTH, label: "Next Month" }, + { value: rangeOptions.LEADING_MONTH, label: "Leading Month" }, ], }, quarter: { label: "Quarter", - options: [ - { value: options.LAST_QUARTER, label: "Last Quarter" }, - { value: options.TRAILING_QUARTER, label: "Trailing Quarter" }, - { value: options.THIS_QUARTER, label: "This Quarter" }, - { value: options.NEXT_QUARTER, label: "Next Quarter" }, - { value: options.LEADING_QUARTER, label: "Leading Quarter" }, + rangeOptions: [ + { value: rangeOptions.LAST_QUARTER, label: "Last Quarter" }, + { value: rangeOptions.TRAILING_QUARTER, label: "Trailing Quarter" }, + { value: rangeOptions.THIS_QUARTER, label: "This Quarter" }, + { value: rangeOptions.NEXT_QUARTER, label: "Next Quarter" }, + { value: rangeOptions.LEADING_QUARTER, label: "Leading Quarter" }, ], }, year: { label: "Year", - options: [ - { value: options.LAST_YEAR, label: "Last Year" }, - { value: options.TRAILING_YEAR, label: "Trailing Year" }, - { value: options.THIS_YEAR, label: "This Year" }, - { value: options.NEXT_YEAR, label: "Next Year" }, - { value: options.LEADING_YEAR, label: "Leading Year" }, + rangeOptions: [ + { value: rangeOptions.LAST_YEAR, label: "Last Year" }, + { value: rangeOptions.TRAILING_YEAR, label: "Trailing Year" }, + { value: rangeOptions.THIS_YEAR, label: "This Year" }, + { value: rangeOptions.NEXT_YEAR, label: "Next Year" }, + { value: rangeOptions.LEADING_YEAR, label: "Leading Year" }, ], }, }; -const filterFutureDates = (options, isFutureDatesAllowed, futureDates) => { +const filterFutureDates = (rangeOptions, isFutureDatesAllowed, futureDates) => { if (!isFutureDatesAllowed) { - return options.filter((option) => !futureDates.has(option.value)); + return rangeOptions.filter((option) => !futureDates.has(option.value)); } - return options; + return rangeOptions; }; -// List of future date options +// List of future date rangeOptions const futureDates = new Set([ - options.NEXT_DAY, - options.NEXT_WEEK, - options.NEXT_MONTH, - options.NEXT_QUARTER, - options.NEXT_YEAR, - options.LEADING_WEEK, - options.LEADING_MONTH, - options.LEADING_QUARTER, - options.LEADING_YEAR, + rangeOptions.NEXT_DAY, + rangeOptions.NEXT_WEEK, + rangeOptions.NEXT_MONTH, + rangeOptions.NEXT_QUARTER, + rangeOptions.NEXT_YEAR, + rangeOptions.LEADING_WEEK, + rangeOptions.LEADING_MONTH, + rangeOptions.LEADING_QUARTER, + rangeOptions.LEADING_YEAR, ]); const handlers = { - [options.YESTERDAY]: (timezone) => { + [rangeOptions.YESTERDAY]: (timezone) => { const yesterday = now(null, timezone).subtract(1, "day"); return { from: toDate(yesterday.startOf("day")), @@ -123,14 +158,14 @@ const handlers = { }; }, - [options.TODAY]: (timezone) => { + [rangeOptions.TODAY]: (timezone) => { return { from: toDate(now(null, timezone).startOf("day")), to: toDate(now(null, timezone).endOf("day"), false), }; }, - [options.NEXT_DAY]: (timezone) => { + [rangeOptions.NEXT_DAY]: (timezone) => { const nextDay = now(null, timezone).add(1, "day"); return { from: toDate(nextDay.startOf("day")), @@ -138,7 +173,7 @@ const handlers = { }; }, - [options.LAST_WEEK]: (timezone) => { + [rangeOptions.LAST_WEEK]: (timezone) => { const lastWeek = now(null, timezone).subtract(7, "day"); return { from: toDate(lastWeek.startOf("week")), @@ -146,21 +181,21 @@ const handlers = { }; }, - [options.TRAILING_WEEK]: (timezone) => { + [rangeOptions.TRAILING_WEEK]: (timezone) => { return { from: toDate(now(null, timezone).subtract(7, "day").startOf("day")), to: toDate(now(null, timezone).subtract(1, "day").endOf("day"), false), }; }, - [options.THIS_WEEK]: (timezone) => { + [rangeOptions.THIS_WEEK]: (timezone) => { return { from: toDate(now(null, timezone).startOf("week")), to: toDate(now(null, timezone).endOf("week"), false), }; }, - [options.NEXT_WEEK]: (timezone) => { + [rangeOptions.NEXT_WEEK]: (timezone) => { const nextWeek = now(null, timezone).add(1, "week"); return { from: toDate(nextWeek.startOf("week")), @@ -168,7 +203,7 @@ const handlers = { }; }, - [options.LEADING_WEEK]: (timezone) => { + [rangeOptions.LEADING_WEEK]: (timezone) => { const today = now(null, timezone); return { from: toDate(today.startOf("day")), @@ -176,7 +211,7 @@ const handlers = { }; }, - [options.LAST_MONTH]: (timezone) => { + [rangeOptions.LAST_MONTH]: (timezone) => { const lastMonth = now(null, timezone).subtract(1, "month"); return { from: toDate(lastMonth.startOf("month")), @@ -184,19 +219,19 @@ const handlers = { }; }, - [options.TRAILING_MONTH]: (timezone) => { + [rangeOptions.TRAILING_MONTH]: (timezone) => { return { from: toDate(now(null, timezone).subtract(1, "month").startOf("day")), to: toDate(now(null, timezone).subtract(1, "day").endOf("day"), false), }; }, - [options.THIS_MONTH]: (timezone) => ({ + [rangeOptions.THIS_MONTH]: (timezone) => ({ from: toDate(now(null, timezone).startOf("month")), to: toDate(now(null, timezone).endOf("month"), false), }), - [options.NEXT_MONTH]: (timezone) => { + [rangeOptions.NEXT_MONTH]: (timezone) => { const nextMonth = now(null, timezone).add(1, "month"); return { from: toDate(nextMonth.startOf("month")), @@ -204,7 +239,7 @@ const handlers = { }; }, - [options.LEADING_MONTH]: (timezone) => { + [rangeOptions.LEADING_MONTH]: (timezone) => { const today = now(null, timezone); return { from: toDate(today.startOf("day")), @@ -212,28 +247,28 @@ const handlers = { }; }, - [options.LAST_QUARTER]: (timezone) => { + [rangeOptions.LAST_QUARTER]: (timezone) => { return { from: toDate(now(null, timezone).startOf("quarter").subtract(3, "month").startOf("month")), to: toDate(now(null, timezone).endOf("quarter").subtract(3, "month").endOf("month"), false), }; }, - [options.TRAILING_QUARTER]: (timezone) => { + [rangeOptions.TRAILING_QUARTER]: (timezone) => { return { from: toDate(now(null, timezone).subtract(3, "month").startOf("day")), to: toDate(now(null, timezone).subtract(1, "day").endOf("day"), false), }; }, - [options.THIS_QUARTER]: (timezone) => { + [rangeOptions.THIS_QUARTER]: (timezone) => { return { from: toDate(now(null, timezone).startOf("Q")), to: toDate(now(null, timezone).endOf("Q"), false), }; }, - [options.NEXT_QUARTER]: (timezone) => { + [rangeOptions.NEXT_QUARTER]: (timezone) => { const nextQuarter = now(null, timezone).add(1, "quarter"); return { from: toDate(nextQuarter.startOf("quarter")), @@ -241,7 +276,7 @@ const handlers = { }; }, - [options.LEADING_QUARTER]: (timezone) => { + [rangeOptions.LEADING_QUARTER]: (timezone) => { const today = now(null, timezone); return { from: toDate(today.startOf("day")), @@ -249,7 +284,7 @@ const handlers = { }; }, - [options.LAST_YEAR]: (timezone) => { + [rangeOptions.LAST_YEAR]: (timezone) => { const lastYear = now(null, timezone).subtract(1, "year"); return { from: toDate(lastYear.startOf("year")), @@ -257,19 +292,19 @@ const handlers = { }; }, - [options.TRAILING_YEAR]: (timezone) => { + [rangeOptions.TRAILING_YEAR]: (timezone) => { return { from: toDate(now(null, timezone).subtract(1, "year").startOf("day")), to: toDate(now(null, timezone).subtract(1, "day").endOf("day"), false), }; }, - [options.THIS_YEAR]: (timezone) => ({ + [rangeOptions.THIS_YEAR]: (timezone) => ({ from: toDate(now(null, timezone).startOf("year")), to: toDate(now(null, timezone).endOf("year"), false), }), - [options.NEXT_YEAR]: (timezone) => { + [rangeOptions.NEXT_YEAR]: (timezone) => { const nextYear = now(null, timezone).add(1, "year"); return { from: toDate(nextYear.startOf("year")), @@ -277,7 +312,7 @@ const handlers = { }; }, - [options.LEADING_YEAR]: (timezone) => { + [rangeOptions.LEADING_YEAR]: (timezone) => { const today = now(null, timezone); return { from: toDate(today.startOf("day")), @@ -311,7 +346,7 @@ export const RelativeDateRange = ({ return ( - {filterFutureDates(range.options, isFutureDatesAllowed, futureDates).map((option) => ( + {filterFutureDates(range.rangeOptions, isFutureDatesAllowed, futureDates).map((option) => ( diff --git a/src/index.js b/src/index.js index ba2df7c9..c8a1f104 100644 --- a/src/index.js +++ b/src/index.js @@ -54,6 +54,7 @@ export { almostZero, numberFormat, roundNumber } from "./helpers/numbers"; export { getSymbol, isZeroDecimal } from "./helpers/currency"; export { formatDate, formatTime, dateFromObjectId } from "./helpers/date"; export { formatPhoneNumber } from "./helpers/phone"; +export { rangeLabels, rangeOptions } from "./components/DatePicker/RelativeDateRange"; export { isOSX, isIosBrowser } from "./helpers/browser"; export { flash } from "./helpers/flash";