u-search.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <template>
  2. <view
  3. class="u-search"
  4. @tap="clickHandler"
  5. :style="[{
  6. margin: margin,
  7. }, $u.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. height: height + 'rpx'
  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="22"
  27. :name="searchIcon"
  28. :color="searchIconColor ? searchIconColor : color"
  29. ></u-icon>
  30. </view>
  31. <input
  32. confirm-type="search"
  33. @blur="blur"
  34. :value="value"
  35. @confirm="search"
  36. @input="inputChange"
  37. :disabled="disabled"
  38. @focus="getFocus"
  39. :focus="focus"
  40. :maxlength="maxlength"
  41. placeholder-class="u-search__content__input--placeholder"
  42. :placeholder="placeholder"
  43. :placeholder-style="`color: ${placeholderColor}`"
  44. class="u-search__content__input"
  45. type="text"
  46. :style="[{
  47. textAlign: inputAlign,
  48. color: color,
  49. backgroundColor: bgColor,
  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 }}
  71. </text>
  72. </view>
  73. </template>
  74. <script>
  75. import props from './props.js';
  76. /**
  77. * search 搜索框
  78. * @description 搜索组件,集成了常见搜索框所需功能,用户可以一键引入,开箱即用。
  79. * @tutorial https://www.uviewui.com/components/search.html
  80. * @property {String} shape 搜索框形状,round-圆形,square-方形(默认 'round' )
  81. * @property {String} bgColor 搜索框背景颜色(默认 '#f2f2f2' )
  82. * @property {String} placeholder 占位文字内容(默认 '请输入关键字' )
  83. * @property {Boolean} clearabled 是否启用清除控件(默认 true )
  84. * @property {Boolean} focus 是否自动获得焦点(默认 false )
  85. * @property {Boolean} showAction 是否显示右侧控件(默认 true )
  86. * @property {Object} actionStyle 右侧控件的样式,对象形式
  87. * @property {String} actionText 右侧控件文字(默认 '搜索' )
  88. * @property {String} inputAlign 输入框内容水平对齐方式 (默认 'left' )
  89. * @property {Object} inputStyle 自定义输入框样式,对象形式
  90. * @property {Boolean} disabled 是否启用输入框(默认 false )
  91. * @property {String} borderColor 边框颜色,配置了颜色,才会有边框 (默认 'transparent' )
  92. * @property {String} searchIconColor 搜索图标的颜色,默认同输入框字体颜色 (默认 '#909399' )
  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: [uni.$u.mpMixin, uni.$u.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. this.$emit('input', nVal);
  128. // 触发change事件,事件效果和v-model双向绑定的效果一样,让用户多一个选择
  129. this.$emit('change', nVal);
  130. },
  131. value: {
  132. immediate: true,
  133. handler(nVal) {
  134. this.keyword = nVal;
  135. }
  136. }
  137. },
  138. computed: {
  139. showActionBtn() {
  140. return !this.animation && this.showAction
  141. }
  142. },
  143. methods: {
  144. // 目前HX2.6.9 v-model双向绑定无效,故监听input事件获取输入框内容的变化
  145. inputChange(e) {
  146. this.keyword = e.detail.value;
  147. },
  148. // 清空输入
  149. // 也可以作为用户通过this.$refs形式调用清空输入框内容
  150. clear() {
  151. this.keyword = '';
  152. // 延后发出事件,避免在父组件监听clear事件时,value为更新前的值(不为空)
  153. this.$nextTick(() => {
  154. this.$emit('clear');
  155. })
  156. },
  157. // 确定搜索
  158. search(e) {
  159. this.$emit('search', e.detail.value);
  160. try {
  161. // 收起键盘
  162. uni.hideKeyboard();
  163. } catch (e) {
  164. }
  165. },
  166. // 点击右边自定义按钮的事件
  167. custom() {
  168. this.$emit('custom', this.keyword);
  169. try {
  170. // 收起键盘
  171. uni.hideKeyboard();
  172. } catch (e) {
  173. }
  174. },
  175. // 获取焦点
  176. getFocus() {
  177. this.focused = true;
  178. // 开启右侧搜索按钮展开的动画效果
  179. if (this.animation && this.showAction) this.show = true;
  180. this.$emit('focus', this.keyword);
  181. },
  182. // 失去焦点
  183. blur() {
  184. // 最开始使用的是监听图标@touchstart事件,自从hx2.8.4后,此方法在微信小程序出错
  185. // 这里改为监听点击事件,手点击清除图标时,同时也发生了@blur事件,导致图标消失而无法点击,这里做一个延时
  186. setTimeout(() => {
  187. this.focused = false;
  188. }, 100)
  189. this.show = false;
  190. this.$emit('blur', this.keyword);
  191. },
  192. // 点击搜索框,只有disabled=true时才发出事件,因为禁止了输入,意味着是想跳转真正的搜索页
  193. clickHandler() {
  194. if (this.disabled) this.$emit('click');
  195. },
  196. // 点击左边图标
  197. clickIcon() {
  198. this.$emit('clickIcon');
  199. }
  200. }
  201. }
  202. </script>
  203. <style lang="scss" scoped>
  204. @import "../../libs/css/components.scss";
  205. $u-search-content-padding: 0 10px !default;
  206. $u-search-label-color: $u-main-color !default;
  207. $u-search-label-font-size: 14px !default;
  208. $u-search-label-margin: 0 4px !default;
  209. $u-search-close-size: 20px !default;
  210. $u-search-close-radius: 100px !default;
  211. $u-search-close-bgColor: #C6C7CB !default;
  212. $u-search-close-transform: scale(0.82) !default;
  213. $u-search-input-font-size: 14px !default;
  214. $u-search-input-margin: 0 5px !default;
  215. $u-search-input-color: $u-main-color !default;
  216. $u-search-input-placeholder-color: $u-tips-color !default;
  217. $u-search-action-font-size: 14px !default;
  218. $u-search-action-color: $u-main-color !default;
  219. $u-search-action-width: 0 !default;
  220. $u-search-action-active-width: 40px !default;
  221. $u-search-action-margin-left: 5px !default;
  222. /* #ifdef H5 */
  223. // iOS15在H5下,hx的某些版本,input type=search时,会多了一个搜索图标,进行移除
  224. [type="search"]::-webkit-search-decoration {
  225. display: none;
  226. }
  227. /* #endif */
  228. .u-search {
  229. @include flex(row);
  230. align-items: center;
  231. flex: 1;
  232. &__content {
  233. @include flex;
  234. align-items: center;
  235. padding: $u-search-content-padding;
  236. flex: 1;
  237. justify-content: space-between;
  238. border-width: 1px;
  239. border-color: transparent;
  240. border-style: solid;
  241. &__icon {
  242. @include flex;
  243. align-items: center;
  244. }
  245. &__label {
  246. color: $u-search-label-color;
  247. font-size: $u-search-label-font-size;
  248. margin: $u-search-label-margin;
  249. }
  250. &__close {
  251. width: $u-search-close-size;
  252. height: $u-search-close-size;
  253. border-top-left-radius: $u-search-close-radius;
  254. border-top-right-radius: $u-search-close-radius;
  255. border-bottom-left-radius: $u-search-close-radius;
  256. border-bottom-right-radius: $u-search-close-radius;
  257. background-color: $u-search-close-bgColor;
  258. @include flex(row);
  259. align-items: center;
  260. justify-content: center;
  261. transform: $u-search-close-transform;
  262. }
  263. &__input {
  264. flex: 1;
  265. font-size: $u-search-input-font-size;
  266. line-height: 1;
  267. margin: $u-search-input-margin;
  268. color: $u-search-input-color;
  269. &--placeholder {
  270. color: $u-search-input-placeholder-color;
  271. }
  272. }
  273. }
  274. &__action {
  275. font-size: $u-search-action-font-size;
  276. color: $u-search-action-color;
  277. width: $u-search-action-width;
  278. overflow: hidden;
  279. transition-property: width;
  280. transition-duration: 0.3s;
  281. /* #ifndef APP-NVUE */
  282. white-space: nowrap;
  283. /* #endif */
  284. text-align: center;
  285. &--active {
  286. width: $u-search-action-active-width;
  287. margin-left: $u-search-action-margin-left;
  288. }
  289. }
  290. }
  291. </style>