Skip to content

Commit

Permalink
fix(input): fix set selection range (#1488)
Browse files Browse the repository at this point in the history
* fix(input): fix set selection range

* feat(input): refactor use effect

---------

Co-authored-by: Aleksandr Dyuzhikov <[email protected]>
  • Loading branch information
reabiliti and Aleksandr Dyuzhikov authored Dec 13, 2024
1 parent 455a24d commit 8987193
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/weak-poems-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@alfalab/core-components-input': patch
---

Фикс вызова input.setSelectionRange
21 changes: 14 additions & 7 deletions packages/input/src/components/base-input/Component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ const SIZE_TO_CLASSNAME_MAP = {
72: 'size-72',
};

const inputTypesForSelectionRange = ['password', 'search', 'tel', 'text', 'url'];

export const BaseInput = React.forwardRef<HTMLInputElement, BaseInputProps>(
(
{
Expand Down Expand Up @@ -284,14 +286,19 @@ export const BaseInput = React.forwardRef<HTMLInputElement, BaseInputProps>(
const hasInnerLabel = label && labelView === 'inner';

useLayoutEffect_SAFE_FOR_SSR(() => {
// https://github.com/facebook/react/issues/14125
if (restProps.autoFocus) {
const input = inputRef.current;

if (input) {
input.setSelectionRange(input.value.length, input.value.length);
}
const input = inputRef.current;

if (
!restProps.autoFocus ||
!input ||
// https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/setSelectionRange
!inputTypesForSelectionRange.includes(input.type)
) {
return;
}

// https://github.com/facebook/react/issues/14125
input.setSelectionRange(input.value.length, input.value.length);
}, []);

const handleInputFocus = useCallback(
Expand Down

0 comments on commit 8987193

Please sign in to comment.