综合办公系统
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.

uv-search.vue 10KB

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