u-search.vue 9.3 KB

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