Skip to content

Commit cf063a5

Browse files
authored
Merge pull request #2422 from dos1in/master
fix(input): 修复 android 下光标位置错误的问题
2 parents 0fcee81 + e288e65 commit cf063a5

File tree

1 file changed

+11
-2
lines changed
  • packages/webpack-plugin/lib/runtime/components/react

1 file changed

+11
-2
lines changed

packages/webpack-plugin/lib/runtime/components/react/mpx-input.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ import {
5454
NativeTouchEvent
5555
} from 'react-native'
5656
import { warn } from '@mpxjs/utils'
57-
import { useUpdateEffect, useTransformStyle, useLayout, extendObject, isIOS } from './utils'
57+
import { useUpdateEffect, useTransformStyle, useLayout, extendObject, isAndroid } from './utils'
5858
import useInnerProps, { getCustomEvent } from './getInnerListeners'
5959
import useNodesRef, { HandlerRef } from './useNodesRef'
6060
import { FormContext, FormFieldValue, KeyboardAvoidContext } from './context'
@@ -188,6 +188,8 @@ const Input = forwardRef<HandlerRef<TextInput, FinalInputProps>, FinalInputProps
188188
}
189189

190190
const defaultValue = parseValue(value)
191+
// 微信小程序的 input 永远是单行,textAlignVertical 固定为 auto
192+
// multiline 为 true 时表示是 textarea 组件复用此逻辑
191193
const textAlignVertical = multiline ? 'top' : 'auto'
192194
const isAutoFocus = !!autoFocus || !!focus
193195

@@ -467,6 +469,12 @@ const Input = forwardRef<HandlerRef<TextInput, FinalInputProps>, FinalInputProps
467469
: (nodeRef.current as TextInput)?.blur()
468470
}, [isAutoFocus])
469471

472+
// 使用 multiline 来修复光标位置问题
473+
// React Native 的 TextInput 在 textAlign center + placeholder 时光标会跑到右边
474+
// 这个问题只在 Android 上出现
475+
// 参考:https://github.com/facebook/react-native/issues/28794 (Android only)
476+
const needMultilineFix = isAndroid && !multiline
477+
470478
const innerProps = useInnerProps(
471479
extendObject(
472480
{},
@@ -490,7 +498,7 @@ const Input = forwardRef<HandlerRef<TextInput, FinalInputProps>, FinalInputProps
490498
underlineColorAndroid: 'rgba(0,0,0,0)',
491499
textAlignVertical: textAlignVertical,
492500
placeholderTextColor: placeholderStyle?.color,
493-
multiline: !!multiline,
501+
multiline: multiline || needMultilineFix,
494502
onTouchStart,
495503
onTouchEnd,
496504
onFocus,
@@ -500,6 +508,7 @@ const Input = forwardRef<HandlerRef<TextInput, FinalInputProps>, FinalInputProps
500508
onContentSizeChange,
501509
onSubmitEditing: bindconfirm && onSubmitEditing
502510
},
511+
needMultilineFix ? { numberOfLines: 1 } : {},
503512
!!multiline && confirmType === 'return' ? {} : { enterKeyHint: confirmType }
504513
),
505514
[

0 commit comments

Comments
 (0)