微信小程序搜索框组件之SearchBar

╰半橙微兮° 2021-10-19 07:31 1211阅读 0赞

index.wxml

  1. <!-- 组件模板 -->
  2. <view class="wrapper">
  3. <slot></slot>
  4. <view class='tit_seabox'>
  5. <view class="tit_seabox_bar {
  6. {addflag?'tit_seabox_add':''}} {
  7. {searchflag?'tit_start_search':''}}">
  8. <icon type='search' size="32rpx"></icon>
  9. <input type="text" bindinput="searchList" bindconfirm="endsearchList" bindfocus='getfocus' bindblur="blursearch" confirm-type='search' value='{
  10. {searchstr}}' placeholder='请输入...' />
  11. <icon bindtap='activity_clear' wx:if="{
  12. {searchflag}}" type='clear' size="28rpx"></icon>
  13. </view>
  14. <view wx:if="{
  15. {searchflag}}" bindtap='cancelsearch' class="activity_seabtn">取消</view>
  16. <view class='activity_add' wx:if="{
  17. {addflag}}"><image bindtap='addhandle' src='{
  18. {addimg}}'></image></view>
  19. </view>
  20. </view>

index.js

  1. // 本组件为搜索组件
  2. // 需要传入addflag 值为true / false (搜索框右侧部分)
  3. // 若显示搜索框右侧部分 需传入右侧图标url以及addhandle函数
  4. Component({
  5. properties: {
  6. addflag: { //显示搜索框右侧部分
  7. type: Boolean,
  8. value: false,
  9. observer(newVal, oldVal, changedPath) {
  10. }
  11. },
  12. addimg: { //显示搜索框右侧部分icon
  13. type: String,
  14. value: ''
  15. },
  16. searchstr: { //input 值
  17. type: String,
  18. value: '值'
  19. },
  20. searchflag: {
  21. type: Boolean,
  22. value: false,
  23. }
  24. },
  25. /**
  26. * 组件的初始数据
  27. */
  28. data: {
  29. },
  30. /**
  31. * 组件的方法列表
  32. */
  33. methods: {
  34. //获得焦点
  35. getfocus() {
  36. this.setData({
  37. searchflag: true,
  38. })
  39. },
  40. //搜索框右侧按钮事件
  41. addhandle() {
  42. this.triggerEvent("addhandle");
  43. },
  44. //搜索输入
  45. searchList(e) {
  46. this.triggerEvent("searchList", e);
  47. },
  48. //查询
  49. endsearchList(e) {
  50. this.triggerEvent("endsearchList");
  51. },
  52. //失去焦点
  53. blursearch() {
  54. // console.log('失去焦点')
  55. },
  56. // 取消
  57. cancelsearch() {
  58. this.setData({
  59. searchflag: false,
  60. })
  61. this.triggerEvent("cancelsearch");
  62. },
  63. //清空搜索框
  64. activity_clear(e) {
  65. this.triggerEvent("activity_clear");
  66. },
  67. }
  68. })

index.wxss

  1. .tit_seabox{
  2. width: calc( 100% - 64rpx );
  3. background: #e5e5e5;
  4. height: 60rpx;
  5. padding:20rpx 32rpx;
  6. display: flex;
  7. align-items: center;
  8. overflow: hidden;
  9. }
  10. .tit_seabox_bar{
  11. width: calc( 100% - 32rpx );
  12. height: 60rpx;
  13. display: flex;
  14. align-items: center;
  15. border-radius: 13rpx;
  16. background: #ffffff;
  17. padding-left: 32rpx;
  18. }
  19. /*有权限添加活动*/
  20. .tit_seabox_bar.tit_seabox_add{
  21. width:calc( 100% - 122rpx );
  22. }
  23. /*开始搜索时*/
  24. .tit_seabox_bar.tit_start_search{
  25. width: calc( 100% - 102rpx );
  26. }
  27. /*开始搜索且有权限添加*/
  28. .tit_seabox_bar.tit_start_search.tit_seabox_add{
  29. width:calc( 100% - 192rpx );
  30. }
  31. .tit_seabox_bar icon{
  32. margin-right: 20rpx;
  33. }
  34. .tit_seabox input{
  35. height:60rpx;
  36. line-height:60rpx;
  37. font-size:28rpx;
  38. width:100%;
  39. margin-right:32rpx;
  40. }
  41. .activity_add{
  42. width:60rpx;
  43. text-align:right;
  44. border-left:4rpx solid #f2f2f2;
  45. margin-left:20rpx;
  46. }
  47. .activity_add image{
  48. width: 40rpx;
  49. height: 40rpx;
  50. }
  51. .activity_seabtn{
  52. font-size: 28rpx;
  53. width: 70rpx;
  54. text-align: right;
  55. }

发表评论

表情:
评论列表 (有 0 条评论,1211人围观)

还没有评论,来说两句吧...

相关阅读

    相关 程序 - 组件

    > 微信小程序为开发者提供了一系列基础组件,使开发者可以进行快速开发。同时还支持自定义组件,提升代码复用性,有助于代码维护。 一、基础组件 > 基础组件主要分为:视图容