|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright (c) 2021 - 2024 Vaadin Ltd. |
| 4 | + * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/ |
| 5 | + */ |
| 6 | +import { ControllerMixin } from '@vaadin/component-base/src/controller-mixin.js'; |
| 7 | +import { SlotController } from '@vaadin/component-base/src/slot-controller.js'; |
| 8 | +import { TooltipController } from '@vaadin/component-base/src/tooltip-controller.js'; |
| 9 | + |
| 10 | +/** |
| 11 | + * @polymerMixin |
| 12 | + * @mixes ControllerMixin |
| 13 | + */ |
| 14 | +export const MessageInputMixin = (superClass) => |
| 15 | + class MessageInputMixinClass extends ControllerMixin(superClass) { |
| 16 | + static get properties() { |
| 17 | + return { |
| 18 | + /** |
| 19 | + * Current content of the text input field |
| 20 | + */ |
| 21 | + value: { |
| 22 | + type: String, |
| 23 | + value: '', |
| 24 | + }, |
| 25 | + |
| 26 | + /** |
| 27 | + * The object used to localize this component. |
| 28 | + * For changing the default localization, change the entire |
| 29 | + * `i18n` object. |
| 30 | + * |
| 31 | + * The object has the following JSON structure and default values: |
| 32 | + * |
| 33 | + * ``` |
| 34 | + * { |
| 35 | + * // Used as the button label |
| 36 | + * send: 'Send', |
| 37 | + * |
| 38 | + * // Used as the input field's placeholder and aria-label |
| 39 | + * message: 'Message' |
| 40 | + * } |
| 41 | + * ``` |
| 42 | + * |
| 43 | + * @type {!MessageInputI18n} |
| 44 | + * @default {English} |
| 45 | + */ |
| 46 | + i18n: { |
| 47 | + type: Object, |
| 48 | + value: () => ({ |
| 49 | + send: 'Send', |
| 50 | + message: 'Message', |
| 51 | + }), |
| 52 | + }, |
| 53 | + |
| 54 | + /** |
| 55 | + * Set to true to disable this element. |
| 56 | + * @type {boolean} |
| 57 | + */ |
| 58 | + disabled: { |
| 59 | + type: Boolean, |
| 60 | + value: false, |
| 61 | + reflectToAttribute: true, |
| 62 | + }, |
| 63 | + |
| 64 | + /** @private */ |
| 65 | + _button: { |
| 66 | + type: Object, |
| 67 | + }, |
| 68 | + |
| 69 | + /** @private */ |
| 70 | + _textArea: { |
| 71 | + type: Object, |
| 72 | + }, |
| 73 | + }; |
| 74 | + } |
| 75 | + |
| 76 | + static get observers() { |
| 77 | + return [ |
| 78 | + '__buttonPropsChanged(_button, disabled, i18n)', |
| 79 | + '__textAreaPropsChanged(_textArea, disabled, i18n, value)', |
| 80 | + ]; |
| 81 | + } |
| 82 | + |
| 83 | + /** @protected */ |
| 84 | + ready() { |
| 85 | + super.ready(); |
| 86 | + |
| 87 | + this._buttonController = new SlotController(this, 'button', 'vaadin-button', { |
| 88 | + initializer: (btn) => { |
| 89 | + btn.setAttribute('theme', 'primary contained'); |
| 90 | + |
| 91 | + btn.addEventListener('click', () => { |
| 92 | + this.__submit(); |
| 93 | + }); |
| 94 | + |
| 95 | + this._button = btn; |
| 96 | + }, |
| 97 | + }); |
| 98 | + this.addController(this._buttonController); |
| 99 | + |
| 100 | + this._textAreaController = new SlotController(this, 'textarea', 'vaadin-text-area', { |
| 101 | + initializer: (textarea) => { |
| 102 | + textarea.addEventListener('value-changed', (event) => { |
| 103 | + this.value = event.detail.value; |
| 104 | + }); |
| 105 | + |
| 106 | + textarea.addEventListener('keydown', (event) => { |
| 107 | + if (event.key === 'Enter' && !event.shiftKey) { |
| 108 | + event.preventDefault(); |
| 109 | + event.stopImmediatePropagation(); |
| 110 | + this.__submit(); |
| 111 | + } |
| 112 | + }); |
| 113 | + |
| 114 | + const input = textarea.inputElement; |
| 115 | + input.removeAttribute('aria-labelledby'); |
| 116 | + |
| 117 | + // Set initial height to one row |
| 118 | + input.setAttribute('rows', 1); |
| 119 | + input.style.minHeight = '0'; |
| 120 | + |
| 121 | + this._textArea = textarea; |
| 122 | + }, |
| 123 | + }); |
| 124 | + this.addController(this._textAreaController); |
| 125 | + |
| 126 | + this._tooltipController = new TooltipController(this); |
| 127 | + this.addController(this._tooltipController); |
| 128 | + } |
| 129 | + |
| 130 | + focus() { |
| 131 | + if (this._textArea) { |
| 132 | + this._textArea.focus(); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + /** @private */ |
| 137 | + __buttonPropsChanged(button, disabled, i18n) { |
| 138 | + if (button) { |
| 139 | + button.disabled = disabled; |
| 140 | + button.textContent = i18n.send; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + /** @private */ |
| 145 | + __textAreaPropsChanged(textArea, disabled, i18n, value) { |
| 146 | + if (textArea) { |
| 147 | + textArea.disabled = disabled; |
| 148 | + textArea.value = value; |
| 149 | + |
| 150 | + const message = i18n.message; |
| 151 | + textArea.placeholder = message; |
| 152 | + |
| 153 | + if (message) { |
| 154 | + textArea.inputElement.setAttribute('aria-label', message); |
| 155 | + } else { |
| 156 | + textArea.inputElement.removeAttribute('aria-label'); |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Submits the current value as an custom event named 'submit'. |
| 163 | + * It also clears the text input and refocuses it for sending another message. |
| 164 | + * In UI, can be triggered by pressing the submit button or pressing enter key when field is focused. |
| 165 | + * It does not submit anything if text is empty. |
| 166 | + */ |
| 167 | + __submit() { |
| 168 | + if (this.value !== '') { |
| 169 | + this.dispatchEvent(new CustomEvent('submit', { detail: { value: this.value } })); |
| 170 | + this.value = ''; |
| 171 | + } |
| 172 | + this._textArea.focus(); |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Fired when a new message is submitted with `<vaadin-message-input>`, either |
| 177 | + * by clicking the "send" button, or pressing the Enter key. |
| 178 | + * @event submit |
| 179 | + */ |
| 180 | + }; |
0 commit comments