Skip to content

Commit

Permalink
Merge pull request #3605 from ulasturann/master
Browse files Browse the repository at this point in the history
Fix #3595: Add maxLength prop to InputNumber Component.
  • Loading branch information
ulasturann authored Nov 10, 2022
2 parents 141708e + 37138a4 commit 40c1dd9
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion api-generator/components/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ const AutoCompleteProps = [
description: 'When present, it specifies that the component should be disabled.'
},
{
name: 'maxlength',
name: 'maxLength',
type: 'number',
default: 'null',
description: 'Maximum number of character allows in the input field.'
Expand Down
2 changes: 1 addition & 1 deletion api-generator/components/inputmask.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const InputMaskProps = [
description: 'Size of the input field.'
},
{
name: 'maxlength',
name: 'maxLength',
type: 'number',
default: 'null',
description: 'Maximum number of character allows in the input field.'
Expand Down
6 changes: 6 additions & 0 deletions api-generator/components/inputnumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ const InputNumberProps = [
default: 'null',
description: 'Size of the input field.'
},
{
name: 'maxLength',
type: 'number',
default: 'null',
description: 'Maximum number of character allows in the input field.'
},
{
name: 'style',
type: 'string',
Expand Down
2 changes: 1 addition & 1 deletion components/doc/autocomplete/apidoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ export function ApiDoc(props) {
<td>When present, it specifies that the component should be disabled.</td>
</tr>
<tr>
<td>maxlength</td>
<td>maxLength</td>
<td>number</td>
<td>null</td>
<td>Maximum number of character allows in the input field.</td>
Expand Down
2 changes: 1 addition & 1 deletion components/doc/inputmask/apidoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export function ApiDoc(props) {
<td>Size of the input field.</td>
</tr>
<tr>
<td>maxlength</td>
<td>maxLength</td>
<td>number</td>
<td>null</td>
<td>Maximum number of character allows in the input field.</td>
Expand Down
6 changes: 6 additions & 0 deletions components/doc/inputnumber/apidoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,12 @@ export function ApiDoc(props) {
<td>null</td>
<td>Size of the input field.</td>
</tr>
<tr>
<td>maxLength</td>
<td>number</td>
<td>null</td>
<td>Maximum number of character allows in the input field.</td>
</tr>
<tr>
<td>style</td>
<td>string</td>
Expand Down
9 changes: 9 additions & 0 deletions components/lib/inputnumber/InputNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ export const InputNumber = React.memo(
let currentValue = parseValue(inputRef.current.value) || 0;
let newValue = validateValue(currentValue + step);

if (props.maxLength && props.maxLength < formatValue(newValue).length) {
return;
}

// touch devices trigger the keyboard to display because of setSelectionRange
!DomHandler.isTouchDevice() && updateInput(newValue, null, 'spin');
updateModel(event, newValue);
Expand Down Expand Up @@ -809,6 +813,10 @@ export const InputNumber = React.memo(
let selectionStart = inputEl.selectionStart;
let selectionEnd = inputEl.selectionEnd;

if (props.maxLength && props.maxLength < newValue.length) {
return;
}

inputEl.value = newValue;
let newLength = newValue.length;

Expand Down Expand Up @@ -1168,6 +1176,7 @@ InputNumber.defaultProps = {
localeMatcher: undefined,
max: null,
maxFractionDigits: undefined,
maxLength: null,
min: null,
minFractionDigits: undefined,
mode: 'decimal',
Expand Down
1 change: 1 addition & 0 deletions components/lib/inputnumber/inputnumber.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export interface InputNumberProps extends Omit<React.DetailedHTMLProps<React.HTM
step?: number;
min?: number;
max?: number;
maxLength?: number;
disabled?: boolean;
required?: boolean;
tabIndex?: number;
Expand Down

0 comments on commit 40c1dd9

Please sign in to comment.