search.vue 9.0 KB

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