diff --git a/packages/@mantine/core/src/components/Input/Input.tsx b/packages/@mantine/core/src/components/Input/Input.tsx index 4a6efc5c139..0a178f9ed7f 100644 --- a/packages/@mantine/core/src/components/Input/Input.tsx +++ b/packages/@mantine/core/src/components/Input/Input.tsx @@ -1,3 +1,5 @@ +import { useRef } from 'react'; +import { useMergeRefs } from '@floating-ui/react'; import { Box, BoxProps, @@ -38,16 +40,16 @@ export type InputStylesNames = 'input' | 'wrapper' | 'section'; export type InputVariant = 'default' | 'filled' | 'unstyled'; export type InputCssVariables = { wrapper: - | '--input-height' - | '--input-fz' - | '--input-radius' - | '--input-left-section-width' - | '--input-right-section-width' - | '--input-left-section-pointer-events' - | '--input-right-section-pointer-events' - | '--input-padding-y' - | '--input-margin-top' - | '--input-margin-bottom'; + | '--input-height' + | '--input-fz' + | '--input-radius' + | '--input-left-section-width' + | '--input-right-section-width' + | '--input-left-section-pointer-events' + | '--input-right-section-pointer-events' + | '--input-padding-y' + | '--input-margin-top' + | '--input-margin-bottom'; }; export interface InputStylesCtx { @@ -206,6 +208,9 @@ export const Input = polymorphicFactory((_props, ref) => { const ctx = useInputWrapperContext(); const stylesCtx: InputStylesCtx = { offsetBottom: ctx?.offsetBottom, offsetTop: ctx?.offsetTop }; + const inputRef = useRef(null); + const inputMergedRef = useMergeRefs([ref, inputRef]); + const getStyles = useStyles({ name: ['Input', __staticSelector], props: __stylesApiProps || props, @@ -223,12 +228,12 @@ export const Input = polymorphicFactory((_props, ref) => { const ariaAttributes = withAria ? { - required, - disabled, - 'aria-invalid': !!error, - 'aria-describedby': ctx?.describedBy, - id: ctx?.inputId || id, - } + required, + disabled, + 'aria-invalid': !!error, + 'aria-describedby': ctx?.describedBy, + id: ctx?.inputId || id, + } : {}; return ( @@ -263,17 +268,24 @@ export const Input = polymorphicFactory((_props, ref) => { )} - + { + if (!inputRef?.current?.readOnly) return; + inputRef?.current?.focus(); + }}> + + {rightSection && (