Skip to content

Commit a3285d0

Browse files
authored
fix(android): FlatList with TextInput resize bug (#42)
* chore: update example * feat(android): implement `keyboardMode` prop * docs: add `keyboardMode` prop reference * chore: example * chore: bump deps
1 parent 90b3586 commit a3285d0

File tree

13 files changed

+133
-87
lines changed

13 files changed

+133
-87
lines changed

android/src/main/java/com/lodev09/truesheet/TrueSheetDialog.kt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,6 @@ class TrueSheetDialog(private val reactContext: ThemedReactContext, private val
6262

6363
// Setup window params to adjust layout based on Keyboard state
6464
window?.apply {
65-
// SOFT_INPUT_ADJUST_RESIZE to resize the sheet above the keyboard
66-
setSoftInputMode(
67-
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
68-
)
69-
7065
// Store current windowAnimation value to toggle later
7166
windowAnimation = attributes.windowAnimations
7267
}

android/src/main/java/com/lodev09/truesheet/TrueSheetView.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,12 @@ class TrueSheetView(context: Context) :
286286
}
287287
}
288288

289+
fun setSoftInputMode(mode: Int) {
290+
sheetDialog.window?.apply {
291+
this.setSoftInputMode(mode)
292+
}
293+
}
294+
289295
fun setDismissible(dismissible: Boolean) {
290296
sheetDialog.dismissible = dismissible
291297
}

android/src/main/java/com/lodev09/truesheet/TrueSheetViewManager.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.lodev09.truesheet
22

33
import android.util.Log
4+
import android.view.WindowManager
45
import com.facebook.react.bridge.ReadableArray
56
import com.facebook.react.bridge.ReadableType
67
import com.facebook.react.common.MapBuilder
@@ -56,6 +57,16 @@ class TrueSheetViewManager : ViewGroupManager<TrueSheetView>() {
5657
view.initialIndexAnimated = animate
5758
}
5859

