Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 10 additions & 0 deletions example/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@ class App extends React.Component {
/>
</div>

<div className="example">
<h3>Custom allowed decimal separators with decimal precision</h3>
<NumberFormat
thousandSeparator={' '}
allowedDecimalSeparators={['.', ',']}
decimalSeparator='.'
decimalScale={2}
/>
</div>

<div className="example">
<h3>Format with pattern : Format credit card in an input</h3>
<NumberFormat format="#### #### #### ####" mask="_" />
Expand Down
10 changes: 10 additions & 0 deletions src/number_format.js
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,16 @@ class NumberFormat extends React.Component {
);
}

/** Check for "paste event" with, replace any allowed decimal separator with desired decimal separator */
if (!format && decimalScale !== 0 && allowedDecimalSeparators.some(separator => value.indexOf(separator) !== -1)) {
const separator = decimalScale === 0 ? '' : decimalSeparator
let result = value;
allowedDecimalSeparators.forEach(v => {
result = result.replace(v, separator);
})
value = result;
}

const leftBound = !!format ? 0 : prefix.length;
const rightBound = lastValue.length - (!!format ? 0 : suffix.length);

Expand Down
16 changes: 16 additions & 0 deletions test/library/input_numeric_format.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,22 @@ describe('Test NumberFormat as input with numeric format options', () => {
expect(wrapper.state().value).toEqual('23,4456 Sq. ft');
});


it('should allow pasting 123,45 with decimal separator set to . and allowedDecimalSeparators=["," , "."]', () => {
const wrapper = shallow(
<NumberFormat
displayType="input"
thousandSeparator={' '}
prefix=""
allowedDecimalSeparators={['.', ',']}
decimalSeparator='.'
decimalScale={2}
/>,
);
simulateKeyInput(wrapper.find('input'), '123,45');
expect(wrapper.state().value).toEqual('123.45');
});

it('should not break if suffix/prefix has negation sign. Issue #245', () => {
const wrapper = shallow(<NumberFormat suffix="-" />);

Expand Down