Skip to content

Commit 680abe4

Browse files
author
marker dao ®
committed
Chat: Update input auto resize class setting condition
1 parent 14d2239 commit 680abe4

File tree

3 files changed

+78
-26
lines changed

3 files changed

+78
-26
lines changed

packages/devextreme/js/__internal/ui/chat/message_box/chat_text_area.ts

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import devices from '@js/core/devices';
44
import type { DefaultOptionsRule } from '@js/core/options/utils';
55
import type { dxElementWrapper } from '@js/core/renderer';
66
import $ from '@js/core/renderer';
7-
import { getOuterHeight } from '@js/core/utils/size';
87
import type { DxEvent, NativeEventInfo } from '@js/events';
98
import type {
109
ClickEvent,
@@ -404,29 +403,14 @@ class ChatTextArea extends TextArea<Properties> {
404403

405404
_renderButtonContainers(): void {}
406405

407-
_getHeightDifference($input: dxElementWrapper): number {
408-
const baseDifference = super._getHeightDifference($input);
409-
410-
const gap = parseFloat(this.$element().css('gap') ?? '0');
411-
412-
const informerHeight = this._informer ? getOuterHeight(this._informer.$element()) : 0;
413-
const fileUploaderHeight = getOuterHeight(this._$fileUploader);
414-
const toolbarHeight = getOuterHeight(this._$toolbar);
415-
416-
const visibleSections = [
417-
toolbarHeight,
418-
informerHeight,
419-
fileUploaderHeight,
420-
].filter(Boolean).length;
421-
422-
const totalExtraHeight = toolbarHeight
423-
+ informerHeight
424-
+ fileUploaderHeight
425-
+ visibleSections * gap;
406+
_getAdjustedMaxHeight(maxHeight: number): number {
407+
return maxHeight;
408+
}
426409

427-
const difference: number = baseDifference + totalExtraHeight;
410+
_getMaxHeight(): number | undefined {
411+
const maxHeight = parseFloat(this._input().css('maxHeight') ?? '0');
428412

429-
return difference;
413+
return maxHeight;
430414
}
431415

432416
_keyPressHandler(e: InputEvent): void {

packages/devextreme/js/__internal/ui/m_text_area.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import TextBox from '@ts/ui/text_box/m_text_box';
1717
import { allowScroll, prepareScrollData } from '@ts/ui/text_box/m_utils.scroll';
1818

1919
export const TEXTAREA_CLASS = 'dx-textarea';
20-
const TEXTEDITOR_INPUT_CLASS_AUTO_RESIZE = 'dx-texteditor-input-auto-resize';
20+
export const TEXTEDITOR_INPUT_CLASS_AUTO_RESIZE = 'dx-texteditor-input-auto-resize';
2121

2222
export interface TextAreaProperties extends Omit<Properties,
2323
'onChange' | 'onCopy' | 'onCut' | 'onEnterKey' | 'onFocusIn' | 'onFocusOut' | 'onInput' |
@@ -191,18 +191,20 @@ class TextArea<
191191
this._renderDimensions();
192192

193193
const minHeight = this._getBoundaryHeight('minHeight');
194-
const maxHeight = this._getBoundaryHeight('maxHeight');
194+
const maxHeight = this._getMaxHeight();
195+
195196
let inputHeight = $input[0].scrollHeight;
196197

197198
if (minHeight !== undefined) {
198199
inputHeight = Math.max(inputHeight, minHeight - heightDifference);
199200
}
200201

201202
if (maxHeight !== undefined) {
202-
const adjustedMaxHeight = maxHeight - heightDifference;
203-
const needScroll = inputHeight > adjustedMaxHeight;
203+
const adjustedMaxHeight = this._getAdjustedMaxHeight(maxHeight, heightDifference);
204+
const needScroll = this._isScrollNeeded(inputHeight, adjustedMaxHeight);
204205

205206
inputHeight = Math.min(inputHeight, adjustedMaxHeight);
207+
206208
this._updateInputAutoResizeAppearance($input, !needScroll);
207209
}
208210

@@ -213,6 +215,27 @@ class TextArea<
213215
}
214216
}
215217

218+
// eslint-disable-next-line class-methods-use-this
219+
_getAdjustedMaxHeight(maxHeight: number, heightDifference: number): number {
220+
const adjustedMaxHeight = maxHeight - heightDifference;
221+
222+
return adjustedMaxHeight;
223+
}
224+
225+
// eslint-disable-next-line class-methods-use-this
226+
_isScrollNeeded(
227+
inputHeight: number,
228+
adjustedMaxHeight: number,
229+
): boolean {
230+
const needScroll = inputHeight > adjustedMaxHeight;
231+
232+
return needScroll;
233+
}
234+
235+
_getMaxHeight(): number | undefined {
236+
return this._getBoundaryHeight('maxHeight');
237+
}
238+
216239
_getBoundaryHeight(optionName: string): number | undefined {
217240
const boundaryValue = this.option(optionName);
218241

packages/devextreme/testing/tests/DevExpress.ui.widgets/chatParts/chatTextArea.tests.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import FileUploader, { FILEUPLOADER_CLASS, FILEUPLOADER_CANCEL_BUTTON_CLASS } fr
99
import { BUTTON_CLASS } from '__internal/ui/button/button';
1010
import Informer, { INFORMER_CLASS, INFORMER_TEXT_CLASS } from '__internal/ui/informer/informer';
1111
import { TEXTEDITOR_INPUT_CLASS } from '__internal/ui/text_box/m_text_editor.base';
12+
import { TEXTEDITOR_INPUT_CLASS_AUTO_RESIZE } from '__internal/ui/m_text_area';
1213

1314
const fakeFile = {
1415
name: 'fakefile.png',
@@ -868,4 +869,48 @@ QUnit.module('ChatTextArea', moduleConfig, () => {
868869
});
869870
});
870871
});
872+
873+
QUnit.module('MaxHeight and scroll behavior', () => {
874+
QUnit.test('input should have auto-resize class by default', function(assert) {
875+
const hasAutoResizeClass = this.$input.hasClass(TEXTEDITOR_INPUT_CLASS_AUTO_RESIZE);
876+
877+
assert.ok(hasAutoResizeClass, 'input has auto-resize class');
878+
});
879+
880+
QUnit.test('textarea should expand when text is added', function(assert) {
881+
const initialHeight = this.$input.height();
882+
883+
this.typeText('Line 1\nLine 2\nLine 3');
884+
885+
const heightAfterTyping = this.$input.height();
886+
887+
assert.ok(heightAfterTyping > initialHeight, 'textarea height increased after adding multiline text');
888+
});
889+
890+
QUnit.test('textarea should not exceed default maxHeight', function(assert) {
891+
const longText = Array(100).fill('Line of text that should cause scrolling').join('\n');
892+
this.typeText(longText);
893+
894+
const inputHeight = this.$input.height();
895+
const maxHeightValue = parseFloat(this.$input.css('maxHeight'));
896+
897+
assert.roughEqual(inputHeight, maxHeightValue, 0.01, 'input height respects default maxHeight');
898+
assert.notStrictEqual(maxHeightValue, undefined, 'default maxHeight is applied');
899+
});
900+
901+
QUnit.test('textarea height should be restored after clearing text', function(assert) {
902+
const initialHeight = this.$input.height();
903+
904+
this.typeText('Line 1\nLine 2\nLine 3\nLine 4\nLine 5');
905+
906+
const heightWithText = this.$input.height();
907+
assert.ok(heightWithText > initialHeight, 'height increased with text');
908+
909+
this.instance.option('value', '');
910+
911+
const heightAfterClear = this.$input.height();
912+
913+
assert.roughEqual(heightAfterClear, initialHeight, 0.01, 'height is restored after clearing text');
914+
});
915+
});
871916
});

0 commit comments

Comments
 (0)