u-search.vue 9.5 KB

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