LoginSignup
1
1

More than 5 years have passed since last update.

TextInputのフォーカスが外れると確定前の文字列が消える(iOS)

Last updated at Posted at 2016-11-11

症状

  • 日本語等でTextInputに入力途中、「確定」を押さずに適当な場所をタップすることでフォーカスを外すと、確定前だった文字列が表示上から消える
  • しかし、onChangeは呼ばれない
    • そのため、onChangeでtextを取得してstateにsetしている場合、消える前の(=正しい)文字列をstateに持った状態となる

closeされてますが完全に一致:
https://github.com/facebook/react-native/issues/4456

日本語キーボード以外だと問題にならないので放置されてきた感

原因

原因というか症状を掘り下げただけですが:

  • フォーカスを外した直後、ネイティブのUITextFieldの値が確定前の文字列に戻ってしまう
  • これはUIControlEventEditingChangedを介さずに呼ばれており、そのためReactNative側にも伝わらない

対処法

https://github.com/facebook/react-native/blob/master/Libraries/Components/TextInput/TextInput.js#L778

from:

if (this._lastNativeText !== this.props.value && typeof this.props.value === 'string') {
  nativeProps.text = this.props.value;
}

to:

if (typeof this.props.value === 'string') {
  nativeProps.text = this.props.value;
}

「表示上の値(this._lastNativeText)が自身の値と一致していれば上書き処理をしない」というエコさを取り払う悪手ですが暫定的に..

※ 書き換えられたlastNativeTextを取得できていないので、元の条件だと書き込みが実行されません

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1