u-search.vue 10 KB

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