u-search.vue 9.5 KB

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