-
Notifications
You must be signed in to change notification settings - Fork 396
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
decimalPrecision option and supporting tests #15
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ function escapeRegExp(str) { | |
const propTypes = { | ||
thousandSeparator: PropTypes.oneOf([',', '.', true, false]), | ||
decimalSeparator: PropTypes.oneOf([',', '.', true, false]), | ||
decimalPrecision: PropTypes.oneOfType([PropTypes.number, PropTypes.bool]), | ||
displayType: PropTypes.oneOf(['input', 'text']), | ||
prefix: PropTypes.string, | ||
suffix: PropTypes.string, | ||
|
@@ -24,7 +25,8 @@ const propTypes = { | |
|
||
const defaultProps = { | ||
displayType: 'input', | ||
decimalSeparator: '.' | ||
decimalSeparator: '.', | ||
decimalPrecision: false | ||
}; | ||
|
||
class NumberFormat extends React.Component { | ||
|
@@ -119,7 +121,8 @@ class NumberFormat extends React.Component { | |
|
||
formatInput(val) { | ||
const {prefix, suffix, mask, format} = this.props; | ||
const {thousandSeparator, decimalSeparator} = this.getSeparators() | ||
const {thousandSeparator, decimalSeparator} = this.getSeparators(); | ||
const {decimalPrecision} = this.props; | ||
const maskPattern = format && typeof format == 'string' && !!mask; | ||
|
||
const numRegex = this.getNumberRegex(true); | ||
|
@@ -139,9 +142,14 @@ class NumberFormat extends React.Component { | |
} | ||
else{ | ||
let beforeDecimal = formattedValue, afterDecimal = ''; | ||
const hasDecimals = formattedValue.indexOf(decimalSeparator) !== -1; | ||
const hasDecimals = formattedValue.indexOf(decimalSeparator) !== -1 || decimalPrecision !== false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Based on current default value of decimalPrecision, user always have to define decimalPrecision otherwise he will not be able to add it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not be able to add what? If they don't define decimalPrecision, it would bypass my change altogether and function as it normally would. Am I missing something? |
||
if(decimalSeparator && hasDecimals) { | ||
const parts = formattedValue.split(decimalSeparator) | ||
if(decimalPrecision !== false) { | ||
const precision = decimalPrecision === true ? 2 : decimalPrecision; | ||
var parts = parseFloat(formattedValue).toFixed(precision).split(decimalSeparator); | ||
}else { | ||
var parts = formattedValue.split(decimalSeparator); | ||
} | ||
beforeDecimal = parts[0]; | ||
afterDecimal = parts[1]; | ||
} | ||
|
@@ -152,7 +160,7 @@ class NumberFormat extends React.Component { | |
if(prefix) beforeDecimal = prefix + beforeDecimal; | ||
if(suffix) afterDecimal = afterDecimal + suffix; | ||
|
||
formattedValue = beforeDecimal + (hasDecimals && decimalSeparator || '') + afterDecimal; | ||
formattedValue = beforeDecimal + (hasDecimals && decimalSeparator || '') + afterDecimal; | ||
} | ||
|
||
return { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep then default value of decimal precision as unlimited
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure I understand your comment. If they pass false to the decimalPrecision, it would be unlimited number of decimal places. Could you explain this a little more?