-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleanup in Date component + formatDate expression (#2959)
Co-authored-by: Ole Martin Handeland <[email protected]>
- Loading branch information
1 parent
b489108
commit 9412d83
Showing
135 changed files
with
1,474 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,23 @@ | ||
import React from 'react'; | ||
|
||
import { formatDate, isValid, parseISO } from 'date-fns'; | ||
|
||
import classes from 'src/app-components/Date/Date.module.css'; | ||
|
||
interface DateProps { | ||
format?: string; | ||
value: string; | ||
value: React.ReactNode; | ||
iconUrl?: string; | ||
iconAltText?: string; | ||
labelId?: string; | ||
} | ||
|
||
export const DisplayDate = ({ value, format, iconUrl, iconAltText, labelId }: DateProps) => { | ||
const parsedValue = parseISO(value); | ||
let displayData = parsedValue.toDateString(); | ||
if (!isValid(parsedValue)) { | ||
window.logErrorOnce(`Ugyldig datoformat gitt til Date-komponent: "${value}"`); | ||
displayData = ''; | ||
} else if (format) { | ||
displayData = formatDate(parsedValue, format); | ||
} | ||
|
||
return ( | ||
<> | ||
{iconUrl && ( | ||
<img | ||
src={iconUrl} | ||
className={classes.icon} | ||
alt={iconAltText} | ||
/> | ||
)} | ||
{labelId && <span aria-labelledby={labelId}>{displayData}</span>} | ||
{!labelId && <span>{displayData}</span>} | ||
</> | ||
); | ||
}; | ||
export const DisplayDate = ({ value, iconUrl, iconAltText, labelId }: DateProps) => ( | ||
<> | ||
{iconUrl && ( | ||
<img | ||
src={iconUrl} | ||
className={classes.icon} | ||
alt={iconAltText} | ||
/> | ||
)} | ||
<span aria-labelledby={labelId}>{value}</span> | ||
</> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/features/expressions/shared-tests/functions/formatDate/formats-date-english.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "Should format date when only given a year", | ||
"expression": ["formatDate", "2023"], | ||
"expects": "1/1/23", | ||
"profileSettings": { | ||
"language": "en" | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/features/expressions/shared-tests/functions/formatDate/formats-date-english2.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "Should format date in english", | ||
"expression": ["formatDate", "2023-10-26T13:12:38.069Z"], | ||
"expects": "10/26/23", | ||
"profileSettings": { | ||
"language": "en" | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...ures/expressions/shared-tests/functions/formatDate/formats-date-in-format-specified2.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"name": "Should format date in format specified (2)", | ||
"expression": ["formatDate", "2023-10-26T13:12:38.069Z", "dd.MM"], | ||
"expects": "26.10" | ||
} |
5 changes: 5 additions & 0 deletions
5
src/features/expressions/shared-tests/functions/formatDate/formats-date-time.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"name": "Should format date-time from backend", | ||
"expression": ["formatDate", "2025-01-23T10:25:33.9729397+01:00"], | ||
"expects": "23.01.2025" | ||
} |
5 changes: 5 additions & 0 deletions
5
src/features/expressions/shared-tests/functions/formatDate/formats-date-with-offset.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"name": "Should format date when only a date + offset is given", | ||
"expression": ["formatDate", "2023-01-01-08:00", "yyyy-MM-dd HH:mm:ss"], | ||
"expectsFailure": "Unable to parse date \"2023-01-01-08:00\": Unknown format" | ||
} |
5 changes: 5 additions & 0 deletions
5
src/features/expressions/shared-tests/functions/formatDate/formats-date-year-only.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"name": "Should format date when only given a year", | ||
"expression": ["formatDate", "2023"], | ||
"expects": "01.01.2023" | ||
} |
11 changes: 11 additions & 0 deletions
11
...eatures/expressions/shared-tests/functions/formatDate/parse-date-2020-is-a-leap-year.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"comment": "This tests our compliance to the date format from the JSON Schema Test Suite.", | ||
"source": "https://github.com/json-schema-org/JSON-Schema-Test-Suite/blob/main/tests/draft2020-12/optional/format/date.json", | ||
"name": "Compliance: 2020 is a leap year", | ||
"expression": [ | ||
"formatDate", | ||
"2020-02-29", | ||
"G yyyy-MM-dd EEEE HH:mm:ss.SSS a" | ||
], | ||
"expects": "e.Kr. 2020-02-29 lørdag 00:00:00.000 a.m." | ||
} |
11 changes: 11 additions & 0 deletions
11
...res/expressions/shared-tests/functions/formatDate/parse-date-2021-is-not-a-leap-year.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"comment": "This tests our compliance to the date format from the JSON Schema Test Suite.", | ||
"source": "https://github.com/json-schema-org/JSON-Schema-Test-Suite/blob/main/tests/draft2020-12/optional/format/date.json", | ||
"name": "Compliance: 2021 is not a leap year", | ||
"expression": [ | ||
"formatDate", | ||
"2021-02-29", | ||
"G yyyy-MM-dd EEEE HH:mm:ss.SSS a" | ||
], | ||
"expectsFailure": "Unable to parse date \"2021-02-29\": Format was recognized, but the date/time is invalid" | ||
} |
11 changes: 11 additions & 0 deletions
11
...nctions/formatDate/parse-date-a-invalid-date-string-with-29-days-in-february-normal-.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"comment": "This tests our compliance to the date format from the JSON Schema Test Suite.", | ||
"source": "https://github.com/json-schema-org/JSON-Schema-Test-Suite/blob/main/tests/draft2020-12/optional/format/date.json", | ||
"name": "Compliance: a invalid date string with 29 days in February (normal)", | ||
"expression": [ | ||
"formatDate", | ||
"2021-02-29", | ||
"G yyyy-MM-dd EEEE HH:mm:ss.SSS a" | ||
], | ||
"expectsFailure": "Unable to parse date \"2021-02-29\": Format was recognized, but the date/time is invalid" | ||
} |
11 changes: 11 additions & 0 deletions
11
...functions/formatDate/parse-date-a-invalid-date-string-with-30-days-in-february-leap-.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"comment": "This tests our compliance to the date format from the JSON Schema Test Suite.", | ||
"source": "https://github.com/json-schema-org/JSON-Schema-Test-Suite/blob/main/tests/draft2020-12/optional/format/date.json", | ||
"name": "Compliance: a invalid date string with 30 days in February (leap)", | ||
"expression": [ | ||
"formatDate", | ||
"2020-02-30", | ||
"G yyyy-MM-dd EEEE HH:mm:ss.SSS a" | ||
], | ||
"expectsFailure": "Unable to parse date \"2020-02-30\": Format was recognized, but the date/time is invalid" | ||
} |
11 changes: 11 additions & 0 deletions
11
...ed-tests/functions/formatDate/parse-date-a-invalid-date-string-with-31-days-in-april.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"comment": "This tests our compliance to the date format from the JSON Schema Test Suite.", | ||
"source": "https://github.com/json-schema-org/JSON-Schema-Test-Suite/blob/main/tests/draft2020-12/optional/format/date.json", | ||
"name": "Compliance: a invalid date string with 31 days in April", | ||
"expression": [ | ||
"formatDate", | ||
"2020-04-31", | ||
"G yyyy-MM-dd EEEE HH:mm:ss.SSS a" | ||
], | ||
"expectsFailure": "Unable to parse date \"2020-04-31\": Format was recognized, but the date/time is invalid" | ||
} |
11 changes: 11 additions & 0 deletions
11
...red-tests/functions/formatDate/parse-date-a-invalid-date-string-with-31-days-in-june.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"comment": "This tests our compliance to the date format from the JSON Schema Test Suite.", | ||
"source": "https://github.com/json-schema-org/JSON-Schema-Test-Suite/blob/main/tests/draft2020-12/optional/format/date.json", | ||
"name": "Compliance: a invalid date string with 31 days in June", | ||
"expression": [ | ||
"formatDate", | ||
"2020-06-31", | ||
"G yyyy-MM-dd EEEE HH:mm:ss.SSS a" | ||
], | ||
"expectsFailure": "Unable to parse date \"2020-06-31\": Format was recognized, but the date/time is invalid" | ||
} |
11 changes: 11 additions & 0 deletions
11
...tests/functions/formatDate/parse-date-a-invalid-date-string-with-31-days-in-november.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"comment": "This tests our compliance to the date format from the JSON Schema Test Suite.", | ||
"source": "https://github.com/json-schema-org/JSON-Schema-Test-Suite/blob/main/tests/draft2020-12/optional/format/date.json", | ||
"name": "Compliance: a invalid date string with 31 days in November", | ||
"expression": [ | ||
"formatDate", | ||
"2020-11-31", | ||
"G yyyy-MM-dd EEEE HH:mm:ss.SSS a" | ||
], | ||
"expectsFailure": "Unable to parse date \"2020-11-31\": Format was recognized, but the date/time is invalid" | ||
} |
11 changes: 11 additions & 0 deletions
11
...ests/functions/formatDate/parse-date-a-invalid-date-string-with-31-days-in-september.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"comment": "This tests our compliance to the date format from the JSON Schema Test Suite.", | ||
"source": "https://github.com/json-schema-org/JSON-Schema-Test-Suite/blob/main/tests/draft2020-12/optional/format/date.json", | ||
"name": "Compliance: a invalid date string with 31 days in September", | ||
"expression": [ | ||
"formatDate", | ||
"2020-09-31", | ||
"G yyyy-MM-dd EEEE HH:mm:ss.SSS a" | ||
], | ||
"expectsFailure": "Unable to parse date \"2020-09-31\": Format was recognized, but the date/time is invalid" | ||
} |
11 changes: 11 additions & 0 deletions
11
...d-tests/functions/formatDate/parse-date-a-invalid-date-string-with-32-days-in-august.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"comment": "This tests our compliance to the date format from the JSON Schema Test Suite.", | ||
"source": "https://github.com/json-schema-org/JSON-Schema-Test-Suite/blob/main/tests/draft2020-12/optional/format/date.json", | ||
"name": "Compliance: a invalid date string with 32 days in August", | ||
"expression": [ | ||
"formatDate", | ||
"2020-08-32", | ||
"G yyyy-MM-dd EEEE HH:mm:ss.SSS a" | ||
], | ||
"expectsFailure": "Unable to parse date \"2020-08-32\": Format was recognized, but the date/time is invalid" | ||
} |
11 changes: 11 additions & 0 deletions
11
...tests/functions/formatDate/parse-date-a-invalid-date-string-with-32-days-in-december.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"comment": "This tests our compliance to the date format from the JSON Schema Test Suite.", | ||
"source": "https://github.com/json-schema-org/JSON-Schema-Test-Suite/blob/main/tests/draft2020-12/optional/format/date.json", | ||
"name": "Compliance: a invalid date string with 32 days in December", | ||
"expression": [ | ||
"formatDate", | ||
"2020-12-32", | ||
"G yyyy-MM-dd EEEE HH:mm:ss.SSS a" | ||
], | ||
"expectsFailure": "Unable to parse date \"2020-12-32\": Format was recognized, but the date/time is invalid" | ||
} |
11 changes: 11 additions & 0 deletions
11
...-tests/functions/formatDate/parse-date-a-invalid-date-string-with-32-days-in-january.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"comment": "This tests our compliance to the date format from the JSON Schema Test Suite.", | ||
"source": "https://github.com/json-schema-org/JSON-Schema-Test-Suite/blob/main/tests/draft2020-12/optional/format/date.json", | ||
"name": "Compliance: a invalid date string with 32 days in January", | ||
"expression": [ | ||
"formatDate", | ||
"2020-01-32", | ||
"G yyyy-MM-dd EEEE HH:mm:ss.SSS a" | ||
], | ||
"expectsFailure": "Unable to parse date \"2020-01-32\": Format was recognized, but the date/time is invalid" | ||
} |
Oops, something went wrong.