u-search.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. width:width+ 'rpx',
  13. }"
  14. >
  15. <view class="u-icon-wrap">
  16. <u-icon class="u-clear-icon" :size="30" :name="searchIcon" :color="searchIconColor ? searchIconColor : color"></u-icon>
  17. </view>
  18. <input
  19. ref="usearchinput"
  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. textAlign: inputAlign,
  36. color: color,
  37. backgroundColor: bgColor,
  38. }, inputStyle]"
  39. />
  40. <view class="u-close-wrap" v-if="keyword && clearabled && focused" @tap="clear">
  41. <u-icon class="u-clear-icon" name="close-circle-fill" size="34" color="#c0c4cc"></u-icon>
  42. </view>
  43. </view>
  44. <view :style="[actionStyle]" class="u-action"
  45. :class="[showActionBtn || show ? 'u-action-active' : '']"
  46. @tap.stop.prevent="custom"
  47. >{{ 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. * @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. width: {
  154. type: [Number, String],
  155. default: 750
  156. },
  157. // 搜索框高度,单位rpx
  158. height: {
  159. type: [Number, String],
  160. default: 64
  161. },
  162. // input输入框的样式,可以定义文字颜色,大小等,对象形式
  163. inputStyle: {
  164. type: Object,
  165. default() {
  166. return {}
  167. }
  168. },
  169. // 输入框最大能输入的长度,-1为不限制长度(来自uniapp文档)
  170. maxlength: {
  171. type: [Number, String],
  172. default: '-1'
  173. },
  174. // 搜索图标的颜色,默认同输入框字体颜色
  175. searchIconColor: {
  176. type: String,
  177. default: ''
  178. },
  179. // 输入框字体颜色
  180. color: {
  181. type: String,
  182. default: '#606266'
  183. },
  184. // placeholder的颜色
  185. placeholderColor: {
  186. type: String,
  187. default: '#909399'
  188. },
  189. // 组件与其他上下左右元素之间的距离,带单位的字符串形式,如"30rpx"、"30rpx 20rpx"等写法
  190. margin: {
  191. type: String,
  192. default: '0'
  193. },
  194. // 左边输入框的图标,可以为uView图标名称或图片路径
  195. searchIcon: {
  196. type: String,
  197. default: 'search'
  198. }
  199. },
  200. data() {
  201. return {
  202. keyword: '',
  203. showClear: false, // 是否显示右边的清除图标
  204. show: false,
  205. // 标记input当前状态是否处于聚焦中,如果是,才会显示右侧的清除控件
  206. focused: this.focus
  207. // 绑定输入框的值
  208. // inputValue: this.value
  209. };
  210. },
  211. watch: {
  212. keyword(nVal) {
  213. // 双向绑定值,让v-model绑定的值双向变化
  214. this.$emit('input', nVal);
  215. // 触发change事件,事件效果和v-model双向绑定的效果一样,让用户多一个选择
  216. this.$emit('change', nVal);
  217. },
  218. value: {
  219. immediate: true,
  220. handler(nVal) {
  221. this.keyword = nVal;
  222. }
  223. }
  224. },
  225. computed: {
  226. showActionBtn() {
  227. if (!this.animation && this.showAction) return true;
  228. else return false;
  229. },
  230. // 样式,根据用户传入的颜色值生成,如果不传入,默认为none
  231. borderStyle() {
  232. if (this.borderColor) return `1px solid ${this.borderColor}`;
  233. else return 'none';
  234. },
  235. },
  236. methods: {
  237. getRef(){
  238. console.log("--")
  239. return this.$refs.usearchinput
  240. },
  241. // 目前HX2.6.9 v-model双向绑定无效,故监听input事件获取输入框内容的变化
  242. inputChange(e) {
  243. this.keyword = e.detail.value;
  244. },
  245. // 清空输入
  246. // 也可以作为用户通过this.$refs形式调用清空输入框内容
  247. clear() {
  248. this.keyword = '';
  249. // 延后发出事件,避免在父组件监听clear事件时,value为更新前的值(不为空)
  250. this.$nextTick(() => {
  251. this.$emit('clear');
  252. })
  253. },
  254. // 确定搜索
  255. search(e) {
  256. this.$emit('search', e.detail.value);
  257. try{
  258. // 收起键盘
  259. uni.hideKeyboard();
  260. }catch(e){}
  261. },
  262. // 点击右边自定义按钮的事件
  263. custom() {
  264. this.$emit('custom', this.keyword);
  265. try{
  266. // 收起键盘
  267. uni.hideKeyboard();
  268. }catch(e){}
  269. },
  270. // 获取焦点
  271. getFocus() {
  272. this.focused = true;
  273. // 开启右侧搜索按钮展开的动画效果
  274. if (this.animation && this.showAction) this.show = true;
  275. this.$emit('focus', this.keyword);
  276. },
  277. // 失去焦点
  278. blur() {
  279. // 最开始使用的是监听图标@touchstart事件,自从hx2.8.4后,此方法在微信小程序出错
  280. // 这里改为监听点击事件,手点击清除图标时,同时也发生了@blur事件,导致图标消失而无法点击,这里做一个延时
  281. setTimeout(() => {
  282. this.focused = false;
  283. }, 100)
  284. this.show = false;
  285. this.$emit('blur', this.keyword);
  286. },
  287. // 点击搜索框,只有disabled=true时才发出事件,因为禁止了输入,意味着是想跳转真正的搜索页
  288. clickHandler() {
  289. if(this.disabled) this.$emit('click');
  290. }
  291. }
  292. };
  293. </script>
  294. <style lang="scss" scoped>
  295. @import "../../libs/css/style.components.scss";
  296. .u-search {
  297. @include vue-flex;
  298. align-items: center;
  299. flex: 1;
  300. }
  301. .u-content {
  302. @include vue-flex;
  303. align-items: center;
  304. padding: 0 18rpx;
  305. flex: 1;
  306. }
  307. .u-clear-icon {
  308. @include vue-flex;
  309. align-items: center;
  310. }
  311. .u-input {
  312. flex: 1;
  313. font-size: 28rpx;
  314. line-height: 1;
  315. margin: 0 10rpx;
  316. color: $u-tips-color;
  317. }
  318. .u-close-wrap {
  319. width: 40rpx;
  320. height: 100%;
  321. @include vue-flex;
  322. align-items: center;
  323. justify-content: center;
  324. border-radius: 50%;
  325. }
  326. .u-placeholder-class {
  327. color: $u-tips-color;
  328. }
  329. .u-action {
  330. font-size: 28rpx;
  331. color: $u-main-color;
  332. width: 0;
  333. overflow: hidden;
  334. transition: all 0.3s;
  335. white-space: nowrap;
  336. text-align: center;
  337. }
  338. .u-action-active {
  339. width: 80rpx;
  340. margin-left: 10rpx;
  341. }
  342. </style>