Skip to content

Commit

Permalink
feat(amount-input): added integerLength prop
Browse files Browse the repository at this point in the history
* feat(amount-input): added integerLength prop

* fix(amount-input): fixed stories prop name

Co-authored-by: Evgeny Sergeev <SiebenSieben@gmail.com>
  • Loading branch information
goncharovroman and SiebenSieben committed Dec 11, 2020
1 parent ecd5652 commit 881af44
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/amount-input/src/Component.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { name, version } from '../package.json';
<AmountInput
value={number('value', null)}
currency={text('currency', 'RUR')}
integerLength={number('integerLength', 9)}
minority={number('minority', 100)}
bold={boolean('bold', true)}
block={boolean('block', false)}
Expand Down
10 changes: 9 additions & 1 deletion packages/amount-input/src/Component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export type AmountInputProps = Omit<InputProps, 'value' | 'onChange' | 'type'> &
*/
currency?: CurrencyCodes;

/**
* Максимальное число знаков до запятой
*/
integerLength?: number;

/**
* Минорные единицы
*/
Expand Down Expand Up @@ -61,6 +66,7 @@ export const AmountInput = forwardRef<HTMLInputElement, AmountInputProps>(
(
{
value = null,
integerLength = 9,
minority = 100,
currency = 'RUR',
placeholder = `0\u2009${getCurrencySymbol(currency) || ''}`,
Expand Down Expand Up @@ -106,7 +112,9 @@ export const AmountInput = forwardRef<HTMLInputElement, AmountInputProps>(
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const input = e.target;
const enteredValue = input.value.replace(/\s/g, '').replace('.', ',');
const isCorrectEnteredValue = /(^[0-9]{1,9}(,([0-9]+)?)?$|^\s*$)/.test(enteredValue);
const isCorrectEnteredValue = RegExp(
`(^[0-9]{1,${integerLength}}(,([0-9]+)?)?$|^\\s*$)`,
).test(enteredValue);

if (isCorrectEnteredValue) {
const newFormatedValue = getFormatedValue(enteredValue, currency, minority);
Expand Down

0 comments on commit 881af44

Please sign in to comment.