Skip to content
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

add support for paste event given allowedDecimalSeparators #556

Open
wants to merge 5 commits 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: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ In typescript you also have to enable `"esModuleInterop": true` in your tsconfig
| isAllowed | ([values](#values-object)) => true or false | none | A checker function to check if input value is valid or not. If this function returns false, the onChange method will not get triggered |
| renderText | (formattedValue, customProps) => React Element | null | A renderText method useful if you want to render formattedValue in different element other than span. It also returns the custom props that are added to the component which can allow passing down props to the rendered element |
| getInputRef | (elm) => void | null | Method to get reference of input, span (based on displayType prop) or the customInput's reference. See [Getting reference](#getting-reference)
| allowedDecimalSeparators | array of char | none | Characters which when pressed result in a decimal separator. When missing, decimal separator and '.' are used |
| allowedDecimalSeparators | array of char | none | Characters which when pressed or pasted from the clipboard will result in a decimal separator. When missing, decimal separator and '.' are used |

**Other than this it accepts all the props which can be given to a input or span based on displayType you selected.**

Expand Down
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
value={11.11}
allowedDecimalSeparators={['.', ',']}
decimalSeparator='.'
decimalScale={2}
/>
Toumash marked this conversation as resolved.
Show resolved Hide resolved
</div>

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

const decimalSeparatorRegex = new RegExp(allowedDecimalSeparators.map(escapeRegExp).join('|'), 'g');
/** Check for "paste event" with, replace any allowed decimal separator with desired decimal separator. Issue #349 */
if (!format && decimalScale !== 0 && value.match(decimalSeparatorRegex)) {
value = value.replace(decimalSeparatorRegex, decimalSeparator);
}

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

Expand Down
33 changes: 33 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,39 @@ describe('Test NumberFormat as input with numeric format options', () => {
expect(wrapper.state().value).toEqual('23,4456 Sq. ft');
});


it('should work with the configured decimal separators based on the allowedDecimalSeparators prop. Issue #349', () => {
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 work with the configured decimal separators based on the allowedDecimalSeparators prop and should not replace prefix and suffix. Issue #349', () => {
const wrapper = shallow(
<NumberFormat
displayType="input"
thousandSeparator={' '}
prefix="prefix,"
suffix="suffix,"
allowedDecimalSeparators={['.', ',']}
decimalSeparator='.'
decimalScale={2}
value={987}
/>,
);
simulateKeyInput(wrapper.find('input'), '123,45');
expect(wrapper.state().value).toEqual('prefix,987 123.45suffix,');
});

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

Expand Down