Skip to content
This repository has been archived by the owner on Jul 25, 2023. It is now read-only.

Commit

Permalink
Refactor: Pass data via value argument and use children to wrap value
Browse files Browse the repository at this point in the history
  • Loading branch information
avoinea committed Aug 11, 2020
1 parent 4de8260 commit dc94a40
Show file tree
Hide file tree
Showing 34 changed files with 390 additions and 129 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@eeacms/volto-widgets-view",
"version": "0.1.1",
"version": "0.2.0",
"description": "Volto Widgets in View mode",
"main": "src/index.js",
"scripts": {
Expand Down
25 changes: 14 additions & 11 deletions src/components/theme/Widgets/ArrayWidget.jsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import React from 'react';
import cx from 'classnames';

export const ArrayWidget = ({ children, className }) => (
<span className={cx(className, 'array', 'widget')}>
{children
? children.map((child) => (
<span key={child.token || child.title || child}>
{child.title || child.token || child}
</span>
))
: ''}
</span>
);
export const ArrayWidget = ({ value, children, className }) =>
value ? (
<span className={cx(className, 'array', 'widget')}>
{value.map((item) => (
<span key={item.token || item.title || item}>
{children
? children(item.title || item.token || item)
: item.title || item.token || item}
</span>
))}
</span>
) : (
''
);
30 changes: 24 additions & 6 deletions src/components/theme/Widgets/ArrayWidget.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,47 @@ describe('ArrayWidget', () => {

it('renders a simple array view widget component', () => {
const component = renderer.create(
<ArrayWidget className="metadata">{['foo', 'bar']}</ArrayWidget>,
<ArrayWidget className="metadata" value={['foo', 'bar']} />,
);
const json = component.toJSON();
expect(json).toMatchSnapshot();
});

it('renders a vocabulary array view widget component', () => {
const component = renderer.create(
<ArrayWidget className="metadata">
{[{ title: 'Foo' }, { title: 'Bar' }]}
</ArrayWidget>,
<ArrayWidget
className="metadata"
value={[{ title: 'Foo' }, { title: 'Bar' }]}
/>,
);
const json = component.toJSON();
expect(json).toMatchSnapshot();
});

it('renders a full vocabulary array view widget component', () => {
const component = renderer.create(
<ArrayWidget className="metadata">
{[
<ArrayWidget
className="metadata"
value={[
{ title: 'Foo', token: 'foo' },
{ title: 'Bar', token: 'bar' },
]}
/>,
);
const json = component.toJSON();
expect(json).toMatchSnapshot();
});

it('renders a full vocabulary array view widget component with children', () => {
const component = renderer.create(
<ArrayWidget
className="metadata"
value={[
{ title: 'Foo', token: 'foo' },
{ title: 'Bar', token: 'bar' },
]}
>
{(child) => <strong>{child}</strong>}
</ArrayWidget>,
);
const json = component.toJSON();
Expand Down
14 changes: 9 additions & 5 deletions src/components/theme/Widgets/BooleanWidget.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import React from 'react';
import cx from 'classnames';
import { isBoolean } from 'lodash';

export const BooleanWidget = ({ children, className }) => (
<span className={cx(className, 'boolean', 'widget')}>
{children ? 'Yes' : 'No'}
</span>
);
export const BooleanWidget = ({ value, children, className }) =>
isBoolean(value) ? (
<span className={cx(className, 'boolean', 'widget')}>
{children ? children(value ? 'Yes' : 'No') : value ? 'Yes' : 'No'}
</span>
) : (
''
);
26 changes: 23 additions & 3 deletions src/components/theme/Widgets/BooleanWidget.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,43 @@ import renderer from 'react-test-renderer';
import { BooleanWidget } from './BooleanWidget';

describe('BooleanWidget', () => {
it('renders an empty boolean true view widget component', () => {
it('renders an empty boolean view widget component', () => {
const component = renderer.create(<BooleanWidget />);
const json = component.toJSON();
expect(json).toMatchSnapshot();
});

it('renders a boolean true view widget component', () => {
const component = renderer.create(
<BooleanWidget className="metadata">{true}</BooleanWidget>,
<BooleanWidget className="metadata" value={true} />,
);
const json = component.toJSON();
expect(json).toMatchSnapshot();
});

it('renders a boolean false view widget component', () => {
const component = renderer.create(
<BooleanWidget className="metadata">{false}</BooleanWidget>,
<BooleanWidget className="metadata" value={false} />,
);
const json = component.toJSON();
expect(json).toMatchSnapshot();
});

it('renders a boolean true view widget component with children', () => {
const component = renderer.create(
<BooleanWidget className="metadata" value={true}>
{(child) => <strong>{child}</strong>}
</BooleanWidget>,
);
const json = component.toJSON();
expect(json).toMatchSnapshot();
});

it('renders a boolean false view widget component with children', () => {
const component = renderer.create(
<BooleanWidget className="metadata" value={false}>
{(child) => <strong>{child}</strong>}
</BooleanWidget>,
);
const json = component.toJSON();
expect(json).toMatchSnapshot();
Expand Down
20 changes: 10 additions & 10 deletions src/components/theme/Widgets/DateWidget.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import React from 'react';
import cx from 'classnames';
import moment from 'moment';

// import 'moment/locale/en-gb';
// import { useIntl } from 'react-intl';

export const DateWidget = ({ children, className }) => (
// const intl = useIntl();
// moment.locale(intl.locale);
<span className={cx(className, 'date', 'widget')}>
{children ? moment(children).format('ll') : ''}
</span>
);
export const DateWidget = ({ value, children, className }) =>
value ? (
<span className={cx(className, 'date', 'widget')}>
{children
? children(moment(value).format('ll'))
: moment(value).format('ll')}
</span>
) : (
''
);
12 changes: 11 additions & 1 deletion src/components/theme/Widgets/DateWidget.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,17 @@ describe('DateWidget', () => {

it('renders a date view widget component', () => {
const component = renderer.create(
<DateWidget className="metadata">2020-08-18</DateWidget>,
<DateWidget className="metadata" value="2020-08-18" />,
);
const json = component.toJSON();
expect(json).toMatchSnapshot();
});

it('renders a date view widget component with children', () => {
const component = renderer.create(
<DateWidget className="metadata" value="2020-08-18">
{(child) => <strong>{child}</strong>}
</DateWidget>,
);
const json = component.toJSON();
expect(json).toMatchSnapshot();
Expand Down
20 changes: 10 additions & 10 deletions src/components/theme/Widgets/DatetimeWidget.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import React from 'react';
import cx from 'classnames';
import moment from 'moment';

// import 'moment/locale/en-gb';
// import { useIntl } from 'react-intl';

export const DatetimeWidget = ({ children, className }) => (
// const intl = useIntl();
// moment.locale(intl.locale);
<span className={cx(className, 'datetime', 'widget')}>
{children ? moment(children).format('lll') : ''}
</span>
);
export const DatetimeWidget = ({ value, children, className }) =>
value ? (
<span className={cx(className, 'datetime', 'widget')}>
{children
? children(moment(value).format('lll'))
: moment(value).format('lll')}
</span>
) : (
''
);
12 changes: 11 additions & 1 deletion src/components/theme/Widgets/DatetimeWidget.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,17 @@ describe('DatetimeWidget', () => {

it('renders a date view widget component', () => {
const component = renderer.create(
<DatetimeWidget className="metadata">2020-08-04T09:00:00</DatetimeWidget>,
<DatetimeWidget className="metadata" value="2020-08-04T09:00:00" />,
);
const json = component.toJSON();
expect(json).toMatchSnapshot();
});

it('renders a date view widget component with children', () => {
const component = renderer.create(
<DatetimeWidget className="metadata" value="2020-08-04T09:00:00">
{(child) => <strong>{child}</strong>}
</DatetimeWidget>,
);
const json = component.toJSON();
expect(json).toMatchSnapshot();
Expand Down
8 changes: 4 additions & 4 deletions src/components/theme/Widgets/EmailWidget.jsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import React from 'react';
import cx from 'classnames';

export const EmailWidget = ({ children, className }) =>
children ? (
export const EmailWidget = ({ value, children, className }) =>
value ? (
<a
href={'mailto:' + children}
href={'mailto:' + value}
className={cx(className, 'email', 'widget')}
rel="noreferrer"
target="_blank"
>
{children}
{children ? children(value) : value}
</a>
) : (
''
Expand Down
12 changes: 11 additions & 1 deletion src/components/theme/Widgets/EmailWidget.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,17 @@ describe('EmailWidget', () => {

it('renders an email view widget component', () => {
const component = renderer.create(
<EmailWidget className="metadata">foo@bar.com</EmailWidget>,
<EmailWidget className="metadata" value="foo@bar.com" />,
);
const json = component.toJSON();
expect(json).toMatchSnapshot();
});

it('renders an email view widget component with children', () => {
const component = renderer.create(
<EmailWidget className="metadata" value="foo@bar.com">
{(child) => <strong>{child}</strong>}
</EmailWidget>,
);
const json = component.toJSON();
expect(json).toMatchSnapshot();
Expand Down
13 changes: 8 additions & 5 deletions src/components/theme/Widgets/PasswordWidget.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import React from 'react';
import cx from 'classnames';

export const PasswordWidget = ({ children, className }) => (
<span className={cx(className, 'password', 'widget')}>
{children ? '********' : ''}
</span>
);
export const PasswordWidget = ({ value, children, className }) =>
value ? (
<span className={cx(className, 'password', 'widget')}>
{children ? children('********') : '********'}
</span>
) : (
''
);
12 changes: 10 additions & 2 deletions src/components/theme/Widgets/PasswordWidget.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,16 @@ describe('PasswordWidget', () => {

it('renders a password view widget component', () => {
const component = renderer.create(
<PasswordWidget className="metadata">
A very strong password
<PasswordWidget className="metadata" value="A very strong password" />,
);
const json = component.toJSON();
expect(json).toMatchSnapshot();
});

it('renders a password view widget component with children', () => {
const component = renderer.create(
<PasswordWidget className="metadata" value="A very strong password">
{(child) => <strong>{child}</strong>}
</PasswordWidget>,
);
const json = component.toJSON();
Expand Down
8 changes: 5 additions & 3 deletions src/components/theme/Widgets/RichTextWidget.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import React from 'react';
import cx from 'classnames';

export const RichTextWidget = ({ children, className }) =>
children ? (
export const RichTextWidget = ({ value, className }) =>
value ? (
<p
className={cx(className, 'richtext', 'widget')}
dangerouslySetInnerHTML={{ __html: children.data }}
dangerouslySetInnerHTML={{
__html: value.data,
}}
/>
) : (
''
Expand Down
16 changes: 14 additions & 2 deletions src/components/theme/Widgets/RichTextWidget.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,22 @@ describe('RichTextWidget', () => {
const json = component.toJSON();
expect(json).toMatchSnapshot();
});

it('renders a rich text view widget component', () => {
const component = renderer.create(
<RichTextWidget className="metadata">
{{ data: '<b>Foo bar</b>' }}
<RichTextWidget
className="metadata"
value={{ data: '<b>Foo bar</b>' }}
/>,
);
const json = component.toJSON();
expect(json).toMatchSnapshot();
});

it('renders a rich text view widget component with children', () => {
const component = renderer.create(
<RichTextWidget className="metadata" value={{ data: '<b>Foo bar</b>' }}>
{(child) => <strong>{child}</strong>}
</RichTextWidget>,
);
const json = component.toJSON();
Expand Down
15 changes: 10 additions & 5 deletions src/components/theme/Widgets/SelectWidget.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import React from 'react';
import cx from 'classnames';

export const SelectWidget = ({ children, className }) => (
<span className={cx(className, 'select', 'widget')}>
{children?.title || children?.token || children}
</span>
);
export const SelectWidget = ({ value, children, className }) =>
value ? (
<span className={cx(className, 'select', 'widget')}>
{children
? children(value?.title || value?.token || value)
: value?.title || value?.token || value}
</span>
) : (
''
);
Loading

0 comments on commit dc94a40

Please sign in to comment.