Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

378 linhas
12 KiB

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