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 1 commit
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
2 changes: 1 addition & 1 deletion .changeset/fair-books-smash.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
'@alfalab/core-components-input': minor
---

Запрещен ввод символов `[eE]` в input[type=number]
Запрещен ввод и вставка символов `[eE]` в input[type=number]
19 changes: 15 additions & 4 deletions packages/input/src/components/base-input/Component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -311,12 +311,21 @@ 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],
Expand All @@ -329,9 +338,11 @@ export const BaseInput = React.forwardRef<HTMLInputElement, BaseInputProps>(
* Это ломает некоторое поведение, поэтому запрещаем ввод символов [eE]
* @see DS-6808
*/
const { key } = event;
const { key, target } = event;
const eventTarget = target as HTMLInputElement;
const inInputTypeNumber = eventTarget.getAttribute('type') === 'number';

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

return;
Expand Down
Loading