Skip to content

Commit

Permalink
fix: make value required only if useDashForInvalidValues is true
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Bexelius authored and bstream committed Oct 18, 2018
1 parent 6eccfb8 commit 0c93f26
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/components/currency/currency.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import PropTypes from 'prop-types';
import React from 'react';
import Number from '../number/number';
import variables from '../../variables';
import { valuePropType } from '../../utils';

/**
This is the `<Currency /> component`
Expand Down Expand Up @@ -42,7 +43,7 @@ Currency.defaultProps = {
};

Currency.propTypes = {
value: PropTypes.any.isRequired, // eslint-disable-line
value: valuePropType,
/**
Syntactic sugar for `suffix` (either one can be used)
*/
Expand Down
3 changes: 2 additions & 1 deletion src/components/development/development.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import classNames from 'classnames';
import NumberComponent from '../number/number';
import CurrencyComponent from '../currency/currency';
import PercentComponent from '../percent/percent';
import { valuePropType } from '../../utils';

function renderSign(direction) {
switch (direction) {
Expand Down Expand Up @@ -88,7 +89,7 @@ export default function Development({

Development.propTypes = {
className: PropTypes.string,
value: PropTypes.any.isRequired, // eslint-disable-line
value: valuePropType,
decimals: PropTypes.number,
type: PropTypes.oneOf(['number', 'currency', 'percentage']),
direction: PropTypes.oneOf(['positive', 'negative', 'neutral']),
Expand Down
4 changes: 2 additions & 2 deletions src/components/number/number.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React from 'react';
import { injectIntl, intlShape } from 'react-intl';
import classNames from 'classnames';
import Addon from '../addon/addon';
import { getTickDecimals, getFractionDigits } from '../../utils';
import { getTickDecimals, getFractionDigits, valuePropType } from '../../utils';

/**
This component is not intended for public use
Expand Down Expand Up @@ -88,7 +88,7 @@ NumberComponent.propTypes = {
className: PropTypes.string,
style: PropTypes.object,
useDashForInvalidValues: PropTypes.bool,
value: PropTypes.any.isRequired, // eslint-disable-line
value: valuePropType,
valueClass: PropTypes.string,
valueDecimals: PropTypes.number,
valueMaxDecimals: PropTypes.number,
Expand Down
3 changes: 2 additions & 1 deletion src/components/percent/percent.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import PropTypes from 'prop-types';
import React from 'react';
import Number from '../number/number';
import { valuePropType } from '../../utils';

/**
This is the `<Percent /> component`
Expand All @@ -27,7 +28,7 @@ Percent.defaultProps = {
};

Percent.propTypes = {
value: PropTypes.any.isRequired, // eslint-disable-line
value: valuePropType,
decimals: PropTypes.number,
/**
Default is an empty string (`''`)
Expand Down
3 changes: 2 additions & 1 deletion src/components/value/value.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import PropTypes from 'prop-types';
import React from 'react';
import Number from '../number/number';
import { valuePropType } from '../../utils';

/**
This is the `<Value /> component`
Expand All @@ -16,7 +17,7 @@ Value.defaultProps = {
};

Value.propTypes = {
value: PropTypes.any.isRequired, // eslint-disable-line
value: valuePropType,
decimals: PropTypes.number,
maxDecimals: PropTypes.number,
minDecimals: PropTypes.number,
Expand Down
2 changes: 1 addition & 1 deletion src/components/value/value.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Simple examples:
<Value prefix="Value:" prefixSeparator=" " value={ 2.4444 } />
</span>
<span style={{marginRight: '2rem'}} title="Invalid value with handling">
<Value useDashForInvalidValues value={ Number.NEGATIVE_INFINITY } />
<Value useDashForInvalidValues />
</span>
<span style={{marginRight: '2rem'}} title="Abbreviations">
<Value value={ 1420000 } abbreviation="million"/>
Expand Down
20 changes: 20 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import PropTypes from 'prop-types';

export function numberIsFinite(value) {
return typeof value === 'number' && isFinite(value); // eslint-disable-line
}
Expand All @@ -22,3 +24,21 @@ export const getDevelopmentPercentage = (previous, current) => {
}
return (current / previous - 1) * 100;
};

const propTypeValue = {
value: PropTypes.any,
};

const propTypeValueRequired = {
value: PropTypes.any.isRequired,
};

export const valuePropType = (props, propName, componentName) => {
const { useDashForInvalidValues } = props;

if (useDashForInvalidValues) {
return PropTypes.checkPropTypes(propTypeValue, props, propName, componentName);
}

return PropTypes.checkPropTypes(propTypeValueRequired, props, propName, componentName);
};

0 comments on commit 0c93f26

Please sign in to comment.