Skip to content

Commit

Permalink
fix(input): set caret position at the end of the input programmatical…
Browse files Browse the repository at this point in the history
…ly (#1413)

* fix(input): set caret position at the end of the input programmatically

* Create tame-masks-pump.md
  • Loading branch information
reme3d2y authored Nov 8, 2024
1 parent d9e00c7 commit 7161c35
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/tame-masks-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@alfalab/core-components-input": patch
---

Исправлен баг с автофокусом и SSR. Каретка при автофокусе теперь программно устанавливается в конец ввода.
13 changes: 12 additions & 1 deletion packages/input/src/components/base-input/Component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import cn from 'classnames';
import { FormControlProps } from '@alfalab/core-components-form-control';
import { getDataTestId } from '@alfalab/core-components-shared';
import { StatusBadge } from '@alfalab/core-components-status-badge';
import { useFocus } from '@alfalab/hooks';
import { useFocus, useLayoutEffect_SAFE_FOR_SSR } from '@alfalab/hooks';

import { ClearButton } from '../clear-button';

Expand Down Expand Up @@ -283,6 +283,17 @@ export const BaseInput = React.forwardRef<HTMLInputElement, BaseInputProps>(
const clearButtonVisible = clear && filled && !disabled && !readOnlyProp;
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 handleInputFocus = useCallback(
(event: React.FocusEvent<HTMLInputElement>) => {
if (!readOnlyProp || disableUserInput) {
Expand Down

0 comments on commit 7161c35

Please sign in to comment.