Skip to content

Commit ec7ffbe

Browse files
authored
[RN77][[Android] Inbox conversation header is not centered and cut (#8242)
* possible fix * update snapshot android * image snapshot update * android snapshot update
1 parent 9e64243 commit ec7ffbe

4 files changed

Lines changed: 83 additions & 12 deletions

File tree

android/src/main/java/com/reactnativenavigation/views/stack/topbar/titlebar/TitleAndButtonsMeasurer.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ typealias TitleBottom = Int
1717

1818
fun makeTitleAtMostWidthMeasureSpec(containerWidth: Int, rightBarWidth: Int, leftBarWidth: Int, isCenter: Boolean, isFill: Boolean = false): Int {
1919
return if (isCenter) {
20-
View.MeasureSpec.makeMeasureSpec(containerWidth, View.MeasureSpec.AT_MOST)
20+
val availableWidth = containerWidth - rightBarWidth - leftBarWidth
21+
View.MeasureSpec.makeMeasureSpec(availableWidth, View.MeasureSpec.AT_MOST)
2122
} else {
2223
val availableWidth = containerWidth - rightBarWidth - leftBarWidth - 2 * DEFAULT_LEFT_MARGIN_PX
2324
View.MeasureSpec.makeMeasureSpec(

android/src/main/java/com/reactnativenavigation/views/stack/topbar/titlebar/TitleBarReactView.kt

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,22 @@ package com.reactnativenavigation.views.stack.topbar.titlebar
33
import android.annotation.SuppressLint
44
import android.content.Context
55
import android.view.View
6-
import android.view.ViewGroup
7-
import androidx.core.view.children
8-
import com.facebook.react.ReactInstanceManager
96
import com.reactnativenavigation.react.ReactView
107

118
@SuppressLint("ViewConstructor")
129
class TitleBarReactView(context: Context?, componentId: String?,
1310
componentName: String?) : ReactView(context, componentId, componentName) {
1411
var centered: Boolean = false
12+
1513
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
16-
var titleHeightMeasureSpec: Int
17-
var titleWidthMeasureSpec: Int
1814
if (centered) {
19-
titleHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
20-
titleWidthMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
15+
val availableWidth = MeasureSpec.getSize(widthMeasureSpec)
16+
super.onMeasure(
17+
MeasureSpec.makeMeasureSpec(availableWidth, MeasureSpec.AT_MOST),
18+
heightMeasureSpec
19+
)
2120
} else {
22-
titleHeightMeasureSpec = heightMeasureSpec
23-
titleWidthMeasureSpec = widthMeasureSpec
21+
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
2422
}
25-
super.onMeasure(titleWidthMeasureSpec, titleHeightMeasureSpec)
2623
}
2724
}
-385 Bytes
Loading

playground/src/screens/TopBarTitleTestScreen.tsx

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { View, Text } from 'react-native';
2+
import { View, Text, StyleSheet } from 'react-native';
33
import { NavigationComponent, NavigationProps, Navigation as Nav } from 'react-native-navigation';
44
import Button from '../components/Button';
55
import Root from '../components/Root';
@@ -11,6 +11,8 @@ const {
1111
TOPBAR_TITLE_AVATAR,
1212
SET_TOPBAR_WITH_SUBTITLE_BTN,
1313
SET_TOPBAR_WITHOUT_SUBTITLE_BTN,
14+
SET_TOPBAR_CENTER_WITH_BUTTONS_BTN,
15+
SET_TOPBAR_FILL_WITH_BUTTONS_BTN,
1416
} = testIDs;
1517

1618
// TopBar title component WITH subtitle
@@ -53,8 +55,37 @@ function TopBarWithoutSubtitle() {
5355
);
5456
}
5557

58+
// Mimics an inbox conversation header: avatar + long name + status line.
59+
// With center alignment + right buttons, ellipsizeMode won't fire because
60+
// TitleBarReactView measures with UNSPECIFIED — the text renders at full
61+
// natural width and gets clipped behind the buttons.
62+
function InboxConversationHeader() {
63+
return (
64+
<View style={inboxStyles.row}>
65+
<View style={inboxStyles.avatar} />
66+
<View style={inboxStyles.textContainer}>
67+
<Text numberOfLines={1} ellipsizeMode="tail" style={inboxStyles.name}>
68+
John Jacob Jingleheimer Schmidt-Wolfeschlegelsteinhausen
69+
</Text>
70+
<Text numberOfLines={1} ellipsizeMode="tail" style={inboxStyles.status}>
71+
Online - Last message received 2 minutes ago via mobile
72+
</Text>
73+
</View>
74+
</View>
75+
);
76+
}
77+
78+
const inboxStyles = StyleSheet.create({
79+
row: { flexDirection: 'row', alignItems: 'center', flex: 1 },
80+
avatar: { width: 32, height: 32, borderRadius: 16, backgroundColor: '#4A90D9', marginRight: 8 },
81+
textContainer: { flex: 1, justifyContent: 'center' },
82+
name: { fontSize: 16, fontWeight: '600', color: '#000' },
83+
status: { fontSize: 12, color: '#888' },
84+
});
85+
5686
Nav.registerComponent('TopBarWithSubtitle', () => TopBarWithSubtitle);
5787
Nav.registerComponent('TopBarWithoutSubtitle', () => TopBarWithoutSubtitle);
88+
Nav.registerComponent('InboxConversationHeader', () => InboxConversationHeader);
5889

5990
interface Props extends NavigationProps { }
6091

@@ -90,6 +121,16 @@ export default class TopBarTitleTestScreen extends NavigationComponent<Props> {
90121
testID={SET_TOPBAR_WITHOUT_SUBTITLE_BTN}
91122
onPress={this.setTopBarWithoutSubtitle}
92123
/>
124+
<Button
125+
label="Center + Right Buttons (Cut-off Bug)"
126+
testID={SET_TOPBAR_CENTER_WITH_BUTTONS_BTN}
127+
onPress={this.setCenterWithRightButtons}
128+
/>
129+
<Button
130+
label="Fill + Right Buttons (Working)"
131+
testID={SET_TOPBAR_FILL_WITH_BUTTONS_BTN}
132+
onPress={this.setFillWithRightButtons}
133+
/>
93134
</Root>
94135
);
95136
}
@@ -117,5 +158,37 @@ export default class TopBarTitleTestScreen extends NavigationComponent<Props> {
117158
},
118159
},
119160
});
161+
162+
setCenterWithRightButtons = () =>
163+
Navigation.mergeOptions(this, {
164+
topBar: {
165+
rightButtons: [
166+
{ id: 'SEARCH', icon: require('../../img/clear.png') },
167+
{ id: 'MORE', icon: require('../../img/star.png') },
168+
],
169+
title: {
170+
component: {
171+
name: 'InboxConversationHeader',
172+
alignment: 'center',
173+
},
174+
},
175+
},
176+
});
177+
178+
setFillWithRightButtons = () =>
179+
Navigation.mergeOptions(this, {
180+
topBar: {
181+
rightButtons: [
182+
{ id: 'SEARCH', icon: require('../../img/clear.png') },
183+
{ id: 'MORE', icon: require('../../img/star.png') },
184+
],
185+
title: {
186+
component: {
187+
name: 'InboxConversationHeader',
188+
alignment: 'fill',
189+
},
190+
},
191+
},
192+
});
120193
}
121194

0 commit comments

Comments
 (0)