forked from openshift/console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
integer.tsx
161 lines (143 loc) · 4.19 KB
/
integer.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import * as React from 'react';
import * as classNames from 'classnames';
import { TextInput } from '@patternfly/react-core';
import { getSequence, setNativeValue } from '../../../utils/utils';
import { isMinus, KEY_CODES, INPUT_NAVIGATION_KEYS } from '../../../constants/keys';
import './integer.scss';
const NON_NEGATIVE_INTEGER_KEYS = [
...INPUT_NAVIGATION_KEYS,
...getSequence(KEY_CODES[0], KEY_CODES[9]),
...getSequence(KEY_CODES.NUMPAD[0], KEY_CODES.NUMPAD[9]),
];
const INTEGER_KEYS = [
...NON_NEGATIVE_INTEGER_KEYS,
KEY_CODES.MINUS,
KEY_CODES.HYPHEN_MINUS,
KEY_CODES.NUMPAD.SUBTRACT,
];
const POSITIVE_INT_REGEX = /^[1-9]\d*$/;
const NON_NEGATIVE_INT_REGEX = /^(0|[1-9]\d*)$/;
const INT_REGEX = /^(-?0|-?[1-9]\d*)$/;
const PRECEEDING_ZEROS_POSITIVE_INT_REGEX = /^0*([1-9]\d*)$/;
const PRECEEDING_ZEROS_INT_REGEX = /^(-?)0*([1-9]\d*)$/;
const fixPrecedingZerosPositiveInt = (value) => {
const match = PRECEEDING_ZEROS_POSITIVE_INT_REGEX.exec(value);
return match && match.length === 2 ? match[1] : '';
};
const fixPrecedingZerosNonNegativeInt = (value) => {
const match = PRECEEDING_ZEROS_POSITIVE_INT_REGEX.exec(value);
return match && match.length === 2 ? match[1] : '0';
};
const fixInt = (value, oldValue, keyCode) => {
if (value.length === 0 && isMinus(keyCode)) {
if (oldValue) {
return oldValue < 0 || oldValue === '-0' ? oldValue : `-${oldValue}`;
}
return '-0';
}
const match = PRECEEDING_ZEROS_INT_REGEX.exec(value);
if (match && match.length === 3) {
if (match[1]) {
return `${match[1]}${match[2]}`;
}
return match[2];
}
return '0';
};
const isInputValid = (allowedKeys, keyCode, targetValue, additionalValidation) => {
if (allowedKeys.includes(keyCode)) {
return additionalValidation ? additionalValidation(keyCode, targetValue) : true;
}
return false;
};
export const Integer: React.FC<IntegerProps> = ({
id,
value,
isDisabled,
defaultValue,
onChange,
onBlur,
isPositive,
isNonNegative,
className,
isFullWidth,
isValid,
...restProps
}) => {
let allowedKeys;
let validRegex;
let fixAfterValue;
let min;
let additionalValidation;
if (isPositive) {
allowedKeys = NON_NEGATIVE_INTEGER_KEYS;
validRegex = POSITIVE_INT_REGEX;
fixAfterValue = fixPrecedingZerosPositiveInt;
min = 1;
additionalValidation = (keyCode, targetValue) =>
!(targetValue === '' && (keyCode === KEY_CODES[0] || keyCode === KEY_CODES.NUMPAD[0]));
} else if (isNonNegative) {
allowedKeys = NON_NEGATIVE_INTEGER_KEYS;
validRegex = NON_NEGATIVE_INT_REGEX;
fixAfterValue = fixPrecedingZerosNonNegativeInt;
min = 0;
} else {
allowedKeys = INTEGER_KEYS;
validRegex = INT_REGEX;
fixAfterValue = fixInt;
}
const onKeydown = (e) => {
const { keyCode, target } = e;
if (!isInputValid(allowedKeys, keyCode, target.value, additionalValidation)) {
e.preventDefault();
return false;
}
const oldValue = target.value;
window.setTimeout(() => {
if (!validRegex.test(target.value)) {
const v = fixAfterValue(target.value, oldValue, keyCode);
setNativeValue(target, v);
target.dispatchEvent(
new Event('input', {
bubbles: true,
cancelable: true,
}),
);
}
}, 0);
return true;
};
return (
<TextInput
{...restProps}
id={id}
type="number"
onKeyDown={onKeydown}
min={min}
value={value}
defaultValue={defaultValue}
onBlur={
typeof onBlur === 'function' ? (event) => onBlur(event.target.value || '', event) : null
}
onChange={onChange}
className={classNames(className, {
'kubevirt-integer-component': isFullWidth,
})}
isDisabled={isDisabled}
/>
);
};
type IntegerProps = {
id?: string;
isFullWidth?: boolean;
className?: string;
value?: string;
defaultValue?: string;
onChange?: (value: string, event: React.FormEvent<HTMLInputElement>) => void;
onBlur?: (value: string, event: React.FormEvent<HTMLInputElement>) => void;
isPositive?: boolean;
isNonNegative?: boolean; // is ignored when positive == true
isDisabled?: boolean;
isValid?: boolean;
'aria-label'?: string;
};