u-search.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <template>
  2. <view class="u-search" @tap="clickHandler" :style="[{
  3. margin: margin,
  4. }, $u.addStyle(customStyle)]">
  5. <view class="u-search__content" :style="{
  6. backgroundColor: bgColor,
  7. borderRadius: shape == 'round' ? '100px' : '4px',
  8. borderColor: borderColor,
  9. }">
  10. <template v-if="$slots.label || label !== null">
  11. <slot name="label">
  12. <text class="u-search__content__label">{{ label }}</text>
  13. </slot>
  14. </template>
  15. <view class="u-search__content__icon">
  16. <u-icon @tap="clickIcon" :size="searchIconSize" :name="searchIcon"
  17. :color="searchIconColor ? searchIconColor : color"></u-icon>
  18. </view>
  19. <input confirm-type="search" @blur="blur" :value="value" @confirm="search" @input="inputChange"
  20. :disabled="disabled" @focus="getFocus" :focus="focus" :maxlength="maxlength"
  21. placeholder-class="u-search__content__input--placeholder" :placeholder="placeholder"
  22. :placeholder-style="`color: ${placeholderColor}`" class="u-search__content__input" type="text" :style="[{
  23. textAlign: inputAlign,
  24. color: color,
  25. backgroundColor: bgColor,
  26. height: $u.addUnit(height)
  27. }, inputStyle]" />
  28. <view class="closeBox" v-if="keyword && clearabled && focused" @click="clear">
  29. <view class="u-search__content__icon u-search__content__close">
  30. <u-icon name="close" size="11" color="#ffffff" customStyle="line-height: 12px"></u-icon>
  31. </view>
  32. </view>
  33. </view>
  34. <text :style="[actionStyle]" class="u-search__action"
  35. :class="[(showActionBtn || show) && 'u-search__action--active']"
  36. @tap.stop.prevent="custom">{{ actionText }}</text>
  37. </view>
  38. </template>
  39. <script>
  40. import props from './props.js';
  41. /**
  42. * search 搜索框
  43. * @description 搜索组件,集成了常见搜索框所需功能,用户可以一键引入,开箱即用。
  44. * @tutorial https://www.uviewui.com/components/search.html
  45. * @property {String} shape 搜索框形状,round-圆形,square-方形(默认 'round' )
  46. * @property {String} bgColor 搜索框背景颜色(默认 '#f2f2f2' )
  47. * @property {String} placeholder 占位文字内容(默认 '请输入关键字' )
  48. * @property {Boolean} clearabled 是否启用清除控件(默认 true )
  49. * @property {Boolean} focus 是否自动获得焦点(默认 false )
  50. * @property {Boolean} showAction 是否显示右侧控件(默认 true )
  51. * @property {Object} actionStyle 右侧控件的样式,对象形式
  52. * @property {String} actionText 右侧控件文字(默认 '搜索' )
  53. * @property {String} inputAlign 输入框内容水平对齐方式 (默认 'left' )
  54. * @property {Object} inputStyle 自定义输入框样式,对象形式
  55. * @property {Boolean} disabled 是否启用输入框(默认 false )
  56. * @property {String} borderColor 边框颜色,配置了颜色,才会有边框 (默认 'transparent' )
  57. * @property {String} searchIconColor 搜索图标的颜色,默认同输入框字体颜色 (默认 '#909399' )
  58. * @property {Number | String} searchIconSize 搜索图标的字体,默认22
  59. * @property {String} color 输入框字体颜色(默认 '#606266' )
  60. * @property {String} placeholderColor placeholder的颜色(默认 '#909399' )
  61. * @property {String} searchIcon 输入框左边的图标,可以为uView图标名称或图片路径 (默认 'search' )
  62. * @property {String} margin 组件与其他上下左右元素之间的距离,带单位的字符串形式,如"30px" (默认 '0' )
  63. * @property {Boolean} animation 是否开启动画,见上方说明(默认 false )
  64. * @property {String} value 输入框初始值
  65. * @property {String | Number} maxlength 输入框最大能输入的长度,-1为不限制长度 (默认 '-1' )
  66. * @property {String | Number} height 输入框高度,单位px(默认 64 )
  67. * @property {String | Number} label 搜索框左边显示内容
  68. * @property {Object} customStyle 定义需要用到的外部样式
  69. *
  70. * @event {Function} change 输入框内容发生变化时触发
  71. * @event {Function} search 用户确定搜索时触发,用户按回车键,或者手机键盘右下角的"搜索"键时触发
  72. * @event {Function} custom 用户点击右侧控件时触发
  73. * @event {Function} clear 用户点击清除按钮时触发
  74. * @example <u-search placeholder="日照香炉生紫烟" v-model="keyword"></u-search>
  75. */
  76. export default {
  77. name: "u-search",
  78. mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
  79. data() {
  80. return {
  81. keyword: '',
  82. showClear: false, // 是否显示右边的清除图标
  83. show: false,
  84. // 标记input当前状态是否处于聚焦中,如果是,才会显示右侧的清除控件
  85. focused: this.focus
  86. // 绑定输入框的值
  87. // inputValue: this.value
  88. };
  89. },
  90. watch: {
  91. keyword(nVal) {
  92. // 双向绑定值,让v-model绑定的值双向变化
  93. this.$emit('input', nVal);
  94. // 触发change事件,事件效果和v-model双向绑定的效果一样,让用户多一个选择
  95. this.$emit('change', nVal);
  96. },
  97. value: {
  98. immediate: true,
  99. handler(nVal) {
  100. this.keyword = nVal;
  101. }
  102. }
  103. },
  104. computed: {
  105. showActionBtn() {
  106. return !this.animation && this.showAction
  107. }
  108. },
  109. methods: {
  110. // 目前HX2.6.9 v-model双向绑定无效,故监听input事件获取输入框内容的变化
  111. inputChange(e) {
  112. this.keyword = e.detail.value;
  113. },
  114. // 清空输入
  115. // 也可以作为用户通过this.$refs形式调用清空输入框内容
  116. clear() {
  117. this.keyword = '';
  118. // 延后发出事件,避免在父组件监听clear事件时,value为更新前的值(不为空)
  119. // this.$nextTick(() => {
  120. // this.$emit('clear');
  121. // })
  122. this.$emit('clear');
  123. },
  124. // 确定搜索
  125. search(e) {
  126. this.$emit('search', e.detail.value);
  127. try {
  128. // 收起键盘
  129. uni.hideKeyboard();
  130. } catch (e) {}
  131. },
  132. // 点击右边自定义按钮的事件
  133. custom() {
  134. this.$emit('custom', this.keyword);
  135. try {
  136. // 收起键盘
  137. uni.hideKeyboard();
  138. } catch (e) {}
  139. },
  140. // 获取焦点
  141. getFocus() {
  142. this.focused = true;
  143. // 开启右侧搜索按钮展开的动画效果
  144. if (this.animation && this.showAction) this.show = true;
  145. this.$emit('focus', this.keyword);
  146. },
  147. // 失去焦点
  148. blur() {
  149. // 最开始使用的是监听图标@touchstart事件,自从hx2.8.4后,此方法在微信小程序出错
  150. // 这里改为监听点击事件,手点击清除图标时,同时也发生了@blur事件,导致图标消失而无法点击,这里做一个延时
  151. setTimeout(() => {
  152. this.focused = false;
  153. }, 100)
  154. this.show = false;
  155. this.$emit('blur', this.keyword);
  156. },
  157. // 点击搜索框,只有disabled=true时才发出事件,因为禁止了输入,意味着是想跳转真正的搜索页
  158. clickHandler() {
  159. if (this.disabled) this.$emit('click');
  160. },
  161. // 点击左边图标
  162. clickIcon() {
  163. this.$emit('clickIcon');
  164. }
  165. }
  166. }
  167. </script>
  168. <style lang="scss" scoped>
  169. @import "../../libs/css/components.scss";
  170. $u-search-content-padding: 0 10px !default;
  171. $u-search-label-color: $u-main-color !default;
  172. $u-search-label-font-size: 14px !default;
  173. $u-search-label-margin: 0 4px !default;
  174. $u-search-close-size: 20px !default;
  175. $u-search-close-radius: 100px !default;
  176. $u-search-close-bgColor: #C6C7CB !default;
  177. $u-search-close-transform: scale(0.82) !default;
  178. $u-search-input-font-size: 14px !default;
  179. $u-search-input-margin: 0 5px !default;
  180. $u-search-input-color: $u-main-color !default;
  181. $u-search-input-placeholder-color: $u-tips-color !default;
  182. $u-search-action-font-size: 14px !default;
  183. $u-search-action-color: $u-main-color !default;
  184. $u-search-action-width: 0 !default;
  185. $u-search-action-active-width: 40px !default;
  186. $u-search-action-margin-left: 5px !default;
  187. /* #ifdef H5 */
  188. // iOS15在H5下,hx的某些版本,input type=search时,会多了一个搜索图标,进行移除
  189. [type="search"]::-webkit-search-decoration {
  190. display: none;
  191. }
  192. /* #endif */
  193. .u-search {
  194. @include flex(row);
  195. align-items: center;
  196. flex: 1;
  197. &__content {
  198. @include flex;
  199. align-items: center;
  200. padding: $u-search-content-padding;
  201. // padding-right: 0;
  202. flex: 1;
  203. justify-content: space-between;
  204. border-width: 1px;
  205. border-color: transparent;
  206. border-style: solid;
  207. overflow: hidden;
  208. &__icon {
  209. @include flex;
  210. align-items: center;
  211. }
  212. &__label {
  213. color: $u-search-label-color;
  214. font-size: $u-search-label-font-size;
  215. margin: $u-search-label-margin;
  216. }
  217. &__close {
  218. width: $u-search-close-size;
  219. height: $u-search-close-size;
  220. border-top-left-radius: $u-search-close-radius;
  221. border-top-right-radius: $u-search-close-radius;
  222. border-bottom-left-radius: $u-search-close-radius;
  223. border-bottom-right-radius: $u-search-close-radius;
  224. background-color: $u-search-close-bgColor;
  225. @include flex(row);
  226. align-items: center;
  227. justify-content: center;
  228. transform: $u-search-close-transform;
  229. }
  230. &__input {
  231. flex: 1;
  232. font-size: $u-search-input-font-size;
  233. line-height: 1;
  234. margin: $u-search-input-margin;
  235. color: $u-search-input-color;
  236. &--placeholder {
  237. color: $u-search-input-placeholder-color;
  238. }
  239. }
  240. }
  241. &__action {
  242. font-size: $u-search-action-font-size;
  243. color: $u-search-action-color;
  244. width: $u-search-action-width;
  245. overflow: hidden;
  246. transition-property: width;
  247. transition-duration: 0.3s;
  248. /* #ifndef APP-NVUE */
  249. white-space: nowrap;
  250. /* #endif */
  251. text-align: center;
  252. &--active {
  253. width: $u-search-action-active-width;
  254. margin-left: $u-search-action-margin-left;
  255. }
  256. }
  257. }
  258. .closeBox {
  259. align-items: center;
  260. justify-content: center;
  261. height: 55rpx;
  262. width: 65rpx;
  263. // background: red;
  264. }
  265. </style>