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

feat(input): prevent input of [eE] characters #1422

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions .changeset/fair-books-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@alfalab/core-components-input': minor
---

Запрещен ввод и вставка символов `[eE]` в input[type=number]
40 changes: 38 additions & 2 deletions packages/input/src/components/base-input/Component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import React, {
Fragment,
HTMLAttributes,
InputHTMLAttributes,
KeyboardEvent,
MouseEvent,
ReactNode,
RefAttributes,
Expand Down Expand Up @@ -266,6 +267,7 @@ export const BaseInput = React.forwardRef<HTMLInputElement, BaseInputProps>(
},
ref,
) => {
const { onKeyDown } = restProps;
const uncontrolled = value === undefined;
const readOnly = readOnlyProp || disableUserInput;

Expand Down Expand Up @@ -309,17 +311,50 @@ export const BaseInput = React.forwardRef<HTMLInputElement, BaseInputProps>(

const handleInputChange = useCallback(
(event: React.ChangeEvent<HTMLInputElement>) => {
let inputValue = event.target.value;
const target = event.target as HTMLInputElement;
const inInputTypeNumber = target.getAttribute('type') === 'number';
fulcanellee marked this conversation as resolved.
Show resolved Hide resolved
const pattern = /[eE]/g;

if (inInputTypeNumber && pattern.test(inputValue)) {
inputValue = inputValue.replace(pattern, '');
}

if (onChange) {
onChange(event, { value: event.target.value });
onChange(event, { value: inputValue });
}

if (uncontrolled) {
setStateValue(event.target.value);
setStateValue(inputValue);
}
},
[onChange, uncontrolled],
);

const handleKeyDown = useCallback(
(event: KeyboardEvent<HTMLInputElement>) => {
/**
* По умолчанию в input[type=number] можно вводить числа типа 2e5 (200 000)
* Это ломает некоторое поведение, поэтому запрещаем ввод символов [eE]
* @see DS-6808
*/
const { key, target } = event;
const eventTarget = target as HTMLInputElement;
const inInputTypeNumber = eventTarget.getAttribute('type') === 'number';

if (inInputTypeNumber && (key === 'e' || key === 'E')) {
event.preventDefault();

return;
}

if (onKeyDown) {
onKeyDown(event);
}
},
[onKeyDown],
);

const handleClear = useCallback(
(event: MouseEvent<HTMLButtonElement>) => {
if (!clearButtonVisible) return;
Expand Down Expand Up @@ -436,6 +471,7 @@ export const BaseInput = React.forwardRef<HTMLInputElement, BaseInputProps>(
onBlur={handleInputBlur}
onFocus={handleInputFocus}
onChange={handleInputChange}
onKeyDown={handleKeyDown}
onAnimationStart={handleAnimationStart}
ref={mergeRefs([ref, inputRef])}
type={type}
Expand Down
Loading