Browse Source

#2180 The e letter allowed to paste into number fields (#2508)

Co-authored-by: Roman Zabaluev <rzabaluev@provectus.com>
Co-authored-by: Oleg Shur <workshur@gmail.com>
Kris-K-Dev 2 years ago
parent
commit
91b86b5b78
1 changed files with 22 additions and 1 deletions
  1. 22 1
      kafka-ui-react-app/src/components/common/Input/Input.tsx

+ 22 - 1
kafka-ui-react-app/src/components/common/Input/Input.tsx

@@ -17,6 +17,7 @@ const Input: React.FC<InputProps> = ({
   hookFormOptions,
   search,
   inputSize = 'L',
+  type,
   ...rest
 }) => {
   const methods = useFormContext();
@@ -28,10 +29,30 @@ const Input: React.FC<InputProps> = ({
           inputSize={inputSize}
           {...methods.register(name, { ...hookFormOptions })}
           hasLeftIcon={!!search}
+          type={type}
           {...rest}
+          onKeyDown={(e) => {
+            if (type === 'number') {
+              if (e.key === 'e') {
+                e.preventDefault();
+              }
+            }
+          }}
+          onPaste={(e) => {
+            if (type === 'number') {
+              e.preventDefault();
+              const value = e.clipboardData.getData('Text');
+              methods.setValue(name, value.replace(/[^\d.]/g, ''));
+            }
+          }}
         />
       ) : (
-        <S.Input inputSize={inputSize} hasLeftIcon={!!search} {...rest} />
+        <S.Input
+          inputSize={inputSize}
+          hasLeftIcon={!!search}
+          type={type}
+          {...rest}
+        />
       )}
     </S.Wrapper>
   );