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

fix(component-rn): 修复Input、Text组件无法获取ref问题 (#15380) #15381

Open
wants to merge 3 commits into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 4 additions & 3 deletions packages/taro-components-rn/src/components/Input/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
} from 'react-native'
import { noop, omit, parseStyles, useUpdateEffect } from '../../utils'
import { InputProps } from './PropsType'
import { MutableRefObject } from 'react'

const keyboardTypeMap: { [key: string]: string } = {
text: 'default',
Expand All @@ -55,7 +56,7 @@ const defaultProps = {
selectionEnd: -1
}

const _Input = (props: InputProps) => {
const _Input = React.forwardRef((props: InputProps, ref: MutableRefObject<any>) => {
const {
style,
value,
Expand All @@ -81,7 +82,7 @@ const _Input = (props: InputProps) => {
const tmpValue = React.useRef<string>()
const [_height, setHeight] = React.useState(0)
const lineCount = React.useRef(0)
const inputRef = React.useRef<any>()
const inputRef = ref || React.useRef<any>()

React.useEffect(() => {
tmpValue.current = value
Expand Down Expand Up @@ -272,7 +273,7 @@ const _Input = (props: InputProps) => {
]}
/>
)
}
})

_Input.defaultProps = defaultProps as InputProps

Expand Down
19 changes: 11 additions & 8 deletions packages/taro-components-rn/src/components/Text/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@ import {
} from 'react-native'
import { TextProps } from './PropsType'

const _Text: React.FC<TextProps> = ({
style,
children,
selectable,
onClick,
...otherProps
}) => {
const _Text: React.FC<TextProps> = React.forwardRef((props: TextProps, ref: React.ForwardedRef<any>) => {
const {
style,
children,
selectable,
onClick,
...otherProps
} = props

return (
<Text
ref={ref}
selectable={!!selectable}
style={style}
onPress={onClick}
Expand All @@ -27,7 +30,7 @@ const _Text: React.FC<TextProps> = ({
{children}
</Text>
)
}
})

_Text.displayName = '_Text'

Expand Down