chatInput.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <template>
  2. <view class="chat-input-container" :class="{ 'parent-container-focused': isFocus }">
  3. <view class="x-f input-container-optimized" :class="{
  4. 'input-container-focused': isFocus,
  5. 'justify-between': !isFocus
  6. }" :style="containerStyle">
  7. <input id="txgMsg" :placeholder="placeholderText" v-model="inputValue" placeholder-style="color:#e7e7e7;"
  8. placeholder-class="placeholder-style" class="ml20 input-native input-optimized"
  9. :class="{ 'input-focused': isFocus }" @focus="handleFocus" @blur="handleBlur" cursor-spacing="100"
  10. :adjust-position="false" />
  11. <view v-if="isFocus" class="send" @click="handleSend">发送</view>
  12. </view>
  13. <view class="justify-between mr15 align-center shopping-icon-container" :style="iconContainerStyle">
  14. <view class="icon-bg ml20" @tap="handleOpenCart">
  15. <image src="/static/images/shopping.png" class="w58 h58" />
  16. </view>
  17. <view class="icon-bg ml20" @click="handleShowGift">
  18. <image src="/static/images/gift.png" class="w58 h58" />
  19. </view>
  20. </view>
  21. </view>
  22. </template>
  23. <script>
  24. export default {
  25. name: 'ChatInput',
  26. props: {
  27. // 输入框占位符文本
  28. placeholderText: {
  29. type: String,
  30. default: '说点什么...'
  31. },
  32. // 初始输入值
  33. value: {
  34. type: String,
  35. default: ''
  36. },
  37. // 是否聚焦状态(从父组件传递)
  38. focused: {
  39. type: Boolean,
  40. default: false
  41. }
  42. },
  43. data() {
  44. return {
  45. isFocus: false,
  46. inputValue: ''
  47. };
  48. },
  49. computed: {
  50. // 输入框容器样式
  51. containerStyle() {
  52. return {
  53. padding: this.isFocus ? '10rpx 10rpx 10rpx 20rpx' : '10rpx 14rpx 10rpx 32rpx',
  54. width: this.isFocus ? 'calc(100% - 20rpx)' : '100%',
  55. maxWidth: this.isFocus ? 'calc(100% - 20rpx)' : 'auto',
  56. marginRight: this.isFocus ? '20rpx' : '0',
  57. flex: this.isFocus ? '1' : 'auto'
  58. };
  59. },
  60. // 图标容器样式
  61. iconContainerStyle() {
  62. return {
  63. opacity: this.isFocus ? 0 : 1,
  64. visibility: this.isFocus ? 'hidden' : 'visible',
  65. transform: 'translateZ(0)'
  66. };
  67. }
  68. },
  69. watch: {
  70. // 监听父组件传递的聚焦状态
  71. focused: {
  72. immediate: true,
  73. handler(newVal) {
  74. this.isFocus = newVal;
  75. }
  76. },
  77. // 监听输入值变化,同步到父组件
  78. inputValue(newVal) {
  79. this.$emit('input', newVal);
  80. },
  81. // 监听父组件传递的值变化
  82. value: {
  83. immediate: true,
  84. handler(newVal) {
  85. this.inputValue = newVal;
  86. }
  87. }
  88. },
  89. methods: {
  90. // 输入框聚焦事件
  91. handleFocus() {
  92. this.isFocus = true;
  93. this.$emit('focus', this.inputValue);
  94. },
  95. // 输入框失焦事件
  96. handleBlur() {
  97. this.isFocus = false;
  98. this.$emit('blur', this.inputValue);
  99. },
  100. // 发送消息
  101. handleSend() {
  102. if (this.inputValue.trim()) {
  103. this.$emit('send', this.inputValue);
  104. this.inputValue = ''; // 清空输入框
  105. } else {
  106. uni.showToast({
  107. title: '不能发送空消息',
  108. icon: 'none'
  109. });
  110. }
  111. },
  112. // 打开购物车
  113. handleOpenCart() {
  114. this.$emit('open-cart');
  115. },
  116. // 显示礼物弹窗
  117. handleShowGift() {
  118. this.$emit('show-gift');
  119. },
  120. // 清空输入框(供父组件调用)
  121. clearInput() {
  122. this.inputValue = '';
  123. },
  124. // 设置输入框值(供父组件调用)
  125. setInputValue(value) {
  126. this.inputValue = value;
  127. },
  128. // 获取输入框值(供父组件调用)
  129. getInputValue() {
  130. return this.inputValue;
  131. }
  132. }
  133. };
  134. </script>
  135. <style scoped lang="scss">
  136. .chat-input-container {
  137. display: flex;
  138. justify-content: space-between;
  139. align-items: center;
  140. padding: 24rpx;
  141. width: 100%;
  142. box-sizing: border-box;
  143. &.parent-container-focused {
  144. justify-content: flex-start !important;
  145. align-items: stretch !important;
  146. }
  147. }
  148. .input-container-optimized {
  149. background: #393939;
  150. height: 90rpx;
  151. box-sizing: border-box;
  152. border-radius: 36rpx;
  153. transform: translateZ(0);
  154. will-change: transform;
  155. backface-visibility: hidden;
  156. perspective: 1000px;
  157. display: flex;
  158. align-items: center;
  159. &.input-container-focused {
  160. box-sizing: border-box;
  161. width: calc(100% - 20rpx) !important;
  162. max-width: calc(100% - 20rpx) !important;
  163. min-width: calc(100% - 20rpx) !important;
  164. margin-right: 20rpx !important;
  165. padding: 10rpx 10rpx 10rpx 20rpx !important;
  166. justify-content: flex-start !important;
  167. gap: 20rpx !important;
  168. flex: 1 !important;
  169. flex-grow: 1 !important;
  170. flex-shrink: 0 !important;
  171. flex-basis: calc(100% - 20rpx) !important;
  172. }
  173. &.justify-between {
  174. justify-content: space-between;
  175. }
  176. }
  177. .input-optimized {
  178. border: none !important;
  179. font-size: 32rpx !important;
  180. color: #ffffff !important;
  181. background: transparent !important;
  182. width: 70% !important;
  183. transform: translateZ(0);
  184. will-change: contents, width;
  185. backface-visibility: hidden;
  186. flex: 1;
  187. &.input-focused {
  188. width: auto !important;
  189. flex: 1 !important;
  190. min-width: 0 !important;
  191. margin-left: 10rpx !important;
  192. max-width: none !important;
  193. }
  194. }
  195. .send {
  196. background-color: #fafafa;
  197. border-radius: 28rpx;
  198. padding: 14rpx 16rpx;
  199. color: #181818;
  200. font-weight: 500;
  201. min-width: 80rpx;
  202. text-align: center;
  203. flex-shrink: 0;
  204. white-space: nowrap;
  205. }
  206. .shopping-icon-container {
  207. will-change: opacity, visibility;
  208. backface-visibility: hidden;
  209. display: flex;
  210. align-items: center;
  211. justify-content: space-between;
  212. flex-shrink: 0;
  213. }
  214. .icon-bg {
  215. background-color: rgba(57, 57, 57, 0.8);
  216. border-radius: 50%;
  217. width: 88rpx;
  218. height: 88rpx;
  219. display: flex;
  220. justify-content: center;
  221. align-items: center;
  222. transition: transform 0.2s ease;
  223. &:active {
  224. transform: scale(0.95);
  225. }
  226. }
  227. </style>