@contentful/f36-datetime 4.17.0 → 4.19.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +23 -0
- package/dist/esm/index.js +10 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/{types.d.ts → index.d.ts} +54 -49
- package/dist/index.js +26 -0
- package/dist/index.js.map +1 -0
- package/package.json +6 -6
- package/dist/main.js +0 -141
- package/dist/main.js.map +0 -1
- package/dist/module.js +0 -130
- package/dist/module.js.map +0 -1
- package/dist/types.d.ts.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,28 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
+
## 4.19.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#2209](https://github.com/contentful/forma-36/pull/2209) [`42f0c321`](https://github.com/contentful/forma-36/commit/42f0c3218965137191842f492fe5dbf4bd10784c) Thanks [@denkristoffer](https://github.com/denkristoffer)! - build: build ESM to JS file - legacy output
|
|
8
|
+
fix(notification): use type imports
|
|
9
|
+
- Updated dependencies []:
|
|
10
|
+
- @contentful/f36-core@4.19.1
|
|
11
|
+
|
|
12
|
+
## 4.19.0
|
|
13
|
+
|
|
14
|
+
### Patch Changes
|
|
15
|
+
|
|
16
|
+
- Updated dependencies []:
|
|
17
|
+
- @contentful/f36-core@4.19.0
|
|
18
|
+
|
|
19
|
+
## 4.18.0
|
|
20
|
+
|
|
21
|
+
### Patch Changes
|
|
22
|
+
|
|
23
|
+
- Updated dependencies []:
|
|
24
|
+
- @contentful/f36-core@4.18.0
|
|
25
|
+
|
|
3
26
|
## 4.17.0
|
|
4
27
|
|
|
5
28
|
### Patch Changes
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import c from 'react';
|
|
2
|
+
import m from 'dayjs';
|
|
3
|
+
import R from 'dayjs/plugin/utc.js';
|
|
4
|
+
import Y from 'dayjs/plugin/relativeTime.js';
|
|
5
|
+
import v from 'dayjs/plugin/calendar.js';
|
|
6
|
+
|
|
7
|
+
m.extend(R);function i(e,a="full"){let t;switch(a){case"day":t="DD MMM YYYY";break;case"weekday":t="ddd, DD MMM";break;case"time":t="h:mm A";break;case"fullWithSeconds":t="ddd, DD MMM YYYY [at] h:mm:ss A";break;default:t="ddd, DD MMM YYYY [at] h:mm A";}return m(e).format(t)}function r(e,a="full"){let t;switch(a){case"day":t="YYYY-MM-DD";break;case"weekday":t="MM-DD";break;case"time":t="HH:mm:ss.SSS";break;default:t="YYYY-MM-DDTHH:mm:ss.SSS[Z]";}return m(e).utc().format(t)}m.extend(R);m.extend(Y);m.extend(v);function o(e,a=new Date){return m(e).from(a)}function d(e,a=new Date){return m(e).isSame(a,"day")?o(e,a):m(e).calendar(a,{sameElse:"DD MMM YYYY"})}var h=({date:e,format:a="full",testId:t="cf-ui-date-time",...p},D)=>{let f=r(e);return c.createElement("time",{dateTime:f,"data-test-id":t,...p,ref:D},i(e,a))},k=c.forwardRef(h);m.extend(R);m.extend(Y);m.extend(v);var E=({date:e,baseDate:a,isRelativeToCurrentWeek:t=!1,testId:p="cf-ui-relative-date-time",...D},f)=>{let u=new Date,s=a??u,M=m(e),x=r(e),T;return t&&!M.isSame(s,"day")?T=d(e,s):T=o(e,s),c.createElement("time",{dateTime:x,"data-test-id":p,...D,ref:f},T)},W=c.forwardRef(E);
|
|
8
|
+
|
|
9
|
+
export { k as DateTime, W as RelativeDateTime, i as formatDateAndTime, r as formatMachineReadableDateTime, o as formatRelativeDateTime };
|
|
10
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/DateTime/DateTime.tsx","../../src/utils/formatDateTimeUtils.ts","../../src/utils/relativeDateTimeUtils.ts","../../src/RelativeDateTime/RelativeDateTime.tsx"],"names":["React","dayjs","utcPlugin","formatDateAndTime","date","format","template","formatMachineReadableDateTime","relativeTime","calendarPlugin","formatRelativeDateTime","baseDate","formatRelativeToCurrentWeekDateTime","_DateTime","testId","otherProps","ref","machineReadableDate","DateTime","_RelativeDateTime","isRelativeToCurrentWeek","now","referenceDate","dayjsDate","relativeDate","RelativeDateTime"],"mappings":"AAAA,OAAOA,MAAW,QCAlB,OAAOC,MAAW,QAClB,OAAOC,MAAe,sBACtBD,EAAM,OAAOC,CAAS,EAmBf,SAASC,EACdC,EACAC,EAAqB,OACb,CACR,IAAIC,EAEJ,OAAQD,OACD,MACHC,EAAW,cACX,UACG,UACHA,EAAW,cACX,UACG,OACHA,EAAW,SACX,UACG,kBACHA,EAAW,kCACX,cAEAA,EAAW,+BAGf,OAAOL,EAAMG,CAAI,EAAE,OAAOE,CAAQ,CACpC,CAgBO,SAASC,EACdH,EACAC,EAAqB,OACb,CACR,IAAIC,EAEJ,OAAQD,OACD,MACHC,EAAW,aACX,UACG,UACHA,EAAW,QACX,UACG,OACHA,EAAW,eACX,cAEAA,EAAW,6BAGf,OAAOL,EAAMG,CAAI,EAAE,IAAI,EAAE,OAAOE,CAAQ,CAC1C,CClFA,OAAOL,MAAW,QAClB,OAAOC,MAAe,sBACtB,OAAOM,MAAkB,+BACzB,OAAOC,MAAoB,2BAC3BR,EAAM,OAAOC,CAAS,EACtBD,EAAM,OAAOO,CAAY,EACzBP,EAAM,OAAOQ,CAAc,EAmBpB,SAASC,EACdN,EACAO,EAAqB,IAAI,KACzB,CACA,OAAOV,EAAMG,CAAI,EAAE,KAAKO,CAAQ,CAClC,CAmBO,SAASC,EACdR,EACAO,EAAqB,IAAI,KACzB,CAGA,OAFgBV,EAAMG,CAAI,EAAE,OAAOO,EAAU,KAAK,EAW3CD,EAAuBN,EAAMO,CAAQ,EANnCV,EAAMG,CAAI,EAAE,SAASO,EAAU,CACpC,SAAU,aACZ,CAAC,CAKL,CFxCA,IAAME,EAAY,CAChB,CACE,KAAAT,EACA,OAAAC,EAAS,OACT,OAAAS,EAAS,qBACNC,CACL,EACAC,IACG,CACH,IAAMC,EAAsBV,EAA8BH,CAAI,EAE9D,OACEJ,EAAA,cAAC,QACC,SAAUiB,EACV,eAAcH,EACb,GAAGC,EACJ,IAAKC,GAEJb,EAAkBC,EAAMC,CAAM,CACjC,CAEJ,EAKaa,EAAWlB,EAAM,WAAWa,CAAS,EGnDlD,OAAOb,MAAW,QAOlB,OAAOC,MAAW,QAClB,OAAOC,MAAe,sBACtB,OAAOM,MAAkB,+BACzB,OAAOC,MAAoB,2BAC3BR,EAAM,OAAOC,CAAS,EACtBD,EAAM,OAAOO,CAAY,EACzBP,EAAM,OAAOQ,CAAc,EAiC3B,IAAMU,EAAoB,CACxB,CACE,KAAAf,EACA,SAAAO,EACA,wBAAAS,EAA0B,GAC1B,OAAAN,EAAS,8BACNC,CACL,EACAC,IACG,CACH,IAAMK,EAAM,IAAI,KACVC,EAAgBX,GAAYU,EAC5BE,EAAYtB,EAAMG,CAAI,EACtBa,EAAsBV,EAA8BH,CAAI,EAE1DoB,EAEJ,OAAIJ,GAA2B,CAACG,EAAU,OAAOD,EAAe,KAAK,EAOnEE,EAAeZ,EAAoCR,EAAMkB,CAAa,EAGtEE,EAAed,EAAuBN,EAAMkB,CAAa,EAIzDtB,EAAA,cAAC,QACC,SAAUiB,EACV,eAAcH,EACb,GAAGC,EACJ,IAAKC,GAEJQ,CACH,CAEJ,EAMaC,EAAmBzB,EAAM,WAAWmB,CAAiB","sourcesContent":["import React from 'react';\nimport type {\n CommonProps,\n PropsWithHTMLElement,\n ExpandProps,\n} from '@contentful/f36-core';\n\nimport type { DateType, DateFormat } from '../types';\nimport { formatDateAndTime, formatMachineReadableDateTime } from '../utils';\n\ninterface DateTimeOwnProps extends CommonProps {\n /**\n * The date that will be displayed. It accepts a JS Date, an ISO8601 Timestamp string, or Unix Epoch Milliseconds number\n */\n date: DateType;\n /**\n * The format in which the date will be presented\n *\n * @default full\n **/\n format?: DateFormat;\n}\n\nexport type DateTimeProps = PropsWithHTMLElement<DateTimeOwnProps, 'time'>;\n\nconst _DateTime = (\n {\n date,\n format = 'full',\n testId = 'cf-ui-date-time',\n ...otherProps\n }: ExpandProps<DateTimeProps>,\n ref: React.Ref<HTMLTimeElement>,\n) => {\n const machineReadableDate = formatMachineReadableDateTime(date);\n\n return (\n <time\n dateTime={machineReadableDate}\n data-test-id={testId}\n {...otherProps}\n ref={ref}\n >\n {formatDateAndTime(date, format)}\n </time>\n );\n};\n\n/**\n * The DateTime component will format a date to a human friendly format and wrap it in a `<time>` tag\n */\nexport const DateTime = React.forwardRef(_DateTime);\n","import dayjs from 'dayjs';\nimport utcPlugin from 'dayjs/plugin/utc.js';\ndayjs.extend(utcPlugin);\n\nimport type { DateType, DateFormat } from '../types';\n\n/**\n * A funtion that will return a formatted date string. The format will dependend on the option\n * passed in the second argument.\n * By default, it will return a string with Forma 36’s \"full\" format (e.g. Tue, 17 Aug 2021 at 3:45 PM)\n *\n * @param {DateType} date - the date to be formatted\n * @param {DateFormat} format - the desired format (\"full\", \"day\", \"weekday\", or \"time\")\n * @returns a formatted date\n *\n * @example\n * formatDateAndTime('2021-08-17T15:45:00') // returns \"Tue, 17 Aug 2021 at 3:45 PM\"\n *\n * @example\n * formatDateAndTime('2021-08-17T15:45:00', 'day') // returns \"17 Aug 2021\"\n */\nexport function formatDateAndTime(\n date: DateType,\n format: DateFormat = 'full',\n): string {\n let template: string;\n\n switch (format) {\n case 'day':\n template = 'DD MMM YYYY'; // 17 Aug 2021\n break;\n case 'weekday':\n template = 'ddd, DD MMM'; // Tue, 17 Aug\n break;\n case 'time':\n template = 'h:mm A'; // 3:45 PM\n break;\n case 'fullWithSeconds':\n template = 'ddd, DD MMM YYYY [at] h:mm:ss A'; // Tue, 17 Aug 2021 at 3:45:67 PM\n break;\n default:\n template = 'ddd, DD MMM YYYY [at] h:mm A'; // Tue, 17 Aug 2021 at 3:45 PM\n }\n\n return dayjs(date).format(template);\n}\n\n/**\n * A funtion that will return a machine-readable date string that should be passed to the `datetime` attribute of a `<time>` tag\n * By default, it will return a string with \"YYYY-MM-DDTHH:mm:ss.SSS[Z]\" format\n *\n * @param {DateType} date - the date to be formatted\n * @param {DateFormat} format - the desired format (\"full\", \"day\", \"weekday\", or \"time\")\n * @returns a formatted date\n *\n * @example\n * formatMachineReadableDateTime(date) // returns 2019-08-13T10:00:00.000Z\n *\n * @example\n * formatMachineReadableDateTime(date, 'day') // returns 2019-08-13\n */\nexport function formatMachineReadableDateTime(\n date: DateType,\n format: DateFormat = 'full',\n): string {\n let template: string;\n\n switch (format) {\n case 'day':\n template = 'YYYY-MM-DD'; // 2019-08-24\n break;\n case 'weekday':\n template = 'MM-DD'; // 08-24\n break;\n case 'time':\n template = 'HH:mm:ss.SSS'; // 15:44:07.000\n break;\n default:\n template = 'YYYY-MM-DDTHH:mm:ss.SSS[Z]'; // 2019-08-24T15:44:07.000Z\n }\n\n return dayjs(date).utc().format(template);\n}\n\n/**\n * @example\n * > formatDate(date)\n * 13 Aug 2019\n */\nexport const formatDate = (date: DateType): string => {\n return formatDateAndTime(date, 'day');\n};\n\n/**\n * @example\n * > formatTime(date)\n * 8:00 AM\n */\nexport const formatTime = (date: DateType): string => {\n return formatDateAndTime(date, 'time');\n};\n\n/**\n * @example\n * > formatWeekdayDate(date)\n * Mon, 12 Aug\n */\nexport const formatWeekdayDate = (date: DateType): string => {\n return formatDateAndTime(date, 'weekday');\n};\n","import dayjs from 'dayjs';\nimport utcPlugin from 'dayjs/plugin/utc.js';\nimport relativeTime from 'dayjs/plugin/relativeTime.js';\nimport calendarPlugin from 'dayjs/plugin/calendar.js';\ndayjs.extend(utcPlugin);\ndayjs.extend(relativeTime);\ndayjs.extend(calendarPlugin);\n\nimport type { DateType } from '../types';\n\n/**\n * A function that will return a string with how far a given date is in the past or future,\n * using a baseDate as reference. If the baseDate is not passed, the function will use today as reference.\n *\n * @param {DateType} date - the date to be formatted\n * @param {DateFormat} baseDate - the date that should be used as a reference (default is \"today\")\n * @returns a relative date\n *\n * @example\n * // Considering today as 18.08.2021\n * formatRelativeDateTime('2021-08-17T15:45:00') // returns \"a day ago\"\n *\n * @example\n * formatRelativeDateTime('2021-08-17T15:45:00', '2021-08-16') // returns \"in a day\"\n */\nexport function formatRelativeDateTime(\n date: DateType,\n baseDate: DateType = new Date(),\n) {\n return dayjs(date).from(baseDate);\n}\n\n/**\n * A function that formats a date relative to Today or to the `baseDate` if passed.\n * If the date is not today, it will return a string with \"Yesterday ...\", \"Tomorrow ...\", etc\n * If the date is not in the current week, it return a string with \"DD MMM YYYY\" format\n * If the date is today, it will return a string with \"... ago\" or \"in ...\"\n *\n * @param {DateType} date - the date to be formatted\n * @param {DateFormat} baseDate - the date that should be used as a reference (default is \"today\")\n * @returns a relative date\n *\n * @example\n * // Considering today as 18.08.2021\n * formatRelativeToCurrentWeekDateTime('2021-08-17T15:45:00') // returns \"Yesterday at 3:45 PM\"\n *\n * @example\n * formatRelativeToCurrentWeekDateTime('2021-08-17T15:45:00', '2021-08-16') // returns \"Tomorrow at 3:45 PM\"\n */\nexport function formatRelativeToCurrentWeekDateTime(\n date: DateType,\n baseDate: DateType = new Date(),\n) {\n const isToday = dayjs(date).isSame(baseDate, 'day');\n\n if (!isToday) {\n // if the date is not today, we display it with \"Yesterday\", \"Tomorrow\", etc.\n // and if the date is not in the current week then it will display \"17 Aug 2021\"\n return dayjs(date).calendar(baseDate, {\n sameElse: 'DD MMM YYYY',\n });\n }\n\n // returns \"... ago\"\n return formatRelativeDateTime(date, baseDate);\n}\n","import React from 'react';\nimport type {\n CommonProps,\n PropsWithHTMLElement,\n ExpandProps,\n} from '@contentful/f36-core';\n\nimport dayjs from 'dayjs';\nimport utcPlugin from 'dayjs/plugin/utc.js';\nimport relativeTime from 'dayjs/plugin/relativeTime.js';\nimport calendarPlugin from 'dayjs/plugin/calendar.js';\ndayjs.extend(utcPlugin);\ndayjs.extend(relativeTime);\ndayjs.extend(calendarPlugin);\n\nimport type { DateType } from '../types';\nimport {\n formatMachineReadableDateTime,\n formatRelativeDateTime,\n formatRelativeToCurrentWeekDateTime,\n} from '../utils';\n\ninterface RelativeDateTimeInternalProps extends CommonProps {\n /**\n * The date that will be displayed. It accepts a JS Date, an ISO8601 Timestamp string, or Unix Epoch Milliseconds number\n */\n date: DateType;\n /**\n * If a value is passed to baseDate, then the component will compare both dates and return the time between them.\n * If no value is passed then the date will be compared to \"now\"\n *\n * @default \"Now\"\n */\n baseDate?: DateType;\n /**\n * Sets the date to be relative only if it is in the current week\n * @default false\n */\n isRelativeToCurrentWeek?: boolean;\n}\n\nexport type RelativeDateTimeProps = PropsWithHTMLElement<\n RelativeDateTimeInternalProps,\n 'time'\n>;\n\nconst _RelativeDateTime = (\n {\n date,\n baseDate,\n isRelativeToCurrentWeek = false,\n testId = 'cf-ui-relative-date-time',\n ...otherProps\n }: ExpandProps<RelativeDateTimeProps>,\n ref: React.Ref<HTMLTimeElement>,\n) => {\n const now = new Date();\n const referenceDate = baseDate ?? now;\n const dayjsDate = dayjs(date);\n const machineReadableDate = formatMachineReadableDateTime(date);\n\n let relativeDate: string;\n\n if (isRelativeToCurrentWeek && !dayjsDate.isSame(referenceDate, 'day')) {\n /**\n * if isRelativeToCurrentWeek is true and the date is not today, we display the date with Yesterday, Tomorrow, etc\n * or, if the date is not in the current week, it displays \"17 Aug 2021\"\n *\n * check formatRelativeToCurrentWeekDateTime for more details\n */\n relativeDate = formatRelativeToCurrentWeekDateTime(date, referenceDate);\n } else {\n // otherwise we display it with \"... ago\" or \"in ...\" notation\n relativeDate = formatRelativeDateTime(date, referenceDate);\n }\n\n return (\n <time\n dateTime={machineReadableDate}\n data-test-id={testId}\n {...otherProps}\n ref={ref}\n >\n {relativeDate}\n </time>\n );\n};\n\n/**\n * The RelativeDateTime will show a `date` relative to \"now\" or to the `baseDate`\n * (e.g. in a day, in one month, one month ago, etc).\n */\nexport const RelativeDateTime = React.forwardRef(_RelativeDateTime);\n"]}
|
|
@@ -1,7 +1,52 @@
|
|
|
1
|
-
import React from
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
type
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { PropsWithHTMLElement, CommonProps } from '@contentful/f36-core';
|
|
3
|
+
|
|
4
|
+
declare type DateType = Date | string | number;
|
|
5
|
+
declare type DateFormat = 'fullWithSeconds' | 'full' | 'time' | 'day' | 'weekday';
|
|
6
|
+
|
|
7
|
+
interface DateTimeOwnProps extends CommonProps {
|
|
8
|
+
/**
|
|
9
|
+
* The date that will be displayed. It accepts a JS Date, an ISO8601 Timestamp string, or Unix Epoch Milliseconds number
|
|
10
|
+
*/
|
|
11
|
+
date: DateType;
|
|
12
|
+
/**
|
|
13
|
+
* The format in which the date will be presented
|
|
14
|
+
*
|
|
15
|
+
* @default full
|
|
16
|
+
**/
|
|
17
|
+
format?: DateFormat;
|
|
18
|
+
}
|
|
19
|
+
declare type DateTimeProps = PropsWithHTMLElement<DateTimeOwnProps, 'time'>;
|
|
20
|
+
/**
|
|
21
|
+
* The DateTime component will format a date to a human friendly format and wrap it in a `<time>` tag
|
|
22
|
+
*/
|
|
23
|
+
declare const DateTime: React.ForwardRefExoticComponent<Omit<Omit<Pick<React.DetailedHTMLProps<React.TimeHTMLAttributes<HTMLElement>, HTMLElement>, "key" | keyof React.TimeHTMLAttributes<HTMLElement>>, never>, keyof DateTimeOwnProps> & DateTimeOwnProps & React.RefAttributes<HTMLTimeElement>>;
|
|
24
|
+
|
|
25
|
+
interface RelativeDateTimeInternalProps extends CommonProps {
|
|
26
|
+
/**
|
|
27
|
+
* The date that will be displayed. It accepts a JS Date, an ISO8601 Timestamp string, or Unix Epoch Milliseconds number
|
|
28
|
+
*/
|
|
29
|
+
date: DateType;
|
|
30
|
+
/**
|
|
31
|
+
* If a value is passed to baseDate, then the component will compare both dates and return the time between them.
|
|
32
|
+
* If no value is passed then the date will be compared to "now"
|
|
33
|
+
*
|
|
34
|
+
* @default "Now"
|
|
35
|
+
*/
|
|
36
|
+
baseDate?: DateType;
|
|
37
|
+
/**
|
|
38
|
+
* Sets the date to be relative only if it is in the current week
|
|
39
|
+
* @default false
|
|
40
|
+
*/
|
|
41
|
+
isRelativeToCurrentWeek?: boolean;
|
|
42
|
+
}
|
|
43
|
+
declare type RelativeDateTimeProps = PropsWithHTMLElement<RelativeDateTimeInternalProps, 'time'>;
|
|
44
|
+
/**
|
|
45
|
+
* The RelativeDateTime will show a `date` relative to "now" or to the `baseDate`
|
|
46
|
+
* (e.g. in a day, in one month, one month ago, etc).
|
|
47
|
+
*/
|
|
48
|
+
declare const RelativeDateTime: React.ForwardRefExoticComponent<Omit<Omit<Pick<React.DetailedHTMLProps<React.TimeHTMLAttributes<HTMLElement>, HTMLElement>, "key" | keyof React.TimeHTMLAttributes<HTMLElement>>, never>, keyof RelativeDateTimeInternalProps> & RelativeDateTimeInternalProps & React.RefAttributes<HTMLTimeElement>>;
|
|
49
|
+
|
|
5
50
|
/**
|
|
6
51
|
* A funtion that will return a formatted date string. The format will dependend on the option
|
|
7
52
|
* passed in the second argument.
|
|
@@ -17,7 +62,7 @@ type DateFormat = 'fullWithSeconds' | 'full' | 'time' | 'day' | 'weekday';
|
|
|
17
62
|
* @example
|
|
18
63
|
* formatDateAndTime('2021-08-17T15:45:00', 'day') // returns "17 Aug 2021"
|
|
19
64
|
*/
|
|
20
|
-
|
|
65
|
+
declare function formatDateAndTime(date: DateType, format?: DateFormat): string;
|
|
21
66
|
/**
|
|
22
67
|
* A funtion that will return a machine-readable date string that should be passed to the `datetime` attribute of a `<time>` tag
|
|
23
68
|
* By default, it will return a string with "YYYY-MM-DDTHH:mm:ss.SSS[Z]" format
|
|
@@ -32,7 +77,8 @@ export function formatDateAndTime(date: DateType, format?: DateFormat): string;
|
|
|
32
77
|
* @example
|
|
33
78
|
* formatMachineReadableDateTime(date, 'day') // returns 2019-08-13
|
|
34
79
|
*/
|
|
35
|
-
|
|
80
|
+
declare function formatMachineReadableDateTime(date: DateType, format?: DateFormat): string;
|
|
81
|
+
|
|
36
82
|
/**
|
|
37
83
|
* A function that will return a string with how far a given date is in the past or future,
|
|
38
84
|
* using a baseDate as reference. If the baseDate is not passed, the function will use today as reference.
|
|
@@ -48,47 +94,6 @@ export function formatMachineReadableDateTime(date: DateType, format?: DateForma
|
|
|
48
94
|
* @example
|
|
49
95
|
* formatRelativeDateTime('2021-08-17T15:45:00', '2021-08-16') // returns "in a day"
|
|
50
96
|
*/
|
|
51
|
-
|
|
52
|
-
interface DateTimeOwnProps extends CommonProps {
|
|
53
|
-
/**
|
|
54
|
-
* The date that will be displayed. It accepts a JS Date, an ISO8601 Timestamp string, or Unix Epoch Milliseconds number
|
|
55
|
-
*/
|
|
56
|
-
date: DateType;
|
|
57
|
-
/**
|
|
58
|
-
* The format in which the date will be presented
|
|
59
|
-
*
|
|
60
|
-
* @default full
|
|
61
|
-
**/
|
|
62
|
-
format?: DateFormat;
|
|
63
|
-
}
|
|
64
|
-
export type DateTimeProps = PropsWithHTMLElement<DateTimeOwnProps, 'time'>;
|
|
65
|
-
/**
|
|
66
|
-
* The DateTime component will format a date to a human friendly format and wrap it in a `<time>` tag
|
|
67
|
-
*/
|
|
68
|
-
export const DateTime: React.ForwardRefExoticComponent<Omit<Omit<Pick<React.DetailedHTMLProps<React.TimeHTMLAttributes<HTMLElement>, HTMLElement>, "key" | keyof React.TimeHTMLAttributes<HTMLElement>>, never>, keyof DateTimeOwnProps> & DateTimeOwnProps & React.RefAttributes<HTMLTimeElement>>;
|
|
69
|
-
interface RelativeDateTimeInternalProps extends CommonProps {
|
|
70
|
-
/**
|
|
71
|
-
* The date that will be displayed. It accepts a JS Date, an ISO8601 Timestamp string, or Unix Epoch Milliseconds number
|
|
72
|
-
*/
|
|
73
|
-
date: DateType;
|
|
74
|
-
/**
|
|
75
|
-
* If a value is passed to baseDate, then the component will compare both dates and return the time between them.
|
|
76
|
-
* If no value is passed then the date will be compared to "now"
|
|
77
|
-
*
|
|
78
|
-
* @default "Now"
|
|
79
|
-
*/
|
|
80
|
-
baseDate?: DateType;
|
|
81
|
-
/**
|
|
82
|
-
* Sets the date to be relative only if it is in the current week
|
|
83
|
-
* @default false
|
|
84
|
-
*/
|
|
85
|
-
isRelativeToCurrentWeek?: boolean;
|
|
86
|
-
}
|
|
87
|
-
export type RelativeDateTimeProps = PropsWithHTMLElement<RelativeDateTimeInternalProps, 'time'>;
|
|
88
|
-
/**
|
|
89
|
-
* The RelativeDateTime will show a `date` relative to "now" or to the `baseDate`
|
|
90
|
-
* (e.g. in a day, in one month, one month ago, etc).
|
|
91
|
-
*/
|
|
92
|
-
export const RelativeDateTime: React.ForwardRefExoticComponent<Omit<Omit<Pick<React.DetailedHTMLProps<React.TimeHTMLAttributes<HTMLElement>, HTMLElement>, "key" | keyof React.TimeHTMLAttributes<HTMLElement>>, never>, keyof RelativeDateTimeInternalProps> & RelativeDateTimeInternalProps & React.RefAttributes<HTMLTimeElement>>;
|
|
97
|
+
declare function formatRelativeDateTime(date: DateType, baseDate?: DateType): string;
|
|
93
98
|
|
|
94
|
-
|
|
99
|
+
export { DateTime, DateTimeProps, RelativeDateTime, RelativeDateTimeProps, formatDateAndTime, formatMachineReadableDateTime, formatRelativeDateTime };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var c = require('react');
|
|
6
|
+
var m = require('dayjs');
|
|
7
|
+
var R = require('dayjs/plugin/utc.js');
|
|
8
|
+
var Y = require('dayjs/plugin/relativeTime.js');
|
|
9
|
+
var v = require('dayjs/plugin/calendar.js');
|
|
10
|
+
|
|
11
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
12
|
+
|
|
13
|
+
var c__default = /*#__PURE__*/_interopDefaultLegacy(c);
|
|
14
|
+
var m__default = /*#__PURE__*/_interopDefaultLegacy(m);
|
|
15
|
+
var R__default = /*#__PURE__*/_interopDefaultLegacy(R);
|
|
16
|
+
var Y__default = /*#__PURE__*/_interopDefaultLegacy(Y);
|
|
17
|
+
var v__default = /*#__PURE__*/_interopDefaultLegacy(v);
|
|
18
|
+
|
|
19
|
+
m__default["default"].extend(R__default["default"]);function i(e,a="full"){let t;switch(a){case"day":t="DD MMM YYYY";break;case"weekday":t="ddd, DD MMM";break;case"time":t="h:mm A";break;case"fullWithSeconds":t="ddd, DD MMM YYYY [at] h:mm:ss A";break;default:t="ddd, DD MMM YYYY [at] h:mm A";}return m__default["default"](e).format(t)}function r(e,a="full"){let t;switch(a){case"day":t="YYYY-MM-DD";break;case"weekday":t="MM-DD";break;case"time":t="HH:mm:ss.SSS";break;default:t="YYYY-MM-DDTHH:mm:ss.SSS[Z]";}return m__default["default"](e).utc().format(t)}m__default["default"].extend(R__default["default"]);m__default["default"].extend(Y__default["default"]);m__default["default"].extend(v__default["default"]);function o(e,a=new Date){return m__default["default"](e).from(a)}function d(e,a=new Date){return m__default["default"](e).isSame(a,"day")?o(e,a):m__default["default"](e).calendar(a,{sameElse:"DD MMM YYYY"})}var h=({date:e,format:a="full",testId:t="cf-ui-date-time",...p},D)=>{let f=r(e);return c__default["default"].createElement("time",{dateTime:f,"data-test-id":t,...p,ref:D},i(e,a))},k=c__default["default"].forwardRef(h);m__default["default"].extend(R__default["default"]);m__default["default"].extend(Y__default["default"]);m__default["default"].extend(v__default["default"]);var E=({date:e,baseDate:a,isRelativeToCurrentWeek:t=!1,testId:p="cf-ui-relative-date-time",...D},f)=>{let u=new Date,s=a??u,M=m__default["default"](e),x=r(e),T;return t&&!M.isSame(s,"day")?T=d(e,s):T=o(e,s),c__default["default"].createElement("time",{dateTime:x,"data-test-id":p,...D,ref:f},T)},W=c__default["default"].forwardRef(E);
|
|
20
|
+
|
|
21
|
+
exports.DateTime = k;
|
|
22
|
+
exports.RelativeDateTime = W;
|
|
23
|
+
exports.formatDateAndTime = i;
|
|
24
|
+
exports.formatMachineReadableDateTime = r;
|
|
25
|
+
exports.formatRelativeDateTime = o;
|
|
26
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/DateTime/DateTime.tsx","../src/utils/formatDateTimeUtils.ts","../src/utils/relativeDateTimeUtils.ts","../src/RelativeDateTime/RelativeDateTime.tsx"],"names":["React","dayjs","utcPlugin","formatDateAndTime","date","format","template","formatMachineReadableDateTime","relativeTime","calendarPlugin","formatRelativeDateTime","baseDate","formatRelativeToCurrentWeekDateTime","_DateTime","testId","otherProps","ref","machineReadableDate","DateTime","_RelativeDateTime","isRelativeToCurrentWeek","now","referenceDate","dayjsDate","relativeDate","RelativeDateTime"],"mappings":"AAAA,OAAOA,MAAW,QCAlB,OAAOC,MAAW,QAClB,OAAOC,MAAe,sBACtBD,EAAM,OAAOC,CAAS,EAmBf,SAASC,EACdC,EACAC,EAAqB,OACb,CACR,IAAIC,EAEJ,OAAQD,OACD,MACHC,EAAW,cACX,UACG,UACHA,EAAW,cACX,UACG,OACHA,EAAW,SACX,UACG,kBACHA,EAAW,kCACX,cAEAA,EAAW,+BAGf,OAAOL,EAAMG,CAAI,EAAE,OAAOE,CAAQ,CACpC,CAgBO,SAASC,EACdH,EACAC,EAAqB,OACb,CACR,IAAIC,EAEJ,OAAQD,OACD,MACHC,EAAW,aACX,UACG,UACHA,EAAW,QACX,UACG,OACHA,EAAW,eACX,cAEAA,EAAW,6BAGf,OAAOL,EAAMG,CAAI,EAAE,IAAI,EAAE,OAAOE,CAAQ,CAC1C,CClFA,OAAOL,MAAW,QAClB,OAAOC,MAAe,sBACtB,OAAOM,MAAkB,+BACzB,OAAOC,MAAoB,2BAC3BR,EAAM,OAAOC,CAAS,EACtBD,EAAM,OAAOO,CAAY,EACzBP,EAAM,OAAOQ,CAAc,EAmBpB,SAASC,EACdN,EACAO,EAAqB,IAAI,KACzB,CACA,OAAOV,EAAMG,CAAI,EAAE,KAAKO,CAAQ,CAClC,CAmBO,SAASC,EACdR,EACAO,EAAqB,IAAI,KACzB,CAGA,OAFgBV,EAAMG,CAAI,EAAE,OAAOO,EAAU,KAAK,EAW3CD,EAAuBN,EAAMO,CAAQ,EANnCV,EAAMG,CAAI,EAAE,SAASO,EAAU,CACpC,SAAU,aACZ,CAAC,CAKL,CFxCA,IAAME,EAAY,CAChB,CACE,KAAAT,EACA,OAAAC,EAAS,OACT,OAAAS,EAAS,qBACNC,CACL,EACAC,IACG,CACH,IAAMC,EAAsBV,EAA8BH,CAAI,EAE9D,OACEJ,EAAA,cAAC,QACC,SAAUiB,EACV,eAAcH,EACb,GAAGC,EACJ,IAAKC,GAEJb,EAAkBC,EAAMC,CAAM,CACjC,CAEJ,EAKaa,EAAWlB,EAAM,WAAWa,CAAS,EGnDlD,OAAOb,MAAW,QAOlB,OAAOC,MAAW,QAClB,OAAOC,MAAe,sBACtB,OAAOM,MAAkB,+BACzB,OAAOC,MAAoB,2BAC3BR,EAAM,OAAOC,CAAS,EACtBD,EAAM,OAAOO,CAAY,EACzBP,EAAM,OAAOQ,CAAc,EAiC3B,IAAMU,EAAoB,CACxB,CACE,KAAAf,EACA,SAAAO,EACA,wBAAAS,EAA0B,GAC1B,OAAAN,EAAS,8BACNC,CACL,EACAC,IACG,CACH,IAAMK,EAAM,IAAI,KACVC,EAAgBX,GAAYU,EAC5BE,EAAYtB,EAAMG,CAAI,EACtBa,EAAsBV,EAA8BH,CAAI,EAE1DoB,EAEJ,OAAIJ,GAA2B,CAACG,EAAU,OAAOD,EAAe,KAAK,EAOnEE,EAAeZ,EAAoCR,EAAMkB,CAAa,EAGtEE,EAAed,EAAuBN,EAAMkB,CAAa,EAIzDtB,EAAA,cAAC,QACC,SAAUiB,EACV,eAAcH,EACb,GAAGC,EACJ,IAAKC,GAEJQ,CACH,CAEJ,EAMaC,EAAmBzB,EAAM,WAAWmB,CAAiB","sourcesContent":["import React from 'react';\nimport type {\n CommonProps,\n PropsWithHTMLElement,\n ExpandProps,\n} from '@contentful/f36-core';\n\nimport type { DateType, DateFormat } from '../types';\nimport { formatDateAndTime, formatMachineReadableDateTime } from '../utils';\n\ninterface DateTimeOwnProps extends CommonProps {\n /**\n * The date that will be displayed. It accepts a JS Date, an ISO8601 Timestamp string, or Unix Epoch Milliseconds number\n */\n date: DateType;\n /**\n * The format in which the date will be presented\n *\n * @default full\n **/\n format?: DateFormat;\n}\n\nexport type DateTimeProps = PropsWithHTMLElement<DateTimeOwnProps, 'time'>;\n\nconst _DateTime = (\n {\n date,\n format = 'full',\n testId = 'cf-ui-date-time',\n ...otherProps\n }: ExpandProps<DateTimeProps>,\n ref: React.Ref<HTMLTimeElement>,\n) => {\n const machineReadableDate = formatMachineReadableDateTime(date);\n\n return (\n <time\n dateTime={machineReadableDate}\n data-test-id={testId}\n {...otherProps}\n ref={ref}\n >\n {formatDateAndTime(date, format)}\n </time>\n );\n};\n\n/**\n * The DateTime component will format a date to a human friendly format and wrap it in a `<time>` tag\n */\nexport const DateTime = React.forwardRef(_DateTime);\n","import dayjs from 'dayjs';\nimport utcPlugin from 'dayjs/plugin/utc.js';\ndayjs.extend(utcPlugin);\n\nimport type { DateType, DateFormat } from '../types';\n\n/**\n * A funtion that will return a formatted date string. The format will dependend on the option\n * passed in the second argument.\n * By default, it will return a string with Forma 36’s \"full\" format (e.g. Tue, 17 Aug 2021 at 3:45 PM)\n *\n * @param {DateType} date - the date to be formatted\n * @param {DateFormat} format - the desired format (\"full\", \"day\", \"weekday\", or \"time\")\n * @returns a formatted date\n *\n * @example\n * formatDateAndTime('2021-08-17T15:45:00') // returns \"Tue, 17 Aug 2021 at 3:45 PM\"\n *\n * @example\n * formatDateAndTime('2021-08-17T15:45:00', 'day') // returns \"17 Aug 2021\"\n */\nexport function formatDateAndTime(\n date: DateType,\n format: DateFormat = 'full',\n): string {\n let template: string;\n\n switch (format) {\n case 'day':\n template = 'DD MMM YYYY'; // 17 Aug 2021\n break;\n case 'weekday':\n template = 'ddd, DD MMM'; // Tue, 17 Aug\n break;\n case 'time':\n template = 'h:mm A'; // 3:45 PM\n break;\n case 'fullWithSeconds':\n template = 'ddd, DD MMM YYYY [at] h:mm:ss A'; // Tue, 17 Aug 2021 at 3:45:67 PM\n break;\n default:\n template = 'ddd, DD MMM YYYY [at] h:mm A'; // Tue, 17 Aug 2021 at 3:45 PM\n }\n\n return dayjs(date).format(template);\n}\n\n/**\n * A funtion that will return a machine-readable date string that should be passed to the `datetime` attribute of a `<time>` tag\n * By default, it will return a string with \"YYYY-MM-DDTHH:mm:ss.SSS[Z]\" format\n *\n * @param {DateType} date - the date to be formatted\n * @param {DateFormat} format - the desired format (\"full\", \"day\", \"weekday\", or \"time\")\n * @returns a formatted date\n *\n * @example\n * formatMachineReadableDateTime(date) // returns 2019-08-13T10:00:00.000Z\n *\n * @example\n * formatMachineReadableDateTime(date, 'day') // returns 2019-08-13\n */\nexport function formatMachineReadableDateTime(\n date: DateType,\n format: DateFormat = 'full',\n): string {\n let template: string;\n\n switch (format) {\n case 'day':\n template = 'YYYY-MM-DD'; // 2019-08-24\n break;\n case 'weekday':\n template = 'MM-DD'; // 08-24\n break;\n case 'time':\n template = 'HH:mm:ss.SSS'; // 15:44:07.000\n break;\n default:\n template = 'YYYY-MM-DDTHH:mm:ss.SSS[Z]'; // 2019-08-24T15:44:07.000Z\n }\n\n return dayjs(date).utc().format(template);\n}\n\n/**\n * @example\n * > formatDate(date)\n * 13 Aug 2019\n */\nexport const formatDate = (date: DateType): string => {\n return formatDateAndTime(date, 'day');\n};\n\n/**\n * @example\n * > formatTime(date)\n * 8:00 AM\n */\nexport const formatTime = (date: DateType): string => {\n return formatDateAndTime(date, 'time');\n};\n\n/**\n * @example\n * > formatWeekdayDate(date)\n * Mon, 12 Aug\n */\nexport const formatWeekdayDate = (date: DateType): string => {\n return formatDateAndTime(date, 'weekday');\n};\n","import dayjs from 'dayjs';\nimport utcPlugin from 'dayjs/plugin/utc.js';\nimport relativeTime from 'dayjs/plugin/relativeTime.js';\nimport calendarPlugin from 'dayjs/plugin/calendar.js';\ndayjs.extend(utcPlugin);\ndayjs.extend(relativeTime);\ndayjs.extend(calendarPlugin);\n\nimport type { DateType } from '../types';\n\n/**\n * A function that will return a string with how far a given date is in the past or future,\n * using a baseDate as reference. If the baseDate is not passed, the function will use today as reference.\n *\n * @param {DateType} date - the date to be formatted\n * @param {DateFormat} baseDate - the date that should be used as a reference (default is \"today\")\n * @returns a relative date\n *\n * @example\n * // Considering today as 18.08.2021\n * formatRelativeDateTime('2021-08-17T15:45:00') // returns \"a day ago\"\n *\n * @example\n * formatRelativeDateTime('2021-08-17T15:45:00', '2021-08-16') // returns \"in a day\"\n */\nexport function formatRelativeDateTime(\n date: DateType,\n baseDate: DateType = new Date(),\n) {\n return dayjs(date).from(baseDate);\n}\n\n/**\n * A function that formats a date relative to Today or to the `baseDate` if passed.\n * If the date is not today, it will return a string with \"Yesterday ...\", \"Tomorrow ...\", etc\n * If the date is not in the current week, it return a string with \"DD MMM YYYY\" format\n * If the date is today, it will return a string with \"... ago\" or \"in ...\"\n *\n * @param {DateType} date - the date to be formatted\n * @param {DateFormat} baseDate - the date that should be used as a reference (default is \"today\")\n * @returns a relative date\n *\n * @example\n * // Considering today as 18.08.2021\n * formatRelativeToCurrentWeekDateTime('2021-08-17T15:45:00') // returns \"Yesterday at 3:45 PM\"\n *\n * @example\n * formatRelativeToCurrentWeekDateTime('2021-08-17T15:45:00', '2021-08-16') // returns \"Tomorrow at 3:45 PM\"\n */\nexport function formatRelativeToCurrentWeekDateTime(\n date: DateType,\n baseDate: DateType = new Date(),\n) {\n const isToday = dayjs(date).isSame(baseDate, 'day');\n\n if (!isToday) {\n // if the date is not today, we display it with \"Yesterday\", \"Tomorrow\", etc.\n // and if the date is not in the current week then it will display \"17 Aug 2021\"\n return dayjs(date).calendar(baseDate, {\n sameElse: 'DD MMM YYYY',\n });\n }\n\n // returns \"... ago\"\n return formatRelativeDateTime(date, baseDate);\n}\n","import React from 'react';\nimport type {\n CommonProps,\n PropsWithHTMLElement,\n ExpandProps,\n} from '@contentful/f36-core';\n\nimport dayjs from 'dayjs';\nimport utcPlugin from 'dayjs/plugin/utc.js';\nimport relativeTime from 'dayjs/plugin/relativeTime.js';\nimport calendarPlugin from 'dayjs/plugin/calendar.js';\ndayjs.extend(utcPlugin);\ndayjs.extend(relativeTime);\ndayjs.extend(calendarPlugin);\n\nimport type { DateType } from '../types';\nimport {\n formatMachineReadableDateTime,\n formatRelativeDateTime,\n formatRelativeToCurrentWeekDateTime,\n} from '../utils';\n\ninterface RelativeDateTimeInternalProps extends CommonProps {\n /**\n * The date that will be displayed. It accepts a JS Date, an ISO8601 Timestamp string, or Unix Epoch Milliseconds number\n */\n date: DateType;\n /**\n * If a value is passed to baseDate, then the component will compare both dates and return the time between them.\n * If no value is passed then the date will be compared to \"now\"\n *\n * @default \"Now\"\n */\n baseDate?: DateType;\n /**\n * Sets the date to be relative only if it is in the current week\n * @default false\n */\n isRelativeToCurrentWeek?: boolean;\n}\n\nexport type RelativeDateTimeProps = PropsWithHTMLElement<\n RelativeDateTimeInternalProps,\n 'time'\n>;\n\nconst _RelativeDateTime = (\n {\n date,\n baseDate,\n isRelativeToCurrentWeek = false,\n testId = 'cf-ui-relative-date-time',\n ...otherProps\n }: ExpandProps<RelativeDateTimeProps>,\n ref: React.Ref<HTMLTimeElement>,\n) => {\n const now = new Date();\n const referenceDate = baseDate ?? now;\n const dayjsDate = dayjs(date);\n const machineReadableDate = formatMachineReadableDateTime(date);\n\n let relativeDate: string;\n\n if (isRelativeToCurrentWeek && !dayjsDate.isSame(referenceDate, 'day')) {\n /**\n * if isRelativeToCurrentWeek is true and the date is not today, we display the date with Yesterday, Tomorrow, etc\n * or, if the date is not in the current week, it displays \"17 Aug 2021\"\n *\n * check formatRelativeToCurrentWeekDateTime for more details\n */\n relativeDate = formatRelativeToCurrentWeekDateTime(date, referenceDate);\n } else {\n // otherwise we display it with \"... ago\" or \"in ...\" notation\n relativeDate = formatRelativeDateTime(date, referenceDate);\n }\n\n return (\n <time\n dateTime={machineReadableDate}\n data-test-id={testId}\n {...otherProps}\n ref={ref}\n >\n {relativeDate}\n </time>\n );\n};\n\n/**\n * The RelativeDateTime will show a `date` relative to \"now\" or to the `baseDate`\n * (e.g. in a day, in one month, one month ago, etc).\n */\nexport const RelativeDateTime = React.forwardRef(_RelativeDateTime);\n"]}
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contentful/f36-datetime",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.19.1",
|
|
4
4
|
"description": "Forma 36: DateTime component",
|
|
5
5
|
"scripts": {
|
|
6
|
-
"build": "
|
|
6
|
+
"build": "tsup"
|
|
7
7
|
},
|
|
8
8
|
"dependencies": {
|
|
9
9
|
"@babel/runtime": "^7.6.2",
|
|
10
|
-
"@contentful/f36-core": "^4.
|
|
10
|
+
"@contentful/f36-core": "^4.19.1",
|
|
11
11
|
"@contentful/f36-tokens": "^4.0.1",
|
|
12
12
|
"dayjs": "^1.10.6",
|
|
13
13
|
"emotion": "^10.0.17"
|
|
@@ -19,9 +19,9 @@
|
|
|
19
19
|
"files": [
|
|
20
20
|
"dist"
|
|
21
21
|
],
|
|
22
|
-
"main": "dist/
|
|
23
|
-
"module": "dist/
|
|
24
|
-
"types": "dist/
|
|
22
|
+
"main": "dist/index.js",
|
|
23
|
+
"module": "dist/esm/index.js",
|
|
24
|
+
"types": "dist/index.d.ts",
|
|
25
25
|
"source": "src/index.ts",
|
|
26
26
|
"sideEffects": false,
|
|
27
27
|
"browserslist": "extends @contentful/browserslist-config",
|
package/dist/main.js
DELETED
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
var $iSxHR$react = require("react");
|
|
2
|
-
var $iSxHR$dayjs = require("dayjs");
|
|
3
|
-
var $iSxHR$dayjspluginutc = require("dayjs/plugin/utc");
|
|
4
|
-
var $iSxHR$dayjspluginrelativeTime = require("dayjs/plugin/relativeTime");
|
|
5
|
-
var $iSxHR$dayjsplugincalendar = require("dayjs/plugin/calendar");
|
|
6
|
-
|
|
7
|
-
function $parcel$export(e, n, v, s) {
|
|
8
|
-
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
9
|
-
}
|
|
10
|
-
function $parcel$interopDefault(a) {
|
|
11
|
-
return a && a.__esModule ? a.default : a;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
$parcel$export(module.exports, "DateTime", () => $8c9ef2783da7ec5f$export$82f9ebd9adeba146);
|
|
15
|
-
$parcel$export(module.exports, "RelativeDateTime", () => $de66f20f0f273947$export$6de14fd95adb1c8a);
|
|
16
|
-
$parcel$export(module.exports, "formatDateAndTime", () => $fe3d2807f48471b7$export$de4eb09f10f9c95e);
|
|
17
|
-
$parcel$export(module.exports, "formatMachineReadableDateTime", () => $fe3d2807f48471b7$export$5fb913e47d486079);
|
|
18
|
-
$parcel$export(module.exports, "formatRelativeDateTime", () => $4e7e5146bc048e19$export$68534c3ecebfa124);
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
(0, ($parcel$interopDefault($iSxHR$dayjs))).extend((0, ($parcel$interopDefault($iSxHR$dayjspluginutc))));
|
|
23
|
-
function $fe3d2807f48471b7$export$de4eb09f10f9c95e(date, format = "full") {
|
|
24
|
-
let template;
|
|
25
|
-
switch(format){
|
|
26
|
-
case "day":
|
|
27
|
-
template = "DD MMM YYYY"; // 17 Aug 2021
|
|
28
|
-
break;
|
|
29
|
-
case "weekday":
|
|
30
|
-
template = "ddd, DD MMM"; // Tue, 17 Aug
|
|
31
|
-
break;
|
|
32
|
-
case "time":
|
|
33
|
-
template = "h:mm A"; // 3:45 PM
|
|
34
|
-
break;
|
|
35
|
-
case "fullWithSeconds":
|
|
36
|
-
template = "ddd, DD MMM YYYY [at] h:mm:ss A"; // Tue, 17 Aug 2021 at 3:45:67 PM
|
|
37
|
-
break;
|
|
38
|
-
default:
|
|
39
|
-
template = "ddd, DD MMM YYYY [at] h:mm A";
|
|
40
|
-
}
|
|
41
|
-
return (0, ($parcel$interopDefault($iSxHR$dayjs)))(date).format(template);
|
|
42
|
-
}
|
|
43
|
-
function $fe3d2807f48471b7$export$5fb913e47d486079(date, format = "full") {
|
|
44
|
-
let template;
|
|
45
|
-
switch(format){
|
|
46
|
-
case "day":
|
|
47
|
-
template = "YYYY-MM-DD"; // 2019-08-24
|
|
48
|
-
break;
|
|
49
|
-
case "weekday":
|
|
50
|
-
template = "MM-DD"; // 08-24
|
|
51
|
-
break;
|
|
52
|
-
case "time":
|
|
53
|
-
template = "HH:mm:ss.SSS"; // 15:44:07.000
|
|
54
|
-
break;
|
|
55
|
-
default:
|
|
56
|
-
template = "YYYY-MM-DDTHH:mm:ss.SSS[Z]";
|
|
57
|
-
}
|
|
58
|
-
return (0, ($parcel$interopDefault($iSxHR$dayjs)))(date).utc().format(template);
|
|
59
|
-
}
|
|
60
|
-
const $fe3d2807f48471b7$export$3ae94a2503e890a1 = (date)=>{
|
|
61
|
-
return $fe3d2807f48471b7$export$de4eb09f10f9c95e(date, "day");
|
|
62
|
-
};
|
|
63
|
-
const $fe3d2807f48471b7$export$3203edd9e5edd663 = (date)=>{
|
|
64
|
-
return $fe3d2807f48471b7$export$de4eb09f10f9c95e(date, "time");
|
|
65
|
-
};
|
|
66
|
-
const $fe3d2807f48471b7$export$b89623867a65b725 = (date)=>{
|
|
67
|
-
return $fe3d2807f48471b7$export$de4eb09f10f9c95e(date, "weekday");
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
(0, ($parcel$interopDefault($iSxHR$dayjs))).extend((0, ($parcel$interopDefault($iSxHR$dayjspluginutc))));
|
|
75
|
-
(0, ($parcel$interopDefault($iSxHR$dayjs))).extend((0, ($parcel$interopDefault($iSxHR$dayjspluginrelativeTime))));
|
|
76
|
-
(0, ($parcel$interopDefault($iSxHR$dayjs))).extend((0, ($parcel$interopDefault($iSxHR$dayjsplugincalendar))));
|
|
77
|
-
function $4e7e5146bc048e19$export$68534c3ecebfa124(date, baseDate = new Date()) {
|
|
78
|
-
return (0, ($parcel$interopDefault($iSxHR$dayjs)))(date).from(baseDate);
|
|
79
|
-
}
|
|
80
|
-
function $4e7e5146bc048e19$export$aa1a2def90bfd930(date, baseDate = new Date()) {
|
|
81
|
-
const isToday = (0, ($parcel$interopDefault($iSxHR$dayjs)))(date).isSame(baseDate, "day");
|
|
82
|
-
if (!isToday) // if the date is not today, we display it with "Yesterday", "Tomorrow", etc.
|
|
83
|
-
// and if the date is not in the current week then it will display "17 Aug 2021"
|
|
84
|
-
return (0, ($parcel$interopDefault($iSxHR$dayjs)))(date).calendar(baseDate, {
|
|
85
|
-
sameElse: "DD MMM YYYY"
|
|
86
|
-
});
|
|
87
|
-
// returns "... ago"
|
|
88
|
-
return $4e7e5146bc048e19$export$68534c3ecebfa124(date, baseDate);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
const $8c9ef2783da7ec5f$var$_DateTime = ({ date: date , format: format = "full" , testId: testId = "cf-ui-date-time" , ...otherProps }, ref)=>{
|
|
94
|
-
const machineReadableDate = (0, $fe3d2807f48471b7$export$5fb913e47d486079)(date);
|
|
95
|
-
return /*#__PURE__*/ (0, ($parcel$interopDefault($iSxHR$react))).createElement("time", {
|
|
96
|
-
dateTime: machineReadableDate,
|
|
97
|
-
"data-test-id": testId,
|
|
98
|
-
...otherProps,
|
|
99
|
-
ref: ref
|
|
100
|
-
}, (0, $fe3d2807f48471b7$export$de4eb09f10f9c95e)(date, format));
|
|
101
|
-
};
|
|
102
|
-
const $8c9ef2783da7ec5f$export$82f9ebd9adeba146 = /*#__PURE__*/ (0, ($parcel$interopDefault($iSxHR$react))).forwardRef($8c9ef2783da7ec5f$var$_DateTime);
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
(0, ($parcel$interopDefault($iSxHR$dayjs))).extend((0, ($parcel$interopDefault($iSxHR$dayjspluginutc))));
|
|
112
|
-
(0, ($parcel$interopDefault($iSxHR$dayjs))).extend((0, ($parcel$interopDefault($iSxHR$dayjspluginrelativeTime))));
|
|
113
|
-
(0, ($parcel$interopDefault($iSxHR$dayjs))).extend((0, ($parcel$interopDefault($iSxHR$dayjsplugincalendar))));
|
|
114
|
-
const $de66f20f0f273947$var$_RelativeDateTime = ({ date: date , baseDate: baseDate , isRelativeToCurrentWeek: isRelativeToCurrentWeek = false , testId: testId = "cf-ui-relative-date-time" , ...otherProps }, ref)=>{
|
|
115
|
-
const now = new Date();
|
|
116
|
-
const referenceDate = baseDate !== null && baseDate !== void 0 ? baseDate : now;
|
|
117
|
-
const dayjsDate = (0, ($parcel$interopDefault($iSxHR$dayjs)))(date);
|
|
118
|
-
const machineReadableDate = (0, $fe3d2807f48471b7$export$5fb913e47d486079)(date);
|
|
119
|
-
let relativeDate;
|
|
120
|
-
if (isRelativeToCurrentWeek && !dayjsDate.isSame(referenceDate, "day")) /**
|
|
121
|
-
* if isRelativeToCurrentWeek is true and the date is not today, we display the date with Yesterday, Tomorrow, etc
|
|
122
|
-
* or, if the date is not in the current week, it displays "17 Aug 2021"
|
|
123
|
-
*
|
|
124
|
-
* check formatRelativeToCurrentWeekDateTime for more details
|
|
125
|
-
*/ relativeDate = (0, $4e7e5146bc048e19$export$aa1a2def90bfd930)(date, referenceDate);
|
|
126
|
-
else // otherwise we display it with "... ago" or "in ..." notation
|
|
127
|
-
relativeDate = (0, $4e7e5146bc048e19$export$68534c3ecebfa124)(date, referenceDate);
|
|
128
|
-
return /*#__PURE__*/ (0, ($parcel$interopDefault($iSxHR$react))).createElement("time", {
|
|
129
|
-
dateTime: machineReadableDate,
|
|
130
|
-
"data-test-id": testId,
|
|
131
|
-
...otherProps,
|
|
132
|
-
ref: ref
|
|
133
|
-
}, relativeDate);
|
|
134
|
-
};
|
|
135
|
-
const $de66f20f0f273947$export$6de14fd95adb1c8a = /*#__PURE__*/ (0, ($parcel$interopDefault($iSxHR$react))).forwardRef($de66f20f0f273947$var$_RelativeDateTime);
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
//# sourceMappingURL=main.js.map
|
package/dist/main.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"mappings":";;;;;;;;;;;;;;;;;;ACAA;AEAA;;AAEA4B,CAAAA,GAAAA,sCAAK,CAAA,CAACE,MAAN,CAAaD,CAAAA,GAAAA,+CAAb,CAAA,CAAAD,CAAAA;AAmBO,SAASxB,yCAAT,CACLU,IADK,EAELC,MAAkB,GAAG,MAFhB,EAGG;IACR,IAAIgB,QAAQ,AAAZ,AAAA;IAEA,OAAQhB,MAAR;QACE,KAAK,KAAL;YACEgB,QAAQ,GAAG,aAAX,CADF,CAC4B,cAA1BA;YACA,MAAA;QACF,KAAK,SAAL;YACEA,QAAQ,GAAG,aAAX,CADF,CAC4B,cAA1BA;YACA,MAAA;QACF,KAAK,MAAL;YACEA,QAAQ,GAAG,QAAX,CADF,CACuB,UAArBA;YACA,MAAA;QACF,KAAK,iBAAL;YACEA,QAAQ,GAAG,iCAAX,CADF,CACgD,iCAA9CA;YACA,MAAA;QACF;YACEA,QAAQ,GAAG,8BAAX,CAAAA;KAdJ;IAiBA,OAAOH,CAAAA,GAAAA,sCAAK,CAAA,CAACd,IAAD,CAAL,CAAYC,MAAZ,CAAmBgB,QAAnB,CAAP,CAAA;CACD;AAgBM,SAAS1B,yCAAT,CACLS,IADK,EAELC,MAAkB,GAAG,MAFhB,EAGG;IACR,IAAIgB,QAAQ,AAAZ,AAAA;IAEA,OAAQhB,MAAR;QACE,KAAK,KAAL;YACEgB,QAAQ,GAAG,YAAX,CADF,CAC2B,aAAzBA;YACA,MAAA;QACF,KAAK,SAAL;YACEA,QAAQ,GAAG,OAAX,CADF,CACsB,QAApBA;YACA,MAAA;QACF,KAAK,MAAL;YACEA,QAAQ,GAAG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,CAAA,2BAGEQ,uBAAuB,GAAG,KAH5B,WAIE1B,MAAM,GAAG,0BAJX,GAKE,GAAGC,UAAH,EANsB,EAQxBC,GARwB,GASrB;IACH,MAAM0B,GAAG,GAAG,IAAIT,IAAJ,EAAZ,AAAA;IACA,MAAMU,aAAa,GAAGX,QAAQ,aAARA,QAAQ,cAARA,QAAQ,GAAIU,GAAlC,AAAA;IACA,MAAME,SAAS,GAAGnB,CAAAA,GAAAA,sCAAK,CAAA,CAACd,IAAD,CAAvB,AAAA;IACA,MAAMQ,mBAAmB,GAAGjB,CAAAA,GAAAA,yCAA6B,CAAA,CAACS,IAAD,CAAzD,AAAA;IAEA,IAAIkC,YAAY,AAAhB,AAAA;IAEA,IAAIL,uBAAuB,IAAI,CAACI,SAAS,CAACR,MAAV,CAAiBO,aAAjB,EAAgC,KAAhC,CAAhC,EACE;;;;;OAKJ,CACIE,YAAY,GAAGrB,CAAAA,GAAAA,yCAAmC,CAAA,CAACb,IAAD,EAAOgC,aAAP,CAAlD,CAAAE;SAEA,8DAAA;IACAA,YAAY,GAAG1C,CAAAA,GAAAA,yCAAsB,CAAA,CAACQ,IAAD,EAAOgC,aAAP,CAArC,CAAAE;IAGF,qBACE,0DAAC,MAAD;QACE,QAAA,EAAU1B,mBAAD;QACT,cAAA,EAAcL,MAAD;QACb,GAAIC,UAAJ;QACA,GAAA,EAAKC,GAAD;OAEH6B,YAAD,CAPJ,CAQF;CAtCA,AAwCC;AAMM,MAAM9C,yCAAgB,iBAAGK,CAAAA,GAAAA,sCAAK,CAAA,CAACgB,UAAN,CAAiBqB,uCAAjB,CAAzB,AAAP;","sources":["packages/components/datetime/src/index.ts","packages/components/datetime/src/DateTime/DateTime.tsx","packages/components/datetime/src/utils/index.ts","packages/components/datetime/src/utils/formatDateTimeUtils.ts","packages/components/datetime/src/utils/relativeDateTimeUtils.ts","packages/components/datetime/src/RelativeDateTime/RelativeDateTime.tsx"],"sourcesContent":["export { DateTime } from './DateTime/DateTime';\nexport type { DateTimeProps } from './DateTime/DateTime';\nexport { RelativeDateTime } from './RelativeDateTime/RelativeDateTime';\nexport type { RelativeDateTimeProps } from './RelativeDateTime/RelativeDateTime';\nexport {\n formatDateAndTime,\n formatMachineReadableDateTime,\n formatRelativeDateTime,\n} from './utils';\n","import React from 'react';\nimport type {\n CommonProps,\n PropsWithHTMLElement,\n ExpandProps,\n} from '@contentful/f36-core';\n\nimport type { DateType, DateFormat } from '../types';\nimport { formatDateAndTime, formatMachineReadableDateTime } from '../utils';\n\ninterface DateTimeOwnProps extends CommonProps {\n /**\n * The date that will be displayed. It accepts a JS Date, an ISO8601 Timestamp string, or Unix Epoch Milliseconds number\n */\n date: DateType;\n /**\n * The format in which the date will be presented\n *\n * @default full\n **/\n format?: DateFormat;\n}\n\nexport type DateTimeProps = PropsWithHTMLElement<DateTimeOwnProps, 'time'>;\n\nconst _DateTime = (\n {\n date,\n format = 'full',\n testId = 'cf-ui-date-time',\n ...otherProps\n }: ExpandProps<DateTimeProps>,\n ref: React.Ref<HTMLTimeElement>,\n) => {\n const machineReadableDate = formatMachineReadableDateTime(date);\n\n return (\n <time\n dateTime={machineReadableDate}\n data-test-id={testId}\n {...otherProps}\n ref={ref}\n >\n {formatDateAndTime(date, format)}\n </time>\n );\n};\n\n/**\n * The DateTime component will format a date to a human friendly format and wrap it in a `<time>` tag\n */\nexport const DateTime = React.forwardRef(_DateTime);\n","export {\n formatDateAndTime,\n formatMachineReadableDateTime,\n formatDate,\n formatTime,\n formatWeekdayDate,\n} from './formatDateTimeUtils';\n\nexport {\n formatRelativeDateTime,\n formatRelativeToCurrentWeekDateTime,\n} from './relativeDateTimeUtils';\n","import dayjs from 'dayjs';\nimport utcPlugin from 'dayjs/plugin/utc';\ndayjs.extend(utcPlugin);\n\nimport type { DateType, DateFormat } from '../types';\n\n/**\n * A funtion that will return a formatted date string. The format will dependend on the option\n * passed in the second argument.\n * By default, it will return a string with Forma 36’s \"full\" format (e.g. Tue, 17 Aug 2021 at 3:45 PM)\n *\n * @param {DateType} date - the date to be formatted\n * @param {DateFormat} format - the desired format (\"full\", \"day\", \"weekday\", or \"time\")\n * @returns a formatted date\n *\n * @example\n * formatDateAndTime('2021-08-17T15:45:00') // returns \"Tue, 17 Aug 2021 at 3:45 PM\"\n *\n * @example\n * formatDateAndTime('2021-08-17T15:45:00', 'day') // returns \"17 Aug 2021\"\n */\nexport function formatDateAndTime(\n date: DateType,\n format: DateFormat = 'full',\n): string {\n let template: string;\n\n switch (format) {\n case 'day':\n template = 'DD MMM YYYY'; // 17 Aug 2021\n break;\n case 'weekday':\n template = 'ddd, DD MMM'; // Tue, 17 Aug\n break;\n case 'time':\n template = 'h:mm A'; // 3:45 PM\n break;\n case 'fullWithSeconds':\n template = 'ddd, DD MMM YYYY [at] h:mm:ss A'; // Tue, 17 Aug 2021 at 3:45:67 PM\n break;\n default:\n template = 'ddd, DD MMM YYYY [at] h:mm A'; // Tue, 17 Aug 2021 at 3:45 PM\n }\n\n return dayjs(date).format(template);\n}\n\n/**\n * A funtion that will return a machine-readable date string that should be passed to the `datetime` attribute of a `<time>` tag\n * By default, it will return a string with \"YYYY-MM-DDTHH:mm:ss.SSS[Z]\" format\n *\n * @param {DateType} date - the date to be formatted\n * @param {DateFormat} format - the desired format (\"full\", \"day\", \"weekday\", or \"time\")\n * @returns a formatted date\n *\n * @example\n * formatMachineReadableDateTime(date) // returns 2019-08-13T10:00:00.000Z\n *\n * @example\n * formatMachineReadableDateTime(date, 'day') // returns 2019-08-13\n */\nexport function formatMachineReadableDateTime(\n date: DateType,\n format: DateFormat = 'full',\n): string {\n let template: string;\n\n switch (format) {\n case 'day':\n template = 'YYYY-MM-DD'; // 2019-08-24\n break;\n case 'weekday':\n template = 'MM-DD'; // 08-24\n break;\n case 'time':\n template = 'HH:mm:ss.SSS'; // 15:44:07.000\n break;\n default:\n template = 'YYYY-MM-DDTHH:mm:ss.SSS[Z]'; // 2019-08-24T15:44:07.000Z\n }\n\n return dayjs(date).utc().format(template);\n}\n\n/**\n * @example\n * > formatDate(date)\n * 13 Aug 2019\n */\nexport const formatDate = (date: DateType): string => {\n return formatDateAndTime(date, 'day');\n};\n\n/**\n * @example\n * > formatTime(date)\n * 8:00 AM\n */\nexport const formatTime = (date: DateType): string => {\n return formatDateAndTime(date, 'time');\n};\n\n/**\n * @example\n * > formatWeekdayDate(date)\n * Mon, 12 Aug\n */\nexport const formatWeekdayDate = (date: DateType): string => {\n return formatDateAndTime(date, 'weekday');\n};\n","import dayjs from 'dayjs';\nimport utcPlugin from 'dayjs/plugin/utc';\nimport relativeTime from 'dayjs/plugin/relativeTime';\nimport calendarPlugin from 'dayjs/plugin/calendar';\ndayjs.extend(utcPlugin);\ndayjs.extend(relativeTime);\ndayjs.extend(calendarPlugin);\n\nimport type { DateType } from '../types';\n\n/**\n * A function that will return a string with how far a given date is in the past or future,\n * using a baseDate as reference. If the baseDate is not passed, the function will use today as reference.\n *\n * @param {DateType} date - the date to be formatted\n * @param {DateFormat} baseDate - the date that should be used as a reference (default is \"today\")\n * @returns a relative date\n *\n * @example\n * // Considering today as 18.08.2021\n * formatRelativeDateTime('2021-08-17T15:45:00') // returns \"a day ago\"\n *\n * @example\n * formatRelativeDateTime('2021-08-17T15:45:00', '2021-08-16') // returns \"in a day\"\n */\nexport function formatRelativeDateTime(\n date: DateType,\n baseDate: DateType = new Date(),\n) {\n return dayjs(date).from(baseDate);\n}\n\n/**\n * A function that formats a date relative to Today or to the `baseDate` if passed.\n * If the date is not today, it will return a string with \"Yesterday ...\", \"Tomorrow ...\", etc\n * If the date is not in the current week, it return a string with \"DD MMM YYYY\" format\n * If the date is today, it will return a string with \"... ago\" or \"in ...\"\n *\n * @param {DateType} date - the date to be formatted\n * @param {DateFormat} baseDate - the date that should be used as a reference (default is \"today\")\n * @returns a relative date\n *\n * @example\n * // Considering today as 18.08.2021\n * formatRelativeToCurrentWeekDateTime('2021-08-17T15:45:00') // returns \"Yesterday at 3:45 PM\"\n *\n * @example\n * formatRelativeToCurrentWeekDateTime('2021-08-17T15:45:00', '2021-08-16') // returns \"Tomorrow at 3:45 PM\"\n */\nexport function formatRelativeToCurrentWeekDateTime(\n date: DateType,\n baseDate: DateType = new Date(),\n) {\n const isToday = dayjs(date).isSame(baseDate, 'day');\n\n if (!isToday) {\n // if the date is not today, we display it with \"Yesterday\", \"Tomorrow\", etc.\n // and if the date is not in the current week then it will display \"17 Aug 2021\"\n return dayjs(date).calendar(baseDate, {\n sameElse: 'DD MMM YYYY',\n });\n }\n\n // returns \"... ago\"\n return formatRelativeDateTime(date, baseDate);\n}\n","import React from 'react';\nimport type {\n CommonProps,\n PropsWithHTMLElement,\n ExpandProps,\n} from '@contentful/f36-core';\n\nimport dayjs from 'dayjs';\nimport utcPlugin from 'dayjs/plugin/utc';\nimport relativeTime from 'dayjs/plugin/relativeTime';\nimport calendarPlugin from 'dayjs/plugin/calendar';\ndayjs.extend(utcPlugin);\ndayjs.extend(relativeTime);\ndayjs.extend(calendarPlugin);\n\nimport type { DateType } from '../types';\nimport {\n formatMachineReadableDateTime,\n formatRelativeDateTime,\n formatRelativeToCurrentWeekDateTime,\n} from '../utils';\n\ninterface RelativeDateTimeInternalProps extends CommonProps {\n /**\n * The date that will be displayed. It accepts a JS Date, an ISO8601 Timestamp string, or Unix Epoch Milliseconds number\n */\n date: DateType;\n /**\n * If a value is passed to baseDate, then the component will compare both dates and return the time between them.\n * If no value is passed then the date will be compared to \"now\"\n *\n * @default \"Now\"\n */\n baseDate?: DateType;\n /**\n * Sets the date to be relative only if it is in the current week\n * @default false\n */\n isRelativeToCurrentWeek?: boolean;\n}\n\nexport type RelativeDateTimeProps = PropsWithHTMLElement<\n RelativeDateTimeInternalProps,\n 'time'\n>;\n\nconst _RelativeDateTime = (\n {\n date,\n baseDate,\n isRelativeToCurrentWeek = false,\n testId = 'cf-ui-relative-date-time',\n ...otherProps\n }: ExpandProps<RelativeDateTimeProps>,\n ref: React.Ref<HTMLTimeElement>,\n) => {\n const now = new Date();\n const referenceDate = baseDate ?? now;\n const dayjsDate = dayjs(date);\n const machineReadableDate = formatMachineReadableDateTime(date);\n\n let relativeDate: string;\n\n if (isRelativeToCurrentWeek && !dayjsDate.isSame(referenceDate, 'day')) {\n /**\n * if isRelativeToCurrentWeek is true and the date is not today, we display the date with Yesterday, Tomorrow, etc\n * or, if the date is not in the current week, it displays \"17 Aug 2021\"\n *\n * check formatRelativeToCurrentWeekDateTime for more details\n */\n relativeDate = formatRelativeToCurrentWeekDateTime(date, referenceDate);\n } else {\n // otherwise we display it with \"... ago\" or \"in ...\" notation\n relativeDate = formatRelativeDateTime(date, referenceDate);\n }\n\n return (\n <time\n dateTime={machineReadableDate}\n data-test-id={testId}\n {...otherProps}\n ref={ref}\n >\n {relativeDate}\n </time>\n );\n};\n\n/**\n * The RelativeDateTime will show a `date` relative to \"now\" or to the `baseDate`\n * (e.g. in a day, in one month, one month ago, etc).\n */\nexport const RelativeDateTime = React.forwardRef(_RelativeDateTime);\n"],"names":["DateTime","DateTimeProps","RelativeDateTime","RelativeDateTimeProps","formatDateAndTime","formatMachineReadableDateTime","formatRelativeDateTime","React","CommonProps","PropsWithHTMLElement","ExpandProps","DateType","DateFormat","DateTimeOwnProps","date","format","_DateTime","testId","otherProps","ref","Ref","HTMLTimeElement","machineReadableDate","forwardRef","formatDate","formatTime","formatWeekdayDate","formatRelativeToCurrentWeekDateTime","dayjs","utcPlugin","extend","template","utc","relativeTime","calendarPlugin","baseDate","Date","from","isToday","isSame","calendar","sameElse","RelativeDateTimeInternalProps","isRelativeToCurrentWeek","_RelativeDateTime","now","referenceDate","dayjsDate","relativeDate"],"version":3,"file":"main.js.map"}
|
package/dist/module.js
DELETED
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
import $fUYts$react from "react";
|
|
2
|
-
import $fUYts$dayjs from "dayjs";
|
|
3
|
-
import $fUYts$dayjspluginutc from "dayjs/plugin/utc";
|
|
4
|
-
import $fUYts$dayjspluginrelativeTime from "dayjs/plugin/relativeTime";
|
|
5
|
-
import $fUYts$dayjsplugincalendar from "dayjs/plugin/calendar";
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
(0, $fUYts$dayjs).extend((0, $fUYts$dayjspluginutc));
|
|
11
|
-
function $ee655ad1301c8997$export$de4eb09f10f9c95e(date, format = "full") {
|
|
12
|
-
let template;
|
|
13
|
-
switch(format){
|
|
14
|
-
case "day":
|
|
15
|
-
template = "DD MMM YYYY"; // 17 Aug 2021
|
|
16
|
-
break;
|
|
17
|
-
case "weekday":
|
|
18
|
-
template = "ddd, DD MMM"; // Tue, 17 Aug
|
|
19
|
-
break;
|
|
20
|
-
case "time":
|
|
21
|
-
template = "h:mm A"; // 3:45 PM
|
|
22
|
-
break;
|
|
23
|
-
case "fullWithSeconds":
|
|
24
|
-
template = "ddd, DD MMM YYYY [at] h:mm:ss A"; // Tue, 17 Aug 2021 at 3:45:67 PM
|
|
25
|
-
break;
|
|
26
|
-
default:
|
|
27
|
-
template = "ddd, DD MMM YYYY [at] h:mm A";
|
|
28
|
-
}
|
|
29
|
-
return (0, $fUYts$dayjs)(date).format(template);
|
|
30
|
-
}
|
|
31
|
-
function $ee655ad1301c8997$export$5fb913e47d486079(date, format = "full") {
|
|
32
|
-
let template;
|
|
33
|
-
switch(format){
|
|
34
|
-
case "day":
|
|
35
|
-
template = "YYYY-MM-DD"; // 2019-08-24
|
|
36
|
-
break;
|
|
37
|
-
case "weekday":
|
|
38
|
-
template = "MM-DD"; // 08-24
|
|
39
|
-
break;
|
|
40
|
-
case "time":
|
|
41
|
-
template = "HH:mm:ss.SSS"; // 15:44:07.000
|
|
42
|
-
break;
|
|
43
|
-
default:
|
|
44
|
-
template = "YYYY-MM-DDTHH:mm:ss.SSS[Z]";
|
|
45
|
-
}
|
|
46
|
-
return (0, $fUYts$dayjs)(date).utc().format(template);
|
|
47
|
-
}
|
|
48
|
-
const $ee655ad1301c8997$export$3ae94a2503e890a1 = (date)=>{
|
|
49
|
-
return $ee655ad1301c8997$export$de4eb09f10f9c95e(date, "day");
|
|
50
|
-
};
|
|
51
|
-
const $ee655ad1301c8997$export$3203edd9e5edd663 = (date)=>{
|
|
52
|
-
return $ee655ad1301c8997$export$de4eb09f10f9c95e(date, "time");
|
|
53
|
-
};
|
|
54
|
-
const $ee655ad1301c8997$export$b89623867a65b725 = (date)=>{
|
|
55
|
-
return $ee655ad1301c8997$export$de4eb09f10f9c95e(date, "weekday");
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
(0, $fUYts$dayjs).extend((0, $fUYts$dayjspluginutc));
|
|
63
|
-
(0, $fUYts$dayjs).extend((0, $fUYts$dayjspluginrelativeTime));
|
|
64
|
-
(0, $fUYts$dayjs).extend((0, $fUYts$dayjsplugincalendar));
|
|
65
|
-
function $162585a33f185f78$export$68534c3ecebfa124(date, baseDate = new Date()) {
|
|
66
|
-
return (0, $fUYts$dayjs)(date).from(baseDate);
|
|
67
|
-
}
|
|
68
|
-
function $162585a33f185f78$export$aa1a2def90bfd930(date, baseDate = new Date()) {
|
|
69
|
-
const isToday = (0, $fUYts$dayjs)(date).isSame(baseDate, "day");
|
|
70
|
-
if (!isToday) // if the date is not today, we display it with "Yesterday", "Tomorrow", etc.
|
|
71
|
-
// and if the date is not in the current week then it will display "17 Aug 2021"
|
|
72
|
-
return (0, $fUYts$dayjs)(date).calendar(baseDate, {
|
|
73
|
-
sameElse: "DD MMM YYYY"
|
|
74
|
-
});
|
|
75
|
-
// returns "... ago"
|
|
76
|
-
return $162585a33f185f78$export$68534c3ecebfa124(date, baseDate);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
const $02aa7794cdab25f9$var$_DateTime = ({ date: date , format: format = "full" , testId: testId = "cf-ui-date-time" , ...otherProps }, ref)=>{
|
|
82
|
-
const machineReadableDate = (0, $ee655ad1301c8997$export$5fb913e47d486079)(date);
|
|
83
|
-
return /*#__PURE__*/ (0, $fUYts$react).createElement("time", {
|
|
84
|
-
dateTime: machineReadableDate,
|
|
85
|
-
"data-test-id": testId,
|
|
86
|
-
...otherProps,
|
|
87
|
-
ref: ref
|
|
88
|
-
}, (0, $ee655ad1301c8997$export$de4eb09f10f9c95e)(date, format));
|
|
89
|
-
};
|
|
90
|
-
const $02aa7794cdab25f9$export$82f9ebd9adeba146 = /*#__PURE__*/ (0, $fUYts$react).forwardRef($02aa7794cdab25f9$var$_DateTime);
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
(0, $fUYts$dayjs).extend((0, $fUYts$dayjspluginutc));
|
|
100
|
-
(0, $fUYts$dayjs).extend((0, $fUYts$dayjspluginrelativeTime));
|
|
101
|
-
(0, $fUYts$dayjs).extend((0, $fUYts$dayjsplugincalendar));
|
|
102
|
-
const $f5a0093d268a4b59$var$_RelativeDateTime = ({ date: date , baseDate: baseDate , isRelativeToCurrentWeek: isRelativeToCurrentWeek = false , testId: testId = "cf-ui-relative-date-time" , ...otherProps }, ref)=>{
|
|
103
|
-
const now = new Date();
|
|
104
|
-
const referenceDate = baseDate !== null && baseDate !== void 0 ? baseDate : now;
|
|
105
|
-
const dayjsDate = (0, $fUYts$dayjs)(date);
|
|
106
|
-
const machineReadableDate = (0, $ee655ad1301c8997$export$5fb913e47d486079)(date);
|
|
107
|
-
let relativeDate;
|
|
108
|
-
if (isRelativeToCurrentWeek && !dayjsDate.isSame(referenceDate, "day")) /**
|
|
109
|
-
* if isRelativeToCurrentWeek is true and the date is not today, we display the date with Yesterday, Tomorrow, etc
|
|
110
|
-
* or, if the date is not in the current week, it displays "17 Aug 2021"
|
|
111
|
-
*
|
|
112
|
-
* check formatRelativeToCurrentWeekDateTime for more details
|
|
113
|
-
*/ relativeDate = (0, $162585a33f185f78$export$aa1a2def90bfd930)(date, referenceDate);
|
|
114
|
-
else // otherwise we display it with "... ago" or "in ..." notation
|
|
115
|
-
relativeDate = (0, $162585a33f185f78$export$68534c3ecebfa124)(date, referenceDate);
|
|
116
|
-
return /*#__PURE__*/ (0, $fUYts$react).createElement("time", {
|
|
117
|
-
dateTime: machineReadableDate,
|
|
118
|
-
"data-test-id": testId,
|
|
119
|
-
...otherProps,
|
|
120
|
-
ref: ref
|
|
121
|
-
}, relativeDate);
|
|
122
|
-
};
|
|
123
|
-
const $f5a0093d268a4b59$export$6de14fd95adb1c8a = /*#__PURE__*/ (0, $fUYts$react).forwardRef($f5a0093d268a4b59$var$_RelativeDateTime);
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
export {$02aa7794cdab25f9$export$82f9ebd9adeba146 as DateTime, $f5a0093d268a4b59$export$6de14fd95adb1c8a as RelativeDateTime, $ee655ad1301c8997$export$de4eb09f10f9c95e as formatDateAndTime, $ee655ad1301c8997$export$5fb913e47d486079 as formatMachineReadableDateTime, $162585a33f185f78$export$68534c3ecebfa124 as formatRelativeDateTime};
|
|
130
|
-
//# sourceMappingURL=module.js.map
|
package/dist/module.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"mappings":";;;;;;ACAA;AEAA;;AAEA4B,CAAAA,GAAAA,YAAK,CAAA,CAACE,MAAN,CAAaD,CAAAA,GAAAA,qBAAb,CAAA,CAAAD,CAAAA;AAmBO,SAASxB,yCAAT,CACLU,IADK,EAELC,MAAkB,GAAG,MAFhB,EAGG;IACR,IAAIgB,QAAQ,AAAZ,AAAA;IAEA,OAAQhB,MAAR;QACE,KAAK,KAAL;YACEgB,QAAQ,GAAG,aAAX,CADF,CAC4B,cAA1BA;YACA,MAAA;QACF,KAAK,SAAL;YACEA,QAAQ,GAAG,aAAX,CADF,CAC4B,cAA1BA;YACA,MAAA;QACF,KAAK,MAAL;YACEA,QAAQ,GAAG,QAAX,CADF,CACuB,UAArBA;YACA,MAAA;QACF,KAAK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CAArC,CAAAE;IAGF,qBACE,gCAAC,MAAD;QACE,QAAA,EAAU1B,mBAAD;QACT,cAAA,EAAcL,MAAD;QACb,GAAIC,UAAJ;QACA,GAAA,EAAKC,GAAD;OAEH6B,YAAD,CAPJ,CAQF;CAtCA,AAwCC;AAMM,MAAM9C,yCAAgB,iBAAGK,CAAAA,GAAAA,YAAK,CAAA,CAACgB,UAAN,CAAiBqB,uCAAjB,CAAzB,AAAP;","sources":["packages/components/datetime/src/index.ts","packages/components/datetime/src/DateTime/DateTime.tsx","packages/components/datetime/src/utils/index.ts","packages/components/datetime/src/utils/formatDateTimeUtils.ts","packages/components/datetime/src/utils/relativeDateTimeUtils.ts","packages/components/datetime/src/RelativeDateTime/RelativeDateTime.tsx"],"sourcesContent":["export { DateTime } from './DateTime/DateTime';\nexport type { DateTimeProps } from './DateTime/DateTime';\nexport { RelativeDateTime } from './RelativeDateTime/RelativeDateTime';\nexport type { RelativeDateTimeProps } from './RelativeDateTime/RelativeDateTime';\nexport {\n formatDateAndTime,\n formatMachineReadableDateTime,\n formatRelativeDateTime,\n} from './utils';\n","import React from 'react';\nimport type {\n CommonProps,\n PropsWithHTMLElement,\n ExpandProps,\n} from '@contentful/f36-core';\n\nimport type { DateType, DateFormat } from '../types';\nimport { formatDateAndTime, formatMachineReadableDateTime } from '../utils';\n\ninterface DateTimeOwnProps extends CommonProps {\n /**\n * The date that will be displayed. It accepts a JS Date, an ISO8601 Timestamp string, or Unix Epoch Milliseconds number\n */\n date: DateType;\n /**\n * The format in which the date will be presented\n *\n * @default full\n **/\n format?: DateFormat;\n}\n\nexport type DateTimeProps = PropsWithHTMLElement<DateTimeOwnProps, 'time'>;\n\nconst _DateTime = (\n {\n date,\n format = 'full',\n testId = 'cf-ui-date-time',\n ...otherProps\n }: ExpandProps<DateTimeProps>,\n ref: React.Ref<HTMLTimeElement>,\n) => {\n const machineReadableDate = formatMachineReadableDateTime(date);\n\n return (\n <time\n dateTime={machineReadableDate}\n data-test-id={testId}\n {...otherProps}\n ref={ref}\n >\n {formatDateAndTime(date, format)}\n </time>\n );\n};\n\n/**\n * The DateTime component will format a date to a human friendly format and wrap it in a `<time>` tag\n */\nexport const DateTime = React.forwardRef(_DateTime);\n","export {\n formatDateAndTime,\n formatMachineReadableDateTime,\n formatDate,\n formatTime,\n formatWeekdayDate,\n} from './formatDateTimeUtils';\n\nexport {\n formatRelativeDateTime,\n formatRelativeToCurrentWeekDateTime,\n} from './relativeDateTimeUtils';\n","import dayjs from 'dayjs';\nimport utcPlugin from 'dayjs/plugin/utc';\ndayjs.extend(utcPlugin);\n\nimport type { DateType, DateFormat } from '../types';\n\n/**\n * A funtion that will return a formatted date string. The format will dependend on the option\n * passed in the second argument.\n * By default, it will return a string with Forma 36’s \"full\" format (e.g. Tue, 17 Aug 2021 at 3:45 PM)\n *\n * @param {DateType} date - the date to be formatted\n * @param {DateFormat} format - the desired format (\"full\", \"day\", \"weekday\", or \"time\")\n * @returns a formatted date\n *\n * @example\n * formatDateAndTime('2021-08-17T15:45:00') // returns \"Tue, 17 Aug 2021 at 3:45 PM\"\n *\n * @example\n * formatDateAndTime('2021-08-17T15:45:00', 'day') // returns \"17 Aug 2021\"\n */\nexport function formatDateAndTime(\n date: DateType,\n format: DateFormat = 'full',\n): string {\n let template: string;\n\n switch (format) {\n case 'day':\n template = 'DD MMM YYYY'; // 17 Aug 2021\n break;\n case 'weekday':\n template = 'ddd, DD MMM'; // Tue, 17 Aug\n break;\n case 'time':\n template = 'h:mm A'; // 3:45 PM\n break;\n case 'fullWithSeconds':\n template = 'ddd, DD MMM YYYY [at] h:mm:ss A'; // Tue, 17 Aug 2021 at 3:45:67 PM\n break;\n default:\n template = 'ddd, DD MMM YYYY [at] h:mm A'; // Tue, 17 Aug 2021 at 3:45 PM\n }\n\n return dayjs(date).format(template);\n}\n\n/**\n * A funtion that will return a machine-readable date string that should be passed to the `datetime` attribute of a `<time>` tag\n * By default, it will return a string with \"YYYY-MM-DDTHH:mm:ss.SSS[Z]\" format\n *\n * @param {DateType} date - the date to be formatted\n * @param {DateFormat} format - the desired format (\"full\", \"day\", \"weekday\", or \"time\")\n * @returns a formatted date\n *\n * @example\n * formatMachineReadableDateTime(date) // returns 2019-08-13T10:00:00.000Z\n *\n * @example\n * formatMachineReadableDateTime(date, 'day') // returns 2019-08-13\n */\nexport function formatMachineReadableDateTime(\n date: DateType,\n format: DateFormat = 'full',\n): string {\n let template: string;\n\n switch (format) {\n case 'day':\n template = 'YYYY-MM-DD'; // 2019-08-24\n break;\n case 'weekday':\n template = 'MM-DD'; // 08-24\n break;\n case 'time':\n template = 'HH:mm:ss.SSS'; // 15:44:07.000\n break;\n default:\n template = 'YYYY-MM-DDTHH:mm:ss.SSS[Z]'; // 2019-08-24T15:44:07.000Z\n }\n\n return dayjs(date).utc().format(template);\n}\n\n/**\n * @example\n * > formatDate(date)\n * 13 Aug 2019\n */\nexport const formatDate = (date: DateType): string => {\n return formatDateAndTime(date, 'day');\n};\n\n/**\n * @example\n * > formatTime(date)\n * 8:00 AM\n */\nexport const formatTime = (date: DateType): string => {\n return formatDateAndTime(date, 'time');\n};\n\n/**\n * @example\n * > formatWeekdayDate(date)\n * Mon, 12 Aug\n */\nexport const formatWeekdayDate = (date: DateType): string => {\n return formatDateAndTime(date, 'weekday');\n};\n","import dayjs from 'dayjs';\nimport utcPlugin from 'dayjs/plugin/utc';\nimport relativeTime from 'dayjs/plugin/relativeTime';\nimport calendarPlugin from 'dayjs/plugin/calendar';\ndayjs.extend(utcPlugin);\ndayjs.extend(relativeTime);\ndayjs.extend(calendarPlugin);\n\nimport type { DateType } from '../types';\n\n/**\n * A function that will return a string with how far a given date is in the past or future,\n * using a baseDate as reference. If the baseDate is not passed, the function will use today as reference.\n *\n * @param {DateType} date - the date to be formatted\n * @param {DateFormat} baseDate - the date that should be used as a reference (default is \"today\")\n * @returns a relative date\n *\n * @example\n * // Considering today as 18.08.2021\n * formatRelativeDateTime('2021-08-17T15:45:00') // returns \"a day ago\"\n *\n * @example\n * formatRelativeDateTime('2021-08-17T15:45:00', '2021-08-16') // returns \"in a day\"\n */\nexport function formatRelativeDateTime(\n date: DateType,\n baseDate: DateType = new Date(),\n) {\n return dayjs(date).from(baseDate);\n}\n\n/**\n * A function that formats a date relative to Today or to the `baseDate` if passed.\n * If the date is not today, it will return a string with \"Yesterday ...\", \"Tomorrow ...\", etc\n * If the date is not in the current week, it return a string with \"DD MMM YYYY\" format\n * If the date is today, it will return a string with \"... ago\" or \"in ...\"\n *\n * @param {DateType} date - the date to be formatted\n * @param {DateFormat} baseDate - the date that should be used as a reference (default is \"today\")\n * @returns a relative date\n *\n * @example\n * // Considering today as 18.08.2021\n * formatRelativeToCurrentWeekDateTime('2021-08-17T15:45:00') // returns \"Yesterday at 3:45 PM\"\n *\n * @example\n * formatRelativeToCurrentWeekDateTime('2021-08-17T15:45:00', '2021-08-16') // returns \"Tomorrow at 3:45 PM\"\n */\nexport function formatRelativeToCurrentWeekDateTime(\n date: DateType,\n baseDate: DateType = new Date(),\n) {\n const isToday = dayjs(date).isSame(baseDate, 'day');\n\n if (!isToday) {\n // if the date is not today, we display it with \"Yesterday\", \"Tomorrow\", etc.\n // and if the date is not in the current week then it will display \"17 Aug 2021\"\n return dayjs(date).calendar(baseDate, {\n sameElse: 'DD MMM YYYY',\n });\n }\n\n // returns \"... ago\"\n return formatRelativeDateTime(date, baseDate);\n}\n","import React from 'react';\nimport type {\n CommonProps,\n PropsWithHTMLElement,\n ExpandProps,\n} from '@contentful/f36-core';\n\nimport dayjs from 'dayjs';\nimport utcPlugin from 'dayjs/plugin/utc';\nimport relativeTime from 'dayjs/plugin/relativeTime';\nimport calendarPlugin from 'dayjs/plugin/calendar';\ndayjs.extend(utcPlugin);\ndayjs.extend(relativeTime);\ndayjs.extend(calendarPlugin);\n\nimport type { DateType } from '../types';\nimport {\n formatMachineReadableDateTime,\n formatRelativeDateTime,\n formatRelativeToCurrentWeekDateTime,\n} from '../utils';\n\ninterface RelativeDateTimeInternalProps extends CommonProps {\n /**\n * The date that will be displayed. It accepts a JS Date, an ISO8601 Timestamp string, or Unix Epoch Milliseconds number\n */\n date: DateType;\n /**\n * If a value is passed to baseDate, then the component will compare both dates and return the time between them.\n * If no value is passed then the date will be compared to \"now\"\n *\n * @default \"Now\"\n */\n baseDate?: DateType;\n /**\n * Sets the date to be relative only if it is in the current week\n * @default false\n */\n isRelativeToCurrentWeek?: boolean;\n}\n\nexport type RelativeDateTimeProps = PropsWithHTMLElement<\n RelativeDateTimeInternalProps,\n 'time'\n>;\n\nconst _RelativeDateTime = (\n {\n date,\n baseDate,\n isRelativeToCurrentWeek = false,\n testId = 'cf-ui-relative-date-time',\n ...otherProps\n }: ExpandProps<RelativeDateTimeProps>,\n ref: React.Ref<HTMLTimeElement>,\n) => {\n const now = new Date();\n const referenceDate = baseDate ?? now;\n const dayjsDate = dayjs(date);\n const machineReadableDate = formatMachineReadableDateTime(date);\n\n let relativeDate: string;\n\n if (isRelativeToCurrentWeek && !dayjsDate.isSame(referenceDate, 'day')) {\n /**\n * if isRelativeToCurrentWeek is true and the date is not today, we display the date with Yesterday, Tomorrow, etc\n * or, if the date is not in the current week, it displays \"17 Aug 2021\"\n *\n * check formatRelativeToCurrentWeekDateTime for more details\n */\n relativeDate = formatRelativeToCurrentWeekDateTime(date, referenceDate);\n } else {\n // otherwise we display it with \"... ago\" or \"in ...\" notation\n relativeDate = formatRelativeDateTime(date, referenceDate);\n }\n\n return (\n <time\n dateTime={machineReadableDate}\n data-test-id={testId}\n {...otherProps}\n ref={ref}\n >\n {relativeDate}\n </time>\n );\n};\n\n/**\n * The RelativeDateTime will show a `date` relative to \"now\" or to the `baseDate`\n * (e.g. in a day, in one month, one month ago, etc).\n */\nexport const RelativeDateTime = React.forwardRef(_RelativeDateTime);\n"],"names":["DateTime","DateTimeProps","RelativeDateTime","RelativeDateTimeProps","formatDateAndTime","formatMachineReadableDateTime","formatRelativeDateTime","React","CommonProps","PropsWithHTMLElement","ExpandProps","DateType","DateFormat","DateTimeOwnProps","date","format","_DateTime","testId","otherProps","ref","Ref","HTMLTimeElement","machineReadableDate","forwardRef","formatDate","formatTime","formatWeekdayDate","formatRelativeToCurrentWeekDateTime","dayjs","utcPlugin","extend","template","utc","relativeTime","calendarPlugin","baseDate","Date","from","isToday","isSame","calendar","sameElse","RelativeDateTimeInternalProps","isRelativeToCurrentWeek","_RelativeDateTime","now","referenceDate","dayjsDate","relativeDate"],"version":3,"file":"module.js.map"}
|
package/dist/types.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"mappings":";;AAAA,gBAAuB,IAAI,GAAG,MAAM,GAAG,MAAM,CAAC;AAE9C,kBACI,iBAAiB,GACjB,MAAM,GACN,MAAM,GACN,KAAK,GACL,SAAS,CAAC;ACDd;;;;;;;;;;;;;;GAcG;AACH,kCACE,IAAI,EAAE,QAAQ,EACd,MAAM,GAAE,UAAmB,GAC1B,MAAM,CAqBR;AAED;;;;;;;;;;;;;GAaG;AACH,8CACE,IAAI,EAAE,QAAQ,EACd,MAAM,GAAE,UAAmB,GAC1B,MAAM,CAkBR;ACxED;;;;;;;;;;;;;;GAcG;AACH,uCACE,IAAI,EAAE,QAAQ,EACd,QAAQ,GAAE,QAAqB,UAGhC;AEpBD,0BAA2B,SAAQ,WAAW;IAC5C;;OAEG;IACH,IAAI,EAAE,QAAQ,CAAC;IACf;;;;QAII;IACJ,MAAM,CAAC,EAAE,UAAU,CAAC;CACrB;AAED,4BAA4B,qBAAqB,gBAAgB,EAAE,MAAM,CAAC,CAAC;AAyB3E;;GAEG;AACH,OAAO,MAAM,sRAAsC,CAAC;AC7BpD,uCAAwC,SAAQ,WAAW;IACzD;;OAEG;IACH,IAAI,EAAE,QAAQ,CAAC;IACf;;;;;OAKG;IACH,QAAQ,CAAC,EAAE,QAAQ,CAAC;IACpB;;;OAGG;IACH,uBAAuB,CAAC,EAAE,OAAO,CAAC;CACnC;AAED,oCAAoC,qBAClC,6BAA6B,EAC7B,MAAM,CACP,CAAC;AA4CF;;;GAGG;AACH,OAAO,MAAM,wTAAsD,CAAC","sources":["packages/components/datetime/src/src/types.ts","packages/components/datetime/src/src/utils/formatDateTimeUtils.ts","packages/components/datetime/src/src/utils/relativeDateTimeUtils.ts","packages/components/datetime/src/src/utils/index.ts","packages/components/datetime/src/src/DateTime/DateTime.tsx","packages/components/datetime/src/src/RelativeDateTime/RelativeDateTime.tsx","packages/components/datetime/src/src/index.ts","packages/components/datetime/src/index.ts"],"sourcesContent":[null,null,null,null,null,null,null,"export { DateTime } from './DateTime/DateTime';\nexport type { DateTimeProps } from './DateTime/DateTime';\nexport { RelativeDateTime } from './RelativeDateTime/RelativeDateTime';\nexport type { RelativeDateTimeProps } from './RelativeDateTime/RelativeDateTime';\nexport {\n formatDateAndTime,\n formatMachineReadableDateTime,\n formatRelativeDateTime,\n} from './utils';\n"],"names":[],"version":3,"file":"types.d.ts.map"}
|