60+
@ReactProp(name = "keyboardMode")
61+
fun setKeyboardMode(view: TrueSheetView, mode: String) {
62+
val softInputMode = when (mode) {
63+
"pan" -> WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN
64+
else -> WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
65+
}
66+
67+
view.setSoftInputMode(softInputMode)
68+
}
69+
5970
@ReactProp(name = "dimmedIndex")
6071
fun setDimmedIndex(view: TrueSheetView, index: Int) {
6172
view.setDimmedIndex(index)

docs/docs/reference/01-props.mdx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,15 @@ Used with `initialIndex`.
116116
| - | - | - | - |
117117
| `boolean` | `true` |||
118118

119+
### `keyboardMode`
120+
121+
Determines how the software keyboard will impact the layout of the sheet.
122+
Set to `pan` if you're working with `FlatList` with a `TextInput`.
123+
124+
| Type | Default | 🍎 | 🤖 |
125+
| - | - | - | - |
126+
| `"resize", "pan"` | `"resize"` | ||
127+
119128
### `grabber`
120129

121130
Shows a grabber (or handle). Native on IOS and styled `View` on Android.

example/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"build:ios": "cd ios && xcodebuild -workspace TrueSheetExample.xcworkspace -scheme TrueSheetExample -configuration Debug -sdk iphonesimulator CC=clang CPLUSPLUS=clang++ LD=clang LDPLUSPLUS=clang++ GCC_OPTIMIZATION_LEVEL=0 GCC_PRECOMPILE_PREFIX_HEADER=YES ASSETCATALOG_COMPILER_OPTIMIZATION=time DEBUG_INFORMATION_FORMAT=dwarf COMPILER_INDEX_STORE_ENABLE=NO"
1414
},
1515
"dependencies": {
16-
"expo": "~51.0.11",
16+
"expo": "~51.0.14",
1717
"expo-build-properties": "~0.12.3",
1818
"react": "18.2.0",
1919
"react-native": "0.74.2",

example/src/App.tsx

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,6 @@ export default function App() {
3535
userInterfaceStyle="dark"
3636
/>
3737

38-
{/* <Button
39-
style={{ position: 'absolute', top: 0, left: 0, right: 0 }}
40-
text="Present"
41-
onPress={() => sheetRef.current?.present(1)}
42-
/> */}
43-
4438
<TrueSheet
4539
sizes={['15%', 'auto', 'large']}
4640
ref={sheetRef}
@@ -70,7 +64,6 @@ export default function App() {
7064
<Spacer />
7165
<Button text="Expand" onPress={() => sheetRef.current?.resize(2)} />
7266
<Button text="Collapse" onPress={() => sheetRef.current?.resize(1)} />
73-
{/* <Button text="Dismiss" onPress={() => sheetRef.current?.dismiss()} /> */}
7467

7568
<BasicSheet ref={basicSheet} />
7669
<PromptSheet ref={promptSheet} />

example/src/components/Input.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React from 'react'
2+
import { TextInput, View, type TextStyle, type ViewStyle } from 'react-native'
3+
4+
import { BORDER_RADIUS, GRAY, INPUT_HEIGHT, SPACING } from '../utils'
5+
6+
export const Input = () => {
7+
return (
8+
<View style={$inputContainer}>
9+
<TextInput style={$input} placeholder="Enter some text..." placeholderTextColor={GRAY} />
10+
</View>
11+
)
12+
}
13+
14+
const $inputContainer: ViewStyle = {
15+
backgroundColor: 'white',
16+
paddingHorizontal: SPACING,
17+
height: INPUT_HEIGHT,
18+
borderRadius: BORDER_RADIUS,
19+
justifyContent: 'center',
20+
marginBottom: SPACING,
21+
}
22+
23+
const $input: TextStyle = {
24+
fontSize: 16,
25+
height: INPUT_HEIGHT,
26+
}

example/src/components/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export * from './Footer'
22
export * from './DemoContent'
33
export * from './Button'
44
export * from './Spacer'
5+
export * from './Input'

example/src/sheets/FlatListSheet.tsx

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import React, { forwardRef, useRef, type Ref } from 'react'
2-
import { FlatList, type ViewStyle } from 'react-native'
2+
import { FlatList, View, type ViewStyle } from 'react-native'
33
import { TrueSheet, type TrueSheetProps } from '@lodev09/react-native-true-sheet'
44

5-
import { SPACING, times } from '../utils'
6-
import { DemoContent } from '../components'
5+
import { DARK, DARK_GRAY, INPUT_HEIGHT, SPACING, times } from '../utils'
6+
import { DemoContent, Input } from '../components'
77

88
interface FlatListSheetProps extends TrueSheetProps {}
99

@@ -14,21 +14,24 @@ export const FlatListSheet = forwardRef((props: FlatListSheetProps, ref: Ref<Tru
1414
<TrueSheet
1515
ref={ref}
1616
scrollRef={flatListRef}
17-
sizes={['large']}
18-
cornerRadius={24}
19-
grabber={false}
20-
maxHeight={600}
17+
sizes={['medium', 'large']}
18+
blurTint="dark"
19+
backgroundColor={DARK}
20+
keyboardMode="pan"
2121
onDismiss={() => console.log('Sheet FlatList dismissed!')}
2222
onPresent={() => console.log(`Sheet FlatList presented!`)}
2323
{...props}
2424
>
25+
<View style={$header}>
26+
<Input />
27+
</View>
2528
<FlatList<number>
2629
ref={flatListRef}
2730
nestedScrollEnabled
2831
data={times(50, (i) => i)}
2932
contentContainerStyle={$content}
3033
indicatorStyle="black"
31-
renderItem={() => <DemoContent radius={24} />}
34+
renderItem={() => <DemoContent color={DARK_GRAY} />}
3235
/>
3336
</TrueSheet>
3437
)
@@ -38,4 +41,16 @@ FlatListSheet.displayName = 'FlatListSheet'
3841

3942
const $content: ViewStyle = {
4043
padding: SPACING,
44+
paddingTop: INPUT_HEIGHT + SPACING * 4,
45+
}
46+
47+
const $header: ViewStyle = {
48+
position: 'absolute',
49+
left: 0,
50+
right: 0,
51+
top: 0,
52+
backgroundColor: DARK,
53+
paddingTop: SPACING * 2,
54+
paddingHorizontal: SPACING,
55+
zIndex: 1,
4156
}

example/src/sheets/PromptSheet.tsx

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
import React, { forwardRef, useRef, type Ref, useImperativeHandle, useState } from 'react'
2-
import { TextInput, View, type TextStyle, type ViewStyle } from 'react-native'
2+
import { type ViewStyle } from 'react-native'
33
import { TrueSheet, type TrueSheetProps } from '@lodev09/react-native-true-sheet'
44

5-
import {
6-
BORDER_RADIUS,
7-
DARK,
8-
DARK_BLUE,
9-
GRABBER_COLOR,
10-
GRAY,
11-
INPUT_HEIGHT,
12-
SPACING,
13-
} from '../utils'
14-
import { Button, DemoContent, Footer } from '../components'
5+
import { DARK, DARK_BLUE, GRABBER_COLOR, SPACING } from '../utils'
6+
import { Button, DemoContent, Footer, Input } from '../components'
157

168
interface PromptSheetProps extends TrueSheetProps {}
179

@@ -67,28 +59,6 @@ export const PromptSheet = forwardRef((props: PromptSheetProps, ref: Ref<TrueShe
6759

6860
PromptSheet.displayName = 'PromptSheet'
6961

70-
const Input = () => {
71-
return (
72-
<View style={$inputContainer}>
73-
<TextInput style={$input} placeholder="Enter some text..." placeholderTextColor={GRAY} />
74-
</View>
75-
)
76-
}
77-
7862
const $content: ViewStyle = {
7963
padding: SPACING,
8064
}
81-
82-
const $inputContainer: ViewStyle = {
83-
backgroundColor: 'white',
84-
paddingHorizontal: SPACING,
85-
height: INPUT_HEIGHT,
86-
borderRadius: BORDER_RADIUS,
87-
justifyContent: 'center',
88-
marginBottom: SPACING,
89-
}
90-
91-
const $input: TextStyle = {
92-
fontSize: 16,
93-
height: SPACING * 3,
94-
}

src/TrueSheet.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ export class TrueSheet extends PureComponent<TrueSheetProps, TrueSheetState> {
209209
grabber = true,
210210
dimmed = true,
211211
initialIndexAnimated = true,
212+
keyboardMode = 'resize',
212213
initialIndex,
213214
dimmedIndex,
214215
grabberProps,
@@ -237,6 +238,7 @@ export class TrueSheet extends PureComponent<TrueSheetProps, TrueSheetState> {
237238
dimmedIndex={dimmedIndex}
238239
initialIndex={initialIndex}
239240
initialIndexAnimated={initialIndexAnimated}
241+
keyboardMode={keyboardMode}
240242
dismissible={dismissible}
241243
maxHeight={maxHeight}
242244
onMount={this.onMount}

src/TrueSheet.types.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,15 @@ export interface TrueSheetProps extends ViewProps {
221221
*/
222222
FooterComponent?: ComponentType<unknown> | ReactElement
223223

224+
/**
225+
* Determines how the software keyboard will impact the layout of the sheet.
226+
* Set to `pan` if you're working with `FlatList` with a `TextInput`.
227+
*
228+
* @platform android
229+
* @default resize
230+
*/
231+
keyboardMode?: 'resize' | 'pan'
232+
224233
/**
225234
* This is called when the sheet is ready to present.
226235
*/

0 commit comments

Comments
 (0)