generated from eea/volto-addon-template
-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
7 changed files
with
168 additions
and
30 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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import React from 'react'; | ||
import cx from 'classnames'; | ||
import { useSelector } from 'react-redux'; | ||
import { toBackendLang } from '@plone/volto/helpers'; | ||
import { formatDate } from '@plone/volto/helpers/Utils/Date'; | ||
import config from '@plone/volto/registry'; | ||
|
||
export const DateWidget = ({ value, children, className }) => { | ||
const lang = useSelector((state) => state.intl.locale); | ||
const backendLang = toBackendLang(lang); | ||
const locale = | ||
backendLang === 'en' ? config.settings.dateLocale : backendLang; | ||
const formatOptions = { | ||
date: value, | ||
format: { | ||
year: 'numeric', | ||
month: 'short', | ||
day: '2-digit', | ||
}, | ||
locale, | ||
}; | ||
|
||
return value ? ( | ||
<span className={cx(className, 'date', 'widget')}> | ||
{children | ||
? children(formatDate(formatOptions)) | ||
: formatDate(formatOptions)} | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import React from 'react'; | ||
import renderer from 'react-test-renderer'; | ||
import { DateWidget } from './DateWidget'; | ||
import { Provider } from 'react-intl-redux'; | ||
import configureStore from 'redux-mock-store'; | ||
import thunk from 'redux-thunk'; | ||
|
||
const mockStore = configureStore([thunk]); | ||
|
||
const store = mockStore({ | ||
intl: { | ||
locale: 'en-gb', | ||
messages: {}, | ||
}, | ||
}); | ||
|
||
describe('DateWidget', () => { | ||
it('renders an empty date view widget component', () => { | ||
const component = renderer.create( | ||
<Provider store={store}> | ||
<DateWidget /> | ||
</Provider>, | ||
); | ||
const json = component.toJSON(); | ||
expect(json).toMatchSnapshot(); | ||
}); | ||
|
||
it('renders a date view widget component', () => { | ||
const component = renderer.create( | ||
<Provider store={store}> | ||
<DateWidget className="metadata" value="2020-08-04T09:00:00" /> | ||
</Provider>, | ||
); | ||
const json = component.toJSON(); | ||
expect(json).toMatchSnapshot(); | ||
}); | ||
|
||
it('renders a date view widget component with custom format', () => { | ||
const component = renderer.create( | ||
<Provider store={store}> | ||
<DateWidget | ||
className="metadata" | ||
value="2020-08-04T09:00:00" | ||
format={{ | ||
year: 'numeric', | ||
month: 'short', | ||
day: '2-digit', | ||
}} | ||
/> | ||
</Provider>, | ||
); | ||
const json = component.toJSON(); | ||
expect(json).toMatchSnapshot(); | ||
}); | ||
|
||
it('renders a date view widget component with children', () => { | ||
const component = renderer.create( | ||
<Provider store={store}> | ||
<DateWidget className="metadata" value="2020-08-04T09:00:00"> | ||
{(child) => <strong>{child}</strong>} | ||
</DateWidget> | ||
</Provider>, | ||
); | ||
const json = component.toJSON(); | ||
expect(json).toMatchSnapshot(); | ||
}); | ||
}); |
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
29 changes: 29 additions & 0 deletions
29
src/components/theme/Widgets/__snapshots__/DateWidget.test.js.snap
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,29 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`DateWidget renders a date view widget component 1`] = ` | ||
<span | ||
className="metadata date widget" | ||
> | ||
04 Aug 2020 | ||
</span> | ||
`; | ||
|
||
exports[`DateWidget renders a date view widget component with children 1`] = ` | ||
<span | ||
className="metadata date widget" | ||
> | ||
<strong> | ||
04 Aug 2020 | ||
</strong> | ||
</span> | ||
`; | ||
|
||
exports[`DateWidget renders a date view widget component with custom format 1`] = ` | ||
<span | ||
className="metadata date widget" | ||
> | ||
04 Aug 2020 | ||
</span> | ||
`; | ||
|
||
exports[`DateWidget renders an empty date view widget component 1`] = `""`; |
18 changes: 9 additions & 9 deletions
18
src/components/theme/Widgets/__snapshots__/DatetimeWidget.test.js.snap
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,29 +1,29 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`DatetimeWidget renders a date view widget component 1`] = ` | ||
exports[`DatetimeWidget removes the comma in the formatted date and shows correct time 1`] = ` | ||
<span | ||
className="metadata datetime widget" | ||
> | ||
04 Aug 2020 | ||
05 Sept 2024 15:34 | ||
</span> | ||
`; | ||
|
||
exports[`DatetimeWidget renders a date view widget component with children 1`] = ` | ||
exports[`DatetimeWidget renders a datetime view widget component with a date and time 1`] = ` | ||
<span | ||
className="metadata datetime widget" | ||
> | ||
<strong> | ||
04 Aug 2020 | ||
</strong> | ||
05 Sept 2024 15:34 | ||
</span> | ||
`; | ||
|
||
exports[`DatetimeWidget renders a date view widget component with custom format 1`] = ` | ||
exports[`DatetimeWidget renders a datetime view widget component with children formatting 1`] = ` | ||
<span | ||
className="metadata datetime widget" | ||
> | ||
04 Aug 2020 | ||
<strong> | ||
05 Sept 2024 15:34 | ||
</strong> | ||
</span> | ||
`; | ||
|
||
exports[`DatetimeWidget renders an empty date view widget component 1`] = `""`; | ||
exports[`DatetimeWidget renders an empty datetime view widget component 1`] = `""`; |
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