index.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <template>
  2. <div
  3. :class="[
  4. 'tui-search-input-container',
  5. !isPC && 'tui-search-input-container-h5',
  6. props.searchType === 'global'
  7. ? 'tui-search-input-container-global'
  8. : 'tui-search-input-container-conversation'
  9. ]"
  10. >
  11. <div :class="['tui-search-input', !isPC && 'tui-search-input-h5']">
  12. <div class="tui-search-input-left">
  13. <Icon
  14. class="icon"
  15. :file="searchIcon"
  16. width="14px"
  17. height="14px"
  18. />
  19. </div>
  20. <input
  21. v-model="searchValueModel"
  22. class="tui-search-input-main"
  23. type="text"
  24. :placeholder="props.placeholder"
  25. :focus="false"
  26. enterkeyhint="search"
  27. @blur="onBlur"
  28. @keyup.enter="search"
  29. @confirm="search"
  30. @click.stop.prevent="onSearchInputClick"
  31. >
  32. <div
  33. v-if="searchingStatus"
  34. class="tui-search-input-right"
  35. @click="endSearching"
  36. >
  37. <Icon
  38. class="icon"
  39. :file="closeIcon"
  40. width="14px"
  41. height="14px"
  42. />
  43. </div>
  44. </div>
  45. <div
  46. v-if="!isPC && searchingStatus && props.searchType === 'global'"
  47. :class="[
  48. 'tui-search-input-cancel',
  49. !isPC && 'tui-search-input-h5-cancel',
  50. ]"
  51. @click="endSearching"
  52. >
  53. {{ TUITranslateService.t("TUISearch.取消") }}
  54. </div>
  55. </div>
  56. </template>
  57. <script lang="ts" setup>
  58. import { ref, onMounted, onUnmounted } from '../../../adapter-vue';
  59. import {
  60. TUIStore,
  61. StoreName,
  62. TUITranslateService,
  63. } from '@tencentcloud/chat-uikit-engine';
  64. import { TUIGlobal } from '@tencentcloud/universal-api';
  65. import Icon from '../../common/Icon.vue';
  66. import searchIcon from '../../../assets/icon/search.svg';
  67. import closeIcon from '../../../assets/icon/input-close.svg';
  68. import { isPC } from '../../../utils/env';
  69. import { ISearchInputValue, ISearchingStatus } from '../type';
  70. const props = defineProps({
  71. placeholder: {
  72. type: String,
  73. default: () => TUITranslateService.t('TUISearch.搜索'),
  74. },
  75. searchType: {
  76. type: String,
  77. default: 'global', // "global" / "conversation"
  78. validator(value: string) {
  79. return ['global', 'conversation'].includes(value);
  80. },
  81. },
  82. });
  83. const searchValueModel = ref<string>('');
  84. const currentSearchInputValue = ref<string>('');
  85. const searchingStatus = ref<boolean>(false);
  86. function onCurrentSearchInputValueChange(obj: ISearchInputValue) {
  87. if (obj?.searchType === props?.searchType) {
  88. currentSearchInputValue.value = obj?.value;
  89. searchValueModel.value = obj?.value;
  90. }
  91. }
  92. function onCurrentSearchingStatusChange(obj: ISearchingStatus) {
  93. if (obj?.searchType === props?.searchType) {
  94. searchingStatus.value = obj?.isSearching;
  95. }
  96. }
  97. onMounted(() => {
  98. TUIStore.watch(StoreName.SEARCH, {
  99. currentSearchInputValue: onCurrentSearchInputValueChange,
  100. currentSearchingStatus: onCurrentSearchingStatusChange,
  101. });
  102. });
  103. onUnmounted(() => {
  104. TUIStore.unwatch(StoreName.SEARCH, {
  105. currentSearchInputValue: onCurrentSearchInputValueChange,
  106. currentSearchingStatus: onCurrentSearchingStatusChange,
  107. });
  108. });
  109. const search = () => {
  110. // Avoid duplicate searches
  111. if (searchValueModel.value === currentSearchInputValue.value) {
  112. return;
  113. }
  114. TUIStore.update(StoreName.SEARCH, 'currentSearchInputValue', {
  115. value: searchValueModel.value,
  116. searchType: props.searchType,
  117. });
  118. };
  119. const endSearching = () => {
  120. searchingStatus.value = false;
  121. TUIStore.update(StoreName.SEARCH, 'currentSearchingStatus', {
  122. isSearching: false,
  123. searchType: props.searchType,
  124. });
  125. TUIStore.update(StoreName.SEARCH, 'currentSearchInputValue', {
  126. value: '',
  127. searchType: props.searchType,
  128. });
  129. };
  130. const onSearchInputClick = () => {
  131. TUIStore.update(StoreName.SEARCH, 'currentSearchingStatus', {
  132. isSearching: true,
  133. searchType: props.searchType,
  134. });
  135. };
  136. const onBlur = () => {
  137. TUIGlobal?.hideKeyboard?.();
  138. };
  139. </script>
  140. <style lang="scss" scoped>
  141. .tui-search-input-container {
  142. display: flex;
  143. flex-direction: row;
  144. box-sizing: border-box;
  145. &-global {
  146. flex: 1;
  147. }
  148. .tui-search-input {
  149. flex: 1;
  150. display: flex;
  151. flex-direction: row;
  152. width: calc(100% - 20px);
  153. margin: 10px;
  154. background: #ededed;
  155. justify-content: center;
  156. align-items: center;
  157. height: 28px;
  158. border-radius: 4px;
  159. &-main {
  160. flex: 1;
  161. background: transparent;
  162. border: none;
  163. caret-color: #007aff;
  164. font-size: 14px;
  165. &:focus {
  166. border: none;
  167. outline: none;
  168. }
  169. &::placeholder {
  170. color: #666;
  171. font-size: 12px;
  172. }
  173. }
  174. &-left,
  175. &-right {
  176. display: flex;
  177. width: 14px;
  178. height: 14px;
  179. padding: 0 7px;
  180. }
  181. }
  182. }
  183. .tui-search-input-container-h5 {
  184. .tui-search-input-h5 {
  185. height: 34px;
  186. }
  187. .tui-search-input-cancel {
  188. display: flex;
  189. flex-direction: column;
  190. justify-content: center;
  191. align-items: center;
  192. color: #007aff;
  193. font-size: 16px;
  194. padding: 7px 10px 7px 3px;
  195. font-family: "PingFang SC", sans-serif;
  196. }
  197. }
  198. </style>