u-search.vue 10 KB

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