Skip to content

Adding maxValue & minValue support #78

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,7 @@ selectAllOnFocus | false | Selects all text on focus or does not
prefix | '' | Currency prefix
suffix | '' | Currency suffix
autoFocus | false | Autofocus
maxValue | undefined | Maximum input value (state won't change if input is higher)
minValue | undefined | Minimum input value (state won't change if input is lower)

***Note:** Enabling any mask-related features such as prefix, suffix or separators with an inputType="number" or "tel" could trigger errors. Most of those characters would be invalid in such input types.
13 changes: 12 additions & 1 deletion lib/react-currency-input.cjs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/react-currency-input.cjs.js.map

Large diffs are not rendered by default.

13 changes: 12 additions & 1 deletion lib/react-currency-input.es.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/react-currency-input.es.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/react-currency-input.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/react-currency-input.min.js.map

Large diffs are not rendered by default.

13 changes: 12 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class CurrencyInput extends Component {
delete customProps.suffix;
delete customProps.selectAllOnFocus;
delete customProps.autoFocus;
delete customProps.maxValue;
delete customProps.minValue;

let initialValue = props.value;
if (initialValue === null) {
Expand Down Expand Up @@ -194,6 +196,7 @@ class CurrencyInput extends Component {
*/
handleChange(event) {
event.preventDefault();

let { maskedValue, value } = mask(
event.target.value,
this.props.precision,
Expand All @@ -204,6 +207,12 @@ class CurrencyInput extends Component {
this.props.suffix
);

if ((this.props.maxValue && value > this.props.maxValue)
|| (this.props.minValue && value < this.props.maxValue)) {
// if value exceeds min & max limits, do nothing...
return;
}

event.persist(); // fixes issue #23

this.setState({ maskedValue, value }, () => {
Expand Down Expand Up @@ -290,7 +299,9 @@ CurrencyInput.defaultProps = {
allowNegative: false,
prefix: '',
suffix: '',
selectAllOnFocus: false
selectAllOnFocus: false,
maxValue: undefined,
minValue: undefined
};


Expand Down