diff --git a/packages/devextreme/js/__internal/ui/chat/chat_message_box.ts b/packages/devextreme/js/__internal/ui/chat/chat_message_box.ts index 72b1de0c96c8..48d36fb7e49c 100644 --- a/packages/devextreme/js/__internal/ui/chat/chat_message_box.ts +++ b/packages/devextreme/js/__internal/ui/chat/chat_message_box.ts @@ -86,6 +86,11 @@ class MessageBox extends DOMComponent { autoResizeEnabled: true, valueChangeEvent: 'input', maxHeight: '20em', + onInput: (): void => { + const shouldButtonBeDisabled = !this._isValuableTextEntered(); + + this._toggleButtonDisableState(shouldButtonBeDisabled); + }, onEnterKey: (e: EnterKeyEvent): void => { if (!e.event?.shiftKey) { this._sendHandler(e); @@ -118,6 +123,7 @@ class MessageBox extends DOMComponent { icon: 'sendfilled', type: 'default', stylingMode: 'text', + disabled: true, onClick: (e): void => { this._sendHandler(e); }, @@ -132,14 +138,19 @@ class MessageBox extends DOMComponent { } _sendHandler(e: ClickEvent | EnterKeyEvent): void { - const { text } = this._textArea.option(); - if (!this._isValuableTextEntered()) { return; } + const { text } = this._textArea.option(); + this._messageSendAction?.({ text, event: e.event }); - this._textArea?.reset(); + this._textArea.reset(); + this._toggleButtonDisableState(true); + } + + _toggleButtonDisableState(state: boolean): void { + this._button.option('disabled', state); } _optionChanged(args: OptionChanged): void { diff --git a/packages/devextreme/testing/tests/DevExpress.ui.widgets/chatParts/messageBox.tests.js b/packages/devextreme/testing/tests/DevExpress.ui.widgets/chatParts/messageBox.tests.js index d4ba82392a76..267b816709f4 100644 --- a/packages/devextreme/testing/tests/DevExpress.ui.widgets/chatParts/messageBox.tests.js +++ b/packages/devextreme/testing/tests/DevExpress.ui.widgets/chatParts/messageBox.tests.js @@ -22,9 +22,12 @@ const moduleConfig = { this.$element = $(this.instance.$element()); this.$textArea = this.$element.find(`.${CHAT_MESSAGE_BOX_TEXTAREA_CLASS}`); + this.textArea = TextArea.getInstance(this.$textArea); + this.$input = this.$element.find(`.${TEXTEDITOR_INPUT_CLASS}`); this.$sendButton = this.$element.find(`.${CHAT_MESSAGE_BOX_BUTTON_CLASS}`); + this.sendButton = Button.getInstance(this.$sendButton); }; this.reinit = (options) => { @@ -47,11 +50,11 @@ QUnit.module('MessageBox', moduleConfig, () => { icon: 'sendfilled', type: 'default', stylingMode: 'text', + disabled: true, }; - const sendButton = this.$sendButton.dxButton('instance'); Object.entries(expectedOptions).forEach(([key, value]) => { - assert.deepEqual(value, sendButton.option(key), `${key} value is correct`); + assert.deepEqual(value, this.sendButton.option(key), `${key} value is correct`); }); }); @@ -96,6 +99,63 @@ QUnit.module('MessageBox', moduleConfig, () => { assert.strictEqual(this.$input.val(), emptyValue); }); + + QUnit.test('send button should not be disabled after entering any character into input', function(assert) { + keyboardMock(this.$input) + .focus() + .type('i'); + + const { disabled } = this.sendButton.option(); + + assert.strictEqual(disabled, false); + }); + + QUnit.test('send button should be disabled after entering any character into input and clicking the button', function(assert) { + keyboardMock(this.$input) + .focus() + .type('i'); + + this.$sendButton.trigger('dxclick'); + + const { disabled } = this.sendButton.option(); + + assert.strictEqual(disabled, true); + }); + + QUnit.test('send button should be disabled after entering spacing into input', function(assert) { + const emptyValue = ' '; + + keyboardMock(this.$input) + .focus() + .type(emptyValue); + + const { disabled } = this.sendButton.option(); + + assert.strictEqual(disabled, true); + }); + + QUnit.test('send button should be disabled after adding a line break into input', function(assert) { + const lineBreakValue = '\n'; + + keyboardMock(this.$input) + .focus() + .type(lineBreakValue); + + const { disabled } = this.sendButton.option(); + + assert.strictEqual(disabled, true); + }); + + QUnit.test('send button should be disabled after entering any character and then removing it', function(assert) { + keyboardMock(this.$input) + .focus() + .type('i') + .press('backspace'); + + const { disabled } = this.sendButton.option(); + + assert.strictEqual(disabled, true); + }); }); QUnit.module('onMessageSend event', () => { @@ -247,12 +307,9 @@ QUnit.module('MessageBox', moduleConfig, () => { this.reinit(options); - const button = Button.getInstance(this.$sendButton); - const textArea = TextArea.getInstance(this.$textArea); - Object.entries(options).forEach(([key, value]) => { - assert.deepEqual(value, button.option(key), `button ${key} value is correct`); - assert.deepEqual(value, textArea.option(key), `textarea ${key} value is correct`); + assert.deepEqual(value, this.sendButton.option(key), `button ${key} value is correct`); + assert.deepEqual(value, this.textArea.option(key), `textarea ${key} value is correct`); }); }); @@ -265,12 +322,9 @@ QUnit.module('MessageBox', moduleConfig, () => { this.instance.option(options); - const button = Button.getInstance(this.$sendButton); - const textArea = TextArea.getInstance(this.$textArea); - Object.entries(options).forEach(([key, value]) => { - assert.deepEqual(value, button.option(key), `button ${key} value is correct`); - assert.deepEqual(value, textArea.option(key), `textarea ${key} value is correct`); + assert.deepEqual(value, this.sendButton.option(key), `button ${key} value is correct`); + assert.deepEqual(value, this.textArea.option(key), `textarea ${key} value is correct`); }); }); });