u-search.vue 11 KB

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