Skip to content

Commit

Permalink
5520 – Update Number and List components empty states (#2169)
Browse files Browse the repository at this point in the history
Explicitly passing null got a blank output. So we updated how we deal with those values by creating getDisplayValue function, and added a new test for it. 

References: 5520, 5327
PR: 2169
  • Loading branch information
vasconsaurus authored Oct 23, 2024
1 parent 9c79b64 commit 981d761
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/app/components/cds/Sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -2104,7 +2104,7 @@ const SandboxComponent = ({ admin }) => {
>
<option value="2024">2024</option>
<option value="0">0</option>
<option value="-">null</option>
<option value="null">null</option>
</Select>
</li>
<li>
Expand Down Expand Up @@ -2170,7 +2170,7 @@ const SandboxComponent = ({ admin }) => {
id: 'item1',
},
{
itemValue: '94607',
itemValue: null,
itemLink: 'e.not/a/working/url/',
itemText: 'Should not have a link. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam varius commodo malesuada',
id: 'item2',
Expand Down
7 changes: 4 additions & 3 deletions src/app/components/cds/charts/ListWidgetItem.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import PropTypes from 'prop-types';
import { getDisplayValue } from './NumberWidget';
import { isValidURL } from '../../../helpers';
import styles from './ListWidget.module.css';

Expand All @@ -16,22 +17,22 @@ const ListWidgetItem = ({
:
<div className={styles.listWidgetItemText}>{itemText}</div>
}
<div className={styles.listWidgetItemValue}>{itemValue}</div>
<div className={styles.listWidgetItemValue}>{getDisplayValue(itemValue)}</div>
</li>
);

ListWidgetItem.defaultProps = {
id: null,
itemLink: null,
itemText: null,
itemValue: '-',
itemValue: null,
};

ListWidgetItem.propTypes = {
id: PropTypes.string,
itemLink: PropTypes.string,
itemText: PropTypes.node,
itemValue: PropTypes.string,
itemValue: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
};

export default ListWidgetItem;
41 changes: 27 additions & 14 deletions src/app/components/cds/charts/NumberWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,56 @@ import React from 'react';
import PropTypes from 'prop-types';
import styles from './NumberWidget.module.css';

const getDisplayValue = (itemCount) => {
const stringValue = String(itemCount);

if (stringValue === 'null') {
return '-';
}

return stringValue;
};

const NumberWidget = ({
color,
contextText,
itemCount,
title,
unit,
}) => (
<div className={styles.numberWidgetWrapper} style={{ backgroundColor: color }}>
<div className={styles.numberWidgetTitle}>
{title}
</div>
{ itemCount &&
}) => {
const displayValue = getDisplayValue(itemCount);

return (
<div className={styles.numberWidgetWrapper} style={{ backgroundColor: color }}>
<div className={styles.numberWidgetTitle}>
{title}
</div>
<div className={styles.numberWidgetItemCount}>
{itemCount} { unit && itemCount !== '-' && <span className={styles.numberWidgetUnit}>{unit}</span> }
{ displayValue } { unit && displayValue !== '-' && <span className={styles.numberWidgetUnit}>{unit}</span> }
</div>
}
{ contextText &&
{ contextText &&
<div className={styles.numberWidgetContextText}>
{contextText}
</div>
}
</div>
);
}
</div>
);
};

NumberWidget.defaultProps = {
color: 'var(--color-blue-90)',
contextText: null,
itemCount: '-',
itemCount: null,
unit: null,
};

NumberWidget.propTypes = {
color: PropTypes.string,
contextText: PropTypes.node,
itemCount: PropTypes.string,
itemCount: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
title: PropTypes.node.isRequired,
unit: PropTypes.node,
};

export { getDisplayValue };
export default NumberWidget;
10 changes: 9 additions & 1 deletion src/app/components/cds/charts/NumberWidget.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import NumberWidget from './NumberWidget';
import NumberWidget, { getDisplayValue } from './NumberWidget';
import { mountWithIntl } from '../../../../../test/unit/helpers/intl-test';

describe('<NumberWidget />', () => {
Expand All @@ -26,4 +26,12 @@ describe('<NumberWidget />', () => {
it('renders without crashing if not required are null', () => {
mountWithIntl(<NumberWidget {...nullProps} />);
});

it('displays the a random number, 0 or null correctly', () => {
expect(getDisplayValue(0)).toEqual('0');
expect(getDisplayValue(2024)).toEqual('2024');
expect(getDisplayValue('0')).toEqual('0');
expect(getDisplayValue('2024')).toEqual('2024');
expect(getDisplayValue(null)).toEqual('-');
});
});

0 comments on commit 981d761

Please sign in to comment.