uni-events-helper-wx
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

295 lines
9.4 KiB

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