-
Notifications
You must be signed in to change notification settings - Fork 311
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f8cdfff
commit d400d6c
Showing
8 changed files
with
363 additions
and
24 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
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,53 @@ | ||
import * as React from 'react'; | ||
import { FormatDateOptions, FormattedMessage, useIntl } from 'react-intl'; | ||
import { isToday, isYesterday } from '../../../utils/datetime'; | ||
|
||
import messages from '../messages'; | ||
import './DateField.scss'; | ||
|
||
const DEFAULT_DATE_FORMAT = { | ||
weekday: 'short', | ||
month: 'short', | ||
year: 'numeric', | ||
day: 'numeric', | ||
} as const; | ||
|
||
export interface DateFieldProps { | ||
capitalize?: boolean; | ||
date: string; | ||
dateFormat?: FormatDateOptions; | ||
omitCommas?: boolean; | ||
relative?: boolean; | ||
} | ||
|
||
const DateField = ({ | ||
date, | ||
dateFormat = DEFAULT_DATE_FORMAT, | ||
omitCommas = false, | ||
relative = true, | ||
capitalize = false, | ||
}: DateFieldProps): React.ReactNode | string => { | ||
const { formatDate } = useIntl(); | ||
const d = new Date(date); | ||
const isTodaysDate = isToday(d); | ||
const isYesterdaysDate = isYesterday(d); | ||
|
||
if (relative && (isTodaysDate || isYesterdaysDate)) { | ||
let Message = <FormattedMessage {...messages.today} />; | ||
if (isYesterdaysDate) { | ||
Message = <FormattedMessage {...messages.yesterday} />; | ||
} | ||
|
||
if (capitalize) { | ||
return <span className="be-date-capitalize">{Message}</span>; | ||
} | ||
|
||
return Message; | ||
} | ||
|
||
let formattedDate = formatDate(d, dateFormat); | ||
formattedDate = omitCommas ? formattedDate.replace(/,/g, '') : formattedDate; | ||
return formattedDate; | ||
}; | ||
|
||
export default DateField; |
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,48 @@ | ||
import * as React from 'react'; | ||
import { render, screen } from '../../../../test-utils/testing-library'; | ||
|
||
import DateField, { DateFieldProps } from '../DateField'; | ||
|
||
describe('elements/common/date/DateField', () => { | ||
const renderComponent = (props: Partial<DateFieldProps> = {}) => { | ||
render(<DateField date="2023-10-10T10:00:00Z" {...props} />); | ||
}; | ||
test('renders formatted date', () => { | ||
renderComponent(); | ||
expect(screen.getByText('Tue, Oct 10, 2023')).toBeInTheDocument(); | ||
}); | ||
|
||
test("renders today message for today's date", () => { | ||
renderComponent({ date: new Date().toISOString(), relative: true }); | ||
expect(screen.getByText('today')).toBeInTheDocument(); | ||
}); | ||
|
||
test("renders yesterday message for yesterday's date", () => { | ||
const yesterday = new Date(); | ||
yesterday.setDate(yesterday.getDate() - 1); | ||
renderComponent({ date: yesterday.toISOString(), relative: true }); | ||
expect(screen.getByText('yesterday')).toBeInTheDocument(); | ||
}); | ||
|
||
test('renders formatted date without commas', () => { | ||
renderComponent({ omitCommas: true }); | ||
expect(screen.getByText('Tue Oct 10 2023')).toBeInTheDocument(); | ||
}); | ||
|
||
test('renders capitalized today message', () => { | ||
renderComponent({ date: new Date().toISOString(), relative: true, capitalize: true }); | ||
expect(screen.getByText('today')).toHaveClass('be-date-capitalize'); | ||
}); | ||
|
||
test('renders capitalized yesterday message', () => { | ||
const yesterday = new Date(); | ||
yesterday.setDate(yesterday.getDate() - 1); | ||
renderComponent({ date: yesterday.toISOString(), relative: true, capitalize: true }); | ||
expect(screen.getByText('yesterday')).toHaveClass('be-date-capitalize'); | ||
}); | ||
|
||
test('renders formatted date with custom format', () => { | ||
renderComponent({ dateFormat: { year: '2-digit', month: '2-digit', day: '2-digit' } }); | ||
expect(screen.getByText('10/10/23')).toBeInTheDocument(); | ||
}); | ||
}); |
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 @@ | ||
export {default} from './DateField'; |
1 change: 0 additions & 1 deletion
1
src/elements/common/date/index.js → src/elements/common/date/index.ts
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,2 +1 @@ | ||
// @flow | ||
export { default } from './DateField'; |
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
Oops, something went wrong.