Skip to content

TextInPut MaxLength #32732

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

Closed
peng-oss opened this issue Dec 10, 2021 · 5 comments
Closed

TextInPut MaxLength #32732

peng-oss opened this issue Dec 10, 2021 · 5 comments
Labels
Component: TextInput Related to the TextInput component. Needs: Triage 🔍 Stale There has been a lack of activity on this issue and it may be closed soon.

Comments

@peng-oss
Copy link

Description

for example :

MaxLength is 200

199 Text + 1 😈 emoji textValue clear

Version

"react-native": "0.65.1",

Output of react-native info

o

Steps to reproduce

0

Snack, code example, screenshot, or link to a repository

No response

@github-actions github-actions bot added the Version: unspecified No version information could be extracted label Dec 10, 2021
@react-native-bot react-native-bot added the Component: TextInput Related to the TextInput component. label Dec 10, 2021
@shahanshah87
Copy link

Hey, did you find any solution?

@peng-oss
Copy link
Author

Hey, did you find any solution?

no,it's react-native bug

@gitBingxu
Copy link

gitBingxu commented Feb 27, 2023

This issue may have been resolved in v 0.66.0.

My solution in react-native@v0.63:

import React, { forwardRef, memo, useRef } from 'react'
import { TextInput, TextInputProps } from 'react-native'
import { useMemoizedFn } from 'ahooks'

export interface MaxLenInputProps extends Omit<TextInputProps, 'maxLength' | 'onChange'> {
    /*
     * 允许输入内容的最大长度,必填
     */
    maxLength: number
}
const MaxLenInputCompo = forwardRef<TextInput, MaxLenInputProps>((props, ref) => {
    const {
        value, onKeyPress, onChangeText, maxLength
    } = props

    const notChangeRef = useRef(false)

    const originalOnKeyPress = useMemoizedFn((...args: Parameters<typeof onKeyPress>) => {
        const [event] = args
        const { nativeEvent: { key } } = event
        const valueLen = value?.length || 0
        const keyLen = key.length

        const isEmoji = keyLen > 1 && !['Backspace', 'Enter'].includes(key)
        if (isEmoji) {
            const hasEnoughLen = maxLength - valueLen >= keyLen
            if (!hasEnoughLen) {
                // 如果输入的字符是 emoji,并且没有足够的长度容纳该字符,那么我们需要舍弃本次输入的字符。
                notChangeRef.current = true
            }
        }

        onKeyPress?.(...args)
    })

    const originalOnChangeText = useMemoizedFn((...args: Parameters<typeof onChangeText>) => {
        const [text] = args
        const emitValue = notChangeRef.current ? value : text

        onChangeText?.apply?.(null, [emitValue, ...args.slice(1)])
        notChangeRef.current = false
    })

    return (
        <TextInput
            ref={ref}
            {...props}
            value={value}
            maxLength={maxLength}
            onKeyPress={originalOnKeyPress}
            onChangeText={originalOnChangeText} />
    )
})

export const MaxLenInput = memo(MaxLenInputCompo)

@peng-oss @shahanshah87

@cortinico cortinico removed the Version: unspecified No version information could be extracted label Dec 4, 2023
Copy link

github-actions bot commented Jun 2, 2024

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@github-actions github-actions bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Jun 2, 2024
Copy link

This issue was closed because it has been stalled for 7 days with no activity.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: TextInput Related to the TextInput component. Needs: Triage 🔍 Stale There has been a lack of activity on this issue and it may be closed soon.
Projects
None yet
Development

No branches or pull requests

5 participants