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.

331 lines
10 KiB

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