Skip to content

Commit e96d0ec

Browse files
committed
* #3 added feature to support prefix and suffix currency symbols
* bumped version number to 1.0.7
1 parent b7d9273 commit e96d0ec

File tree

6 files changed

+129
-9
lines changed

6 files changed

+129
-9
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,21 @@ Specify a specific precision:
8282
```
8383
8484
85+
## Currency
86+
87+
Optionally set a currency symbol as a prefix or suffix
88+
```javascript
89+
// $1,234,567.89
90+
<CurrencyInput prefix="$" />
91+
```
92+
93+
```javascript
94+
// 1,234,567.89 kr
95+
<CurrencyInput suffix=" kr" />
96+
```
97+
98+
99+
85100
All other attributes are applied to the input element. For example, you can integrate bootstrap styling:
86101
87102
```javascript
@@ -102,5 +117,7 @@ All other attributes are applied to the input element. For example, you can int
102117
| thousandSeparator | ',' | The thousand separator |
103118
| inputType | "text" | Input field tag type. You may want to use `number` or `tel` |
104119
| allowNegative | false | Allows negative numbers in the input |
120+
| prefix | '' | Currency prefix |
121+
| suffix | '' | Currency suffix |
105122
106123

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-currency-input",
3-
"version": "1.0.6",
3+
"version": "1.0.7",
44
"description": "React component for inputing currency amounts",
55
"main": "lib/index.js",
66
"scripts": {

src/index.js

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ const CurrencyInput = React.createClass({
2020
thousandSeparator: PropTypes.string,
2121
precision: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
2222
inputType: PropTypes.string,
23-
allowNegative: PropTypes.bool
23+
allowNegative: PropTypes.bool,
24+
prefix: PropTypes.string,
25+
suffix: PropTypes.string
2426
},
2527

2628

@@ -40,7 +42,9 @@ const CurrencyInput = React.createClass({
4042
thousandSeparator: ",",
4143
precision: "2",
4244
inputType: "text",
43-
allowNegative: false
45+
allowNegative: false,
46+
prefix: '',
47+
suffix: ''
4448
}
4549
},
4650

@@ -61,8 +65,18 @@ const CurrencyInput = React.createClass({
6165
delete customProps.precision;
6266
delete customProps.inputType;
6367
delete customProps.allowNegative;
68+
delete customProps.prefix;
69+
delete customProps.suffix;
6470
return {
65-
maskedValue: mask(this.props.value, this.props.precision, this.props.decimalSeparator, this.props.thousandSeparator, this.props.allowNegative),
71+
maskedValue: mask(
72+
this.props.value,
73+
this.props.precision,
74+
this.props.decimalSeparator,
75+
this.props.thousandSeparator,
76+
this.props.allowNegative,
77+
this.props.prefix,
78+
this.props.suffix
79+
),
6680
customProps: customProps
6781
}
6882
},
@@ -85,7 +99,15 @@ const CurrencyInput = React.createClass({
8599
delete customProps.inputType;
86100
delete customProps.allowNegative;
87101
this.setState({
88-
maskedValue: mask(nextProps.value, nextProps.precision, nextProps.decimalSeparator, nextProps.thousandSeparator, nextProps.allowNegative),
102+
maskedValue: mask(
103+
nextProps.value,
104+
nextProps.precision,
105+
nextProps.decimalSeparator,
106+
nextProps.thousandSeparator,
107+
nextProps.allowNegative,
108+
nextProps.prefix,
109+
nextProps.suffix
110+
),
89111
customProps: customProps
90112
});
91113
},
@@ -107,7 +129,15 @@ const CurrencyInput = React.createClass({
107129
*/
108130
handleChange(event){
109131
event.preventDefault();
110-
let maskedValue = mask(event.target.value, this.props.precision, this.props.decimalSeparator, this.props.thousandSeparator, this.props.allowNegative);
132+
let maskedValue = mask(
133+
event.target.value,
134+
this.props.precision,
135+
this.props.decimalSeparator,
136+
this.props.thousandSeparator,
137+
this.props.allowNegative,
138+
this.props.prefix,
139+
this.props.suffix
140+
);
111141
this.setState({maskedValue: maskedValue});
112142
this.props.onChange(maskedValue);
113143
},

src/mask.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11

2-
export default function mask(value, precision, decimalSeparator, thousandSeparator, allowNegative){
2+
export default function mask(value, precision, decimalSeparator, thousandSeparator, allowNegative, prefix, suffix){
33
// provide some default values and arg validation.
44
if (decimalSeparator === undefined){decimalSeparator = ".";} // default to '.' as decimal separator
55
if (thousandSeparator === undefined){thousandSeparator = ",";} // default to ',' as thousand separator
66
if (allowNegative === undefined){allowNegative = false;} // default to not allowing negative numbers
77
if (precision === undefined){precision = 2;} // by default, 2 decimal places
88
if (precision < 0) {precision = 0;} // precision cannot be negative.
99
if (precision > 20) {precision = 20;} // precision cannot greater than 20
10+
if (prefix === undefined){prefix = '';} // default prefix to empty string
11+
if (suffix === undefined){suffix = '';} // default suffix to empty string
1012

1113
let numberIsNegative = false;
1214
if (allowNegative) {
@@ -67,5 +69,10 @@ export default function mask(value, precision, decimalSeparator, thousandSeparat
6769
digits.unshift('-');
6870
}
6971

70-
return digits.join('');
72+
// if we have a prefix or suffix, add them in.
73+
if (prefix.length > 0){digits.unshift(prefix);}
74+
if (suffix.length > 0){digits.push(suffix);}
75+
76+
77+
return digits.join('').trim();
7178
}

test/index.spec.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,42 @@ describe('react-currency-input', function(){
170170
});
171171

172172

173+
describe('currency prefix', function() {
174+
175+
before('render and locate element', function () {
176+
this.renderedComponent = ReactTestUtils.renderIntoDocument(
177+
<CurrencyInput onChange={this.handleChange} value="0" prefix="$"/>
178+
);
179+
180+
this.inputComponent = ReactTestUtils.findRenderedDOMComponentWithTag(
181+
this.renderedComponent,
182+
'input'
183+
);
184+
});
185+
186+
it('should render the prefix', function() {
187+
expect(this.renderedComponent.getMaskedValue()).to.equal('$0.00');
188+
});
189+
190+
});
191+
192+
describe('currency suffix', function() {
193+
194+
before('render and locate element', function () {
195+
this.renderedComponent = ReactTestUtils.renderIntoDocument(
196+
<CurrencyInput onChange={this.handleChange} value="0" suffix=" kr"/>
197+
);
198+
199+
this.inputComponent = ReactTestUtils.findRenderedDOMComponentWithTag(
200+
this.renderedComponent,
201+
'input'
202+
);
203+
});
204+
205+
it('should render the suffix', function() {
206+
expect(this.renderedComponent.getMaskedValue()).to.equal('0.00 kr');
207+
});
208+
209+
});
210+
173211
});

test/mask.spec.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import mask from '../src/mask'
55
describe('mask', function(){
66

77

8-
it('should change "0" to "$0.00"', function(){
8+
it('should change "0" to "0.00"', function(){
99
expect(mask("0")).to.equal("0.00");
1010
});
1111

@@ -119,4 +119,32 @@ describe('mask', function(){
119119

120120

121121

122+
describe('with currency symbol', function(){
123+
124+
it('"$" prefix should change "0" to "$0.00"', function(){
125+
expect(mask("0","2",".",",",true,"$","")).to.equal("$0.00");
126+
});
127+
128+
it('"kr" suffix should change "0" to "0.00kr"', function(){
129+
expect(mask("0","2",".",",",true,"","kr")).to.equal("0.00kr");
130+
});
131+
132+
it('can have both a prefix and a suffix', function(){
133+
expect(mask("0","2",".",",",true,"$","kr")).to.equal("$0.00kr");
134+
});
135+
136+
it('does not strip whitespaces between amount and symbol', function(){
137+
expect(mask("0","2",".",",",true,"$ ","")).to.equal("$ 0.00");
138+
expect(mask("0","2",".",",",true,""," kr")).to.equal("0.00 kr");
139+
});
140+
141+
it('strips whitespaces before and after value', function(){
142+
expect(mask("0","2",".",",",true," $ ","")).to.equal("$ 0.00");
143+
expect(mask("0","2",".",",",true,""," kr ")).to.equal("0.00 kr");
144+
});
145+
146+
});
147+
148+
149+
122150
});

0 commit comments

Comments
 (0)