uni-search-bar.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <template>
  2. <view class="uni-searchbar">
  3. <view :style="{borderRadius:radius+'px',backgroundColor: bgColor}" class="uni-searchbar__box" @click="searchClick">
  4. <!-- #ifdef MP-ALIPAY -->
  5. <view class="uni-searchbar__box-icon-search">
  6. <uni-icons color="#999999" size="18" type="search" />
  7. </view>
  8. <!-- #endif -->
  9. <!-- #ifndef MP-ALIPAY -->
  10. <uni-icons color="#999999" class="uni-searchbar__box-icon-search" size="18" type="search" />
  11. <!-- #endif -->
  12. <input v-if="show" :focus="focus" :placeholder="placeholder" :maxlength="maxlength" class="uni-searchbar__box-search-input"
  13. confirm-type="search" type="text" :value="value" @input="$emit('input', $event.target.value)" @blur="blur"/>
  14. <text v-else class="uni-searchbar__text-placeholder">{{ placeholder }}</text>
  15. <view v-if="show && (clearButton==='always'||clearButton==='auto'&&value!=='')" class="uni-searchbar__box-icon-clear" @click="clear">
  16. <uni-icons color="#999999" class="" size="24" type="clear" />
  17. </view>
  18. </view>
  19. </view>
  20. </template>
  21. <script>
  22. import uniIcons from "../uni-icons/uni-icons.vue";
  23. export default {
  24. name: "UniSearchBar",
  25. components: {
  26. uniIcons
  27. },
  28. props: {
  29. placeholder: {
  30. type: String,
  31. default: "请输入搜索内容"
  32. },
  33. radius: {
  34. type: [Number, String],
  35. default: 5
  36. },
  37. clearButton: {
  38. type: String,
  39. default: "auto"
  40. },
  41. cancelButton: {
  42. type: String,
  43. default: "auto"
  44. },
  45. cancelText: {
  46. type: String,
  47. default: '取消'
  48. },
  49. bgColor: {
  50. type: String,
  51. default: "#F8F8F8"
  52. },
  53. maxlength: {
  54. type: [Number, String],
  55. default: 100
  56. },
  57. value: {
  58. type: String,
  59. }
  60. },
  61. data() {
  62. return {
  63. show: this.value?true:false,
  64. focus: false
  65. }
  66. },
  67. methods: {
  68. searchClick() {
  69. if (this.show) {
  70. return
  71. }
  72. this.show = true;
  73. this.focus = true;
  74. },
  75. clear() {
  76. this.$emit("input", '')
  77. setTimeout(()=>{
  78. this.show = false
  79. }, 0)
  80. },
  81. blur(){
  82. this.focus = false
  83. if(!this.value) this.show = false
  84. }
  85. }
  86. };
  87. </script>
  88. <style lang="scss" scoped>
  89. $uni-searchbar-height: 36px;
  90. .uni-searchbar {
  91. /* #ifndef APP-NVUE */
  92. display: flex;
  93. /* #endif */
  94. flex-direction: row;
  95. position: relative;
  96. padding: $uni-spacing-col-base;
  97. background-color: $uni-bg-color;
  98. }
  99. .uni-searchbar__box {
  100. /* #ifndef APP-NVUE */
  101. display: flex;
  102. box-sizing: border-box;
  103. /* #endif */
  104. overflow: hidden;
  105. position: relative;
  106. flex: 1;
  107. justify-content: center;
  108. flex-direction: row;
  109. align-items: center;
  110. height: $uni-searchbar-height;
  111. padding: 5px 8px 5px 0px;
  112. border-width: 0.5px;
  113. border-style: solid;
  114. border-color: $uni-border-color;
  115. }
  116. .uni-searchbar__box-icon-search {
  117. /* #ifndef APP-NVUE */
  118. display: flex;
  119. /* #endif */
  120. flex-direction: row;
  121. width: 32px;
  122. justify-content: center;
  123. align-items: center;
  124. color: $uni-text-color-placeholder;
  125. }
  126. .uni-searchbar__box-search-input {
  127. flex: 1;
  128. font-size: $uni-font-size-base;
  129. color: $uni-text-color;
  130. }
  131. .uni-searchbar__box-icon-clear {
  132. align-items: center;
  133. line-height: 24px;
  134. padding-left: 5px;
  135. }
  136. .uni-searchbar__text-placeholder {
  137. font-size: $uni-font-size-base;
  138. color: $uni-text-color-placeholder;
  139. margin-left: 5px;
  140. }
  141. .uni-searchbar__cancel {
  142. padding-left: 10px;
  143. line-height: $uni-searchbar-height;
  144. font-size: 14px;
  145. color: $uni-text-color;
  146. }
  147. </style>