Skip to content

Commit

Permalink
added PropTypes support
Browse files Browse the repository at this point in the history
  • Loading branch information
Miguel Vieira committed Dec 11, 2017
1 parent b3a7293 commit c8677bd
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"max-len": ["error", 120, 2],
// disallow adding to native types
"no-extend-native": 1,
"no-debugger": "warn"
"no-debugger": "warn",
"react/require-default-props": [0, { "forbidDefaultForRequired": false }]
}
}
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ React Intl
### Features

- Display numbers (on Input) with separators.
- Pluralize labels in strings.
- Support for 150+ languages.
- Support for 150+ countries.
- Runs in the browser and Node.js.
- Built on standards.

Expand Down Expand Up @@ -68,10 +67,10 @@ export default App;
### Props
| Props | Options | Default | Description |
| ------------- |-------------| -----| -------- |
| currency | Ex: USD, EUR, GBP, BRL etc... | required | International Organization for Standardization publishes a list of standard currency codes referred to as the ISO 4217 code list |
| currency | Ex: USD, EUR, GBP, BRL etc... | required | International Organization for Standardization publishes a list of standard currency codes referred to as the ISO 4217 code list. |
| placeholder | Ex: 0.00, €150.00, US$150,00 etc ...| optional | The placeholder attribute specifies a short hint that describes the expected value of an input field (e.g. a sample value or a short description of the expected format). |
| value | Ex. 1000 | optional | Value to the number format. It can be a float number, or formatted string. |
| onChange | Ex: (values) => {} | none | onChange handler to get values object for usage in your component |
| onChange | Ex: (values) => {} | none | onChange handler to get values object for usage in your component. |

#### values object
values object is on following format
Expand Down
16 changes: 14 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { Component } from 'react';
import { injectIntl } from 'react-intl';
import PropTypes from 'prop-types';
import { injectIntl, intlShape } from 'react-intl';
import { removeAllButLast } from './utils';

class FormatCurrency extends Component {
Expand Down Expand Up @@ -43,7 +44,7 @@ class FormatCurrency extends Component {
e.persist();
const el = e.target;
const inputValue = el.value;
const regex = /^[0-9]+(\.|\,){1}[0-9]+$/;
const regex = /^[0-9]+(\.,){1}[0-9]+$/;

if (!regex.test(inputValue)) {
el.value = removeAllButLast(inputValue.replace(/[^\d,.]/g, '').replace(',', '.'), '.');
Expand Down Expand Up @@ -106,4 +107,15 @@ class FormatCurrency extends Component {
}
}

FormatCurrency.propTypes = {
value: PropTypes.oneOfType([
PropTypes.number,
PropTypes.string,
]),
currency: PropTypes.string.isRequired,
placeholder: PropTypes.string,
onChange: PropTypes.func,
intl: intlShape,
};

export default injectIntl(FormatCurrency);
3 changes: 3 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
function returnTrue() { return true; }

function removeAllButLast(string, token) {
const parts = string.split(token);

Expand All @@ -8,5 +10,6 @@ function removeAllButLast(string, token) {
return (parts.length > 1) ? parts.slice(0, -1).join('') + token + parts.slice(-1) : '';
}
export {
returnTrue,
removeAllButLast,
};

0 comments on commit c8677bd

Please sign in to comment.