react / react/react-native

`View` inside `ScrollView` makes scroll-to-top when multiline input grows

オープン
#48,412 コメント 7 件 リアクション 5 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Component: ScrollView Issue: Author Provided Repro Needs: Attention Newer Patch Available
主要言語
C++
スター
127k
フォーク
25.3k
平均マージ
1日 23時間
マージ済み PR(30日)
4

説明

Description

I use this component:

import React, { forwardRef } from "react";

import {
  ScrollView,
  ScrollViewProps,
  View,  
} from "react-native";

export type KeyboardAwareScrollViewProps = {
  /** The distance between keyboard and focused `TextInput` when keyboard is shown. Default is `0`. */
  bottomOffset?: number;
  /** Prevents automatic scrolling of the `ScrollView` when the keyboard gets hidden, maintaining the current screen position. Default is `false`. */
  disableScrollOnKeyboardHide?: boolean;
  /** Controls whether this `KeyboardAwareScrollView` instance should take effect. Default is `true` */
  enabled?: boolean;
  /** Adjusting the bottom spacing of KeyboardAwareScrollView. Default is `0` */
  extraKeyboardSpace?: number;
  /** Custom component for `ScrollView`. Default is `ScrollView` */
  ScrollViewComponent?: React.ComponentType<ScrollViewProps>;
} & ScrollViewProps;

const KeyboardAwareScrollView = forwardRef<
  ScrollView,
  React.PropsWithChildren<KeyboardAwareScrollViewProps>
>(
  (
    {
      children,
      ...rest
    },
    ref,
  ) => {

    return (
      <>
      <ScrollView {...rest} scrollEventThrottle={16}>
        {children}
        <View style={{height: 336, backgroundColor: 'red'}} />
      </ScrollView>
      
      </>
    );
  },
);

export default KeyboardAwareScrollView;

And I use it the code:

import React, { useCallback, useEffect, useRef, useState } from "react";
import { Button, Platform, Switch, Text, View, TextInput, StyleSheet } from "react-native";

export default function AwareScrollView() {
  const [_, setText] = useState("");

  return (
    <View style={styles.main}>
        <KeyboardAwareScrollView>
          <View style={styles.contentContainer}>
            <TextInput
              style={styles.contentText}
              multiline
              value={_}
              onChangeText={(value) => setText(value)}
              scrollEnabled={false}
            />
          </View>
        </KeyboardAwareScrollView>
      </View>
  );
}

export const styles = StyleSheet.create({
  safeAreaView: {
    flex: 1,
  },

  main: {
    flex: 1,
    marginBottom: 0,
  },

  contentContainer: {
    padding: 15,
    flexGrow: 1,
  },

  contentText: {
    width: "100%",
    backgroundColor: "#bcbcbc",
    flex: 1,
    fontSize: 16,
    minHeight: 60,
    textAlignVertical: "top",
  },
});
Steps to reproduce
  1. Type a long message in multiline input (you can simply press Enter/move to the next line to speed up the process)
  2. When TextInput gets obscured by keyboard scroll till the bottom to make sure the bottom of TextInput is visible
  3. Start to type a long message (you can use predictive text to speed up the process)
  4. When text goes to a new line ScrollView suddenly scrolls to top

Expected result is that ScrollView should remain its position.

React Native Version

0.76.2

Affected Platforms

Runtime - iOS

Output of npx react-native info
System:
  OS: macOS 15.1
  CPU: (10) arm64 Apple M1 Pro
  Memory: 118.81 MB / 32.00 GB
  Shell:
    version: "5.9"
    path: /bin/zsh
Binaries:
  Node:
    version: 20.18.0
    path: ~/.nvm/versions/node/v20.18.0/bin/node
  Yarn:
    version: 1.22.22
    path: ~/.nvm/versions/node/v20.18.0/bin/yarn
  npm:
    version: 10.8.2
    path: ~/.nvm/versions/node/v20.18.0/bin/npm
  Watchman:
    version: 2024.10.21.00
    path: /opt/homebrew/bin/watchman
Managers:
  CocoaPods:
    version: 1.14.3
    path: /usr/local/bin/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 24.1
      - iOS 18.1
      - macOS 15.1
      - tvOS 18.1
      - visionOS 2.1
      - watchOS 11.1
  Android SDK: Not Found
IDEs:
  Android Studio: 2024.2 AI-242.23339.11.2421.12483815
  Xcode:
    version: 16.1/16B40
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 17.0.9
    path: /usr/bin/javac
  Ruby:
    version: 2.7.6
    path: /Users/kirylziusko/.rbenv/shims/ruby
npmPackages:
  "@react-native-community/cli":
    installed: 15.0.1
    wanted: 15.0.1
  react:
    installed: 18.3.1
    wanted: 18.3.1
  react-native:
    installed: 0.76.2
    wanted: 0.76.2
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: false
iOS:
  hermesEnabled: true
  newArchEnabled: false
Stacktrace or Logs
I don't have any logs, bug from what I debugged in XCode methods like `scrollToOffset`/`scrollToEnd`/`scrollViewShouldScrollToTop` are not getting called.
Reproducer

https://github.com/kirillzyusko/react-native-keyboard-controller/blob/main/example/src/screens/Examples/AwareScrollView/index.tsx

Screenshots and Videos

https://github.com/user-attachments/assets/48078f8b-a1cc-42c2-8637-deeaa5a28346

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、React Native 0.76.2 を使用して iOS 上で example/src/screens/Examples/AwareScrollView/index.tsx のリンクされた再現コードを実行し、issue で説明されている ScrollView 内の複数行 TextInput を使用します。下部付近までスクロールした状態で入力が大きくなったときの ScrollView と TextInput の動作を調査します。TextInput が新しい行に達したときに ScrollView が現在の位置に留まれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
ios, react-native, typescript
領域
mobile
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
45/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。