u-search.vue 9.9 KB

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