Skip to content

Commit 63cd29a

Browse files
author
Jan Romaniak
committed
fixed problem with messages overflow, fixed problem with send render
1 parent 5d1cea0 commit 63cd29a

File tree

7 files changed

+819
-176
lines changed

7 files changed

+819
-176
lines changed

example/src/App.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ messages.push(generateMessage(`Idylla 2`, 3, {image:'https://www.wykop.pl/cdn/c
99
messages.push(generateMessage(`Goood 1`, 2, {image:'http://img2.dmty.pl//uploads/201010/1286036107_by_julia2332_600.jpg'}));
1010
messages.push(generateMessage(`This is a great example of system message`, 2, {system: true}));
1111

12-
for (let i = 0; i < 20; i++) {
12+
for (let i = 0; i < 30; i++) {
1313
messages.push(generateMessage(loremIpsum.substring(0,(Math.random() * 100000)%loremIpsum.length), i))
1414
}
1515

@@ -67,20 +67,23 @@ class App extends Component {
6767
}
6868
const styles = {
6969
container: {
70-
display:'flex',
71-
flexDirection: 'row',
72-
height: '100vh'
70+
flex: 1,
71+
display: "flex",
72+
flexDirection: "row",
73+
height: "100vh",
7374
},
7475
conversationList: {
7576
display:'flex',
7677
flex: 1,
7778
},
7879
chat: {
79-
display:'flex',
80+
display: "flex",
8081
flex: 3,
81-
borderWidth: '1px',
82-
borderColor: '#ccc',
83-
borderStyle: 'solid'
82+
flexDirection: "column",
83+
borderWidth: "1px",
84+
borderColor: "#ccc",
85+
borderRightStyle: "solid",
86+
borderLeftStyle: "solid",
8487
},
8588
converationDetails: {
8689
display:'flex',

src/Composer.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,16 @@ import { MIN_COMPOSER_HEIGHT, DEFAULT_PLACEHOLDER } from './Constant';
88
import Color from './Color';
99

1010
export default class Composer extends React.Component {
11-
1211
onContentSizeChange(e) {
1312
const { contentSize } = e.nativeEvent;
1413

1514
// Support earlier versions of React Native on Android.
1615
if (!contentSize) return;
1716

1817
if (
19-
!this.contentSize ||
20-
this.contentSize.width !== contentSize.width ||
21-
this.contentSize.height !== contentSize.height
18+
!this.contentSize
19+
|| this.contentSize.width !== contentSize.width
20+
|| this.contentSize.height !== contentSize.height
2221
) {
2322
this.contentSize = contentSize;
2423
this.props.onInputSizeChanged(this.contentSize);
@@ -38,10 +37,10 @@ export default class Composer extends React.Component {
3837
placeholder={this.props.placeholder}
3938
placeholderTextColor={this.props.placeholderTextColor}
4039
multiline={this.props.multiline}
41-
onChange={(e) => this.onContentSizeChange(e)}
42-
onContentSizeChange={(e) => this.onContentSizeChange(e)}
43-
onChangeText={(text) => this.onChangeText(text)}
44-
style={[styles.textInput, this.props.textInputStyle, { height: this.props.composerHeight }]}
40+
onChange={e => this.onContentSizeChange(e)}
41+
onContentSizeChange={e => this.onContentSizeChange(e)}
42+
onChangeText={text => this.onChangeText(text)}
43+
style={[styles.textInput, this.props.textInputStyle, { height: this.props.composerHeight - 1 }]}
4544
autoFocus={this.props.textInputAutoFocus}
4645
value={this.props.text}
4746
enablesReturnKeyAutomatically
@@ -51,18 +50,16 @@ export default class Composer extends React.Component {
5150
/>
5251
);
5352
}
54-
5553
}
5654

5755
const styles = StyleSheet.create({
5856
textInput: {
5957
flex: 1,
60-
minHeight: 40,
6158
marginLeft: 10,
6259
fontSize: 16,
6360
lineHeight: 16,
64-
marginTop: 3,
65-
marginBottom: 3,
61+
paddingTop: 3,
62+
paddingBottom: 3,
6663
outline: 'none',
6764
},
6865
});

src/Constant.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export const MIN_COMPOSER_HEIGHT = 41;
2-
export const MAX_COMPOSER_HEIGHT = 100;
2+
export const MAX_COMPOSER_HEIGHT = 41;
33
export const DEFAULT_PLACEHOLDER = 'Type a message...';
44
export const DATE_FORMAT = 'll';
55
export const TIME_FORMAT = 'LT';

src/GiftedChat.js

Lines changed: 15 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ import {
3939
} from './Constant';
4040

4141
class GiftedChat extends React.Component {
42-
4342
constructor(props) {
4443
super(props);
4544

@@ -54,15 +53,10 @@ class GiftedChat extends React.Component {
5453

5554
this.state = {
5655
isInitialized: false, // initialization will calculate maxHeight before rendering the chat
57-
composerHeight: this.props.minComposerHeight,
58-
messagesContainerHeight: null,
56+
composerHeight: 40,
5957
typingDisabled: false,
6058
};
6159

62-
this.onKeyboardWillShow = this.onKeyboardWillShow.bind(this);
63-
this.onKeyboardWillHide = this.onKeyboardWillHide.bind(this);
64-
this.onKeyboardDidShow = this.onKeyboardDidShow.bind(this);
65-
this.onKeyboardDidHide = this.onKeyboardDidHide.bind(this);
6660
this.onSend = this.onSend.bind(this);
6761
this.getLocale = this.getLocale.bind(this);
6862
this.onInputSizeChanged = this.onInputSizeChanged.bind(this);
@@ -72,11 +66,6 @@ class GiftedChat extends React.Component {
7266

7367
this.invertibleScrollViewProps = {
7468
inverted: this.props.inverted,
75-
keyboardShouldPersistTaps: this.props.keyboardShouldPersistTaps,
76-
onKeyboardWillShow: this.onKeyboardWillShow,
77-
onKeyboardWillHide: this.onKeyboardWillHide,
78-
onKeyboardDidShow: this.onKeyboardDidShow,
79-
onKeyboardDidHide: this.onKeyboardDidHide,
8069
};
8170
}
8271

@@ -156,23 +145,6 @@ class GiftedChat extends React.Component {
156145
return this._messages;
157146
}
158147

159-
setMaxHeight(height) {
160-
this._maxHeight = height;
161-
}
162-
163-
getMaxHeight() {
164-
return this._maxHeight;
165-
}
166-
167-
setKeyboardHeight(height) {
168-
this._keyboardHeight = 0;
169-
}
170-
171-
getKeyboardHeight() {
172-
return 0;
173-
}
174-
175-
176148
setBottomOffset(value) {
177149
this._bottomOffset = value;
178150
}
@@ -207,82 +179,9 @@ class GiftedChat extends React.Component {
207179
return this._isMounted;
208180
}
209181

210-
// TODO: setMinInputToolbarHeight
211-
getMinInputToolbarHeight() {
212-
return this.props.renderAccessory
213-
? this.props.minInputToolbarHeight * 2
214-
: this.props.minInputToolbarHeight;
215-
}
216-
calculateInputToolbarHeight(composerHeight) {
217-
return composerHeight + (this.getMinInputToolbarHeight() - this.props.minComposerHeight);
218-
}
219-
220-
/**
221-
* Returns the height, based on current window size, without taking the keyboard into account.
222-
*/
223-
getBasicMessagesContainerHeight(composerHeight = this.state.composerHeight) {
224-
return this.getMaxHeight() - this.calculateInputToolbarHeight(composerHeight);
225-
}
226-
227-
/**
228-
* Returns the height, based on current window size, taking the keyboard into account.
229-
*/
230-
getMessagesContainerHeightWithKeyboard(composerHeight = this.state.composerHeight) {
231-
return this.getBasicMessagesContainerHeight(composerHeight) - this.getKeyboardHeight() + this.getBottomOffset();
232-
}
233-
234-
prepareMessagesContainerHeight(value) {
235-
if (this.props.isAnimated === true) {
236-
return new Animated.Value(value);
237-
}
238-
return value;
239-
}
240-
241-
onKeyboardWillShow(e) {
242-
this.setIsTypingDisabled(true);
243-
this.setKeyboardHeight(e.endCoordinates ? e.endCoordinates.height : e.end.height);
244-
this.setBottomOffset(this.props.bottomOffset);
245-
const newMessagesContainerHeight = this.getMessagesContainerHeightWithKeyboard();
246-
if (this.props.isAnimated === true) {
247-
Animated.timing(this.state.messagesContainerHeight, {
248-
toValue: newMessagesContainerHeight,
249-
duration: 210,
250-
}).start();
251-
} else {
252-
this.setState({
253-
messagesContainerHeight: newMessagesContainerHeight,
254-
});
255-
}
256-
}
257-
258-
onKeyboardWillHide() {
259-
this.setIsTypingDisabled(true);
260-
this.setKeyboardHeight(0);
261-
this.setBottomOffset(0);
262-
const newMessagesContainerHeight = this.getBasicMessagesContainerHeight();
263-
if (this.props.isAnimated === true) {
264-
Animated.timing(this.state.messagesContainerHeight, {
265-
toValue: newMessagesContainerHeight,
266-
duration: 210,
267-
}).start();
268-
} else {
269-
this.setState({
270-
messagesContainerHeight: newMessagesContainerHeight,
271-
});
272-
}
273-
}
274-
275-
onKeyboardDidShow(e) {
276-
this.setIsTypingDisabled(false);
277-
}
278-
279-
onKeyboardDidHide(e) {
280-
this.setIsTypingDisabled(false);
281-
}
282-
283182
scrollToBottom(animated = true) {
284183
if (this._messageContainerRef === null) {
285-
return
184+
return;
286185
}
287186
this._messageContainerRef.scrollTo({
288187
y: 0,
@@ -294,9 +193,10 @@ class GiftedChat extends React.Component {
294193
renderMessages() {
295194
return (
296195
<div style={{
297-
height: this.state.messagesContainerHeight,
298-
display: 'flex'
299-
}}>
196+
height: `calc(100% - ${this.state.composerHeight}px)`,
197+
display: 'flex',
198+
}}
199+
>
300200
<MessageContainer
301201
{...this.props}
302202
invertibleScrollViewProps={this.invertibleScrollViewProps}
@@ -312,14 +212,12 @@ class GiftedChat extends React.Component {
312212
if (!Array.isArray(messages)) {
313213
messages = [messages];
314214
}
315-
messages = messages.map((message) => {
316-
return {
317-
...message,
318-
user: this.props.user,
319-
createdAt: new Date(),
320-
id: this.props.messageIdGenerator(),
321-
};
322-
});
215+
messages = messages.map(message => ({
216+
...message,
217+
user: this.props.user,
218+
createdAt: new Date(),
219+
id: this.props.messageIdGenerator(),
220+
}));
323221

324222
if (shouldResetInputToolbar === true) {
325223
this.setIsTypingDisabled(true);
@@ -343,12 +241,8 @@ class GiftedChat extends React.Component {
343241
this.textInput.clear();
344242
}
345243
this.notifyInputTextReset();
346-
const newComposerHeight = this.props.minComposerHeight;
347-
const newMessagesContainerHeight = this.getMessagesContainerHeightWithKeyboard(newComposerHeight);
348244
this.setState({
349245
text: this.getTextFromProp(''),
350-
composerHeight: newComposerHeight,
351-
messagesContainerHeight: this.prepareMessagesContainerHeight(newMessagesContainerHeight),
352246
});
353247
}
354248

@@ -359,17 +253,6 @@ class GiftedChat extends React.Component {
359253
}
360254

361255
onInputSizeChanged(size) {
362-
const newComposerHeight = Math.max(
363-
this.props.minComposerHeight,
364-
Math.min(this.props.maxComposerHeight, size.height),
365-
);
366-
const newMessagesContainerHeight = this.getMessagesContainerHeightWithKeyboard(
367-
newComposerHeight,
368-
);
369-
this.setState({
370-
composerHeight: newComposerHeight,
371-
messagesContainerHeight: this.prepareMessagesContainerHeight(newMessagesContainerHeight),
372-
});
373256
}
374257

375258
onInputTextChanged(text) {
@@ -397,14 +280,9 @@ class GiftedChat extends React.Component {
397280
return;
398281
}
399282
this.notifyInputTextReset();
400-
this.setMaxHeight(1000);
401-
const newComposerHeight = '100%';
402-
const newMessagesContainerHeight = newComposerHeight;// this.getMessagesContainerHeightWithKeyboard(newComposerHeight);
403283
this.setState({
404284
isInitialized: true,
405285
text: this.getTextFromProp(''),
406-
composerHeight: newComposerHeight,
407-
messagesContainerHeight: this.prepareMessagesContainerHeight(newMessagesContainerHeight),
408286
});
409287
}
410288

@@ -415,13 +293,13 @@ class GiftedChat extends React.Component {
415293
const inputToolbarProps = {
416294
...this.props,
417295
text: this.getTextFromProp(this.state.text),
418-
composerHeight: Math.max(this.props.minComposerHeight, this.state.composerHeight),
296+
composerHeight: this.state.composerHeight,
419297
onSend: this.onSend,
420298
onInputSizeChanged: this.onInputSizeChanged,
421299
onTextChanged: this.onInputTextChanged,
422300
textInputProps: {
423301
...this.props.textInputProps,
424-
ref: (textInput) => (this.textInput = textInput),
302+
ref: textInput => (this.textInput = textInput),
425303
maxLength: this.getIsTypingDisabled() ? 0 : this.props.maxInputLength,
426304
},
427305
};
@@ -467,13 +345,12 @@ class GiftedChat extends React.Component {
467345
</View>
468346
);
469347
}
470-
471348
}
472349

473350
const styles = StyleSheet.create({
474351
container: {
475352
flex: 1,
476-
height: '100%'
353+
height: '100%',
477354
},
478355
});
479356

src/InputToolbar.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import Actions from './Actions';
1010
import Color from './Color';
1111

1212
export default class InputToolbar extends React.Component {
13-
1413
constructor(props) {
1514
super(props);
1615

@@ -51,7 +50,7 @@ export default class InputToolbar extends React.Component {
5150
renderActions() {
5251
if (this.props.renderActions) {
5352
return this.props.renderActions(this.props);
54-
} else if (this.props.onPressActionButton) {
53+
} if (this.props.onPressActionButton) {
5554
return <Actions {...this.props} />;
5655
}
5756
return null;
@@ -83,7 +82,7 @@ export default class InputToolbar extends React.Component {
8382

8483
render() {
8584
return (
86-
<View style={[styles.container, this.props.containerStyle, { position: this.state.position }]}>
85+
<View style={[styles.container, this.props.containerStyle, { position: this.state.position, height: this.props.composerHeight }]}>
8786
<View style={[styles.primary, this.props.primaryStyle]}>
8887
{this.renderActions()}
8988
{this.renderComposer()}
@@ -93,7 +92,6 @@ export default class InputToolbar extends React.Component {
9392
</View>
9493
);
9594
}
96-
9795
}
9896

9997
const styles = StyleSheet.create({

0 commit comments

Comments
 (0)