uni-search-bar.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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="showSync" :placeholder="placeholder" :maxlength="maxlength" @confirm="confirm" class="uni-searchbar__box-search-input" confirm-type="search" type="text" v-model="searchVal" />
  13. <text v-else class="uni-searchbar__text-placeholder">{{ placeholder }}</text>
  14. <view v-if="show && (clearButton==='always'||clearButton==='auto'&&searchVal!=='')" class="uni-searchbar__box-icon-clear" @click="clear">
  15. <uni-icons color="#999999" class="" size="24" type="clear" />
  16. </view>
  17. </view>
  18. <text @click="cancel" class="uni-searchbar__cancel" v-if="cancelButton ==='always' || show && cancelButton ==='auto'">{{cancelText}}</text>
  19. </view>
  20. </template>
  21. <script>
  22. import uniIcons from "../uni-icons/uni-icons.vue";
  23. /**
  24. * SearchBar 搜索栏
  25. * @description 评分组件
  26. * @tutorial https://ext.dcloud.net.cn/plugin?id=866
  27. * @property {Number} radius 搜索栏圆角
  28. * @property {Number} maxlength 输入最大长度
  29. * @property {String} placeholder 搜索栏Placeholder
  30. * @property {String} clearButton = [always|auto|none] 是否显示清除按钮
  31. * @value always 一直显示
  32. * @value auto 输入框不为空时显示
  33. * @value none 一直不显示
  34. * @property {String} cancelButton = [always|auto|none] 是否显示取消按钮
  35. * @value always 一直显示
  36. * @value auto 输入框不为空时显示
  37. * @value none 一直不显示
  38. * @property {String} cancelText 取消按钮的文字
  39. * @property {String} bgColor 输入框背景颜色
  40. * @event {Function} confirm uniSearchBar 的输入框 confirm 事件,返回参数为uniSearchBar的value,e={value:Number}
  41. * @event {Function} input uniSearchBar 的 value 改变时触发事件,返回参数为uniSearchBar的value,e={value:Number}
  42. * @event {Function} cancel 点击取消按钮时触发事件,返回参数为uniSearchBar的value,e={value:Number}
  43. */
  44. export default {
  45. name: "UniSearchBar",
  46. components: {
  47. uniIcons
  48. },
  49. props: {
  50. placeholder: {
  51. type: String,
  52. default: "请输入搜索内容"
  53. },
  54. radius: {
  55. type: [Number, String],
  56. default: 5
  57. },
  58. clearButton: {
  59. type: String,
  60. default: "auto"
  61. },
  62. cancelButton: {
  63. type: String,
  64. default: "auto"
  65. },
  66. cancelText: {
  67. type: String,
  68. default: '取消'
  69. },
  70. bgColor: {
  71. type: String,
  72. default: "#F8F8F8"
  73. },
  74. maxlength: {
  75. type: [Number, String],
  76. default: 100
  77. }
  78. },
  79. data() {
  80. return {
  81. show: false,
  82. showSync: false,
  83. searchVal: ""
  84. }
  85. },
  86. watch: {
  87. searchVal() {
  88. this.$emit("input", {
  89. value: this.searchVal
  90. })
  91. }
  92. },
  93. methods: {
  94. searchClick() {
  95. if (this.show) {
  96. return
  97. }
  98. this.searchVal = ""
  99. this.show = true;
  100. this.$nextTick(() => {
  101. this.showSync = true;
  102. })
  103. },
  104. clear() {
  105. this.searchVal = ""
  106. },
  107. cancel() {
  108. this.$emit("cancel", {
  109. value: this.searchVal
  110. });
  111. this.searchVal = ""
  112. this.show = false
  113. this.showSync = false
  114. // #ifndef APP-PLUS
  115. uni.hideKeyboard()
  116. // #endif
  117. // #ifdef APP-PLUS
  118. plus.key.hideSoftKeybord()
  119. // #endif
  120. },
  121. confirm() {
  122. // #ifndef APP-PLUS
  123. uni.hideKeyboard();
  124. // #endif
  125. // #ifdef APP-PLUS
  126. plus.key.hideSoftKeybord()
  127. // #endif
  128. this.$emit("confirm", {
  129. value: this.searchVal
  130. })
  131. }
  132. }
  133. };
  134. </script>
  135. <style scoped>
  136. .uni-searchbar {
  137. /* #ifndef APP-NVUE */
  138. display: flex;
  139. /* #endif */
  140. flex-direction: row;
  141. position: relative;
  142. padding: 8px;
  143. background-color: #ffffff;
  144. }
  145. .uni-searchbar__box {
  146. /* #ifndef APP-NVUE */
  147. display: flex;
  148. box-sizing: border-box;
  149. /* #endif */
  150. overflow: hidden;
  151. position: relative;
  152. flex: 1;
  153. justify-content: center;
  154. flex-direction: row;
  155. align-items: center;
  156. height: 36px;
  157. padding: 5px 8px 5px 0px;
  158. border-width: 0.5px;
  159. border-style: solid;
  160. border-color: #e5e5e5;
  161. }
  162. .uni-searchbar__box-icon-search {
  163. /* #ifndef APP-NVUE */
  164. display: flex;
  165. /* #endif */
  166. flex-direction: row;
  167. width: 32px;
  168. justify-content: center;
  169. align-items: center;
  170. color: #808080;
  171. }
  172. .uni-searchbar__box-search-input {
  173. flex: 1;
  174. font-size: 14px;
  175. color: #333;
  176. }
  177. .uni-searchbar__box-icon-clear {
  178. align-items: center;
  179. line-height: 24px;
  180. padding-left: 5px;
  181. }
  182. .uni-searchbar__text-placeholder {
  183. font-size: 14px;
  184. color: #808080;
  185. margin-left: 5px;
  186. }
  187. .uni-searchbar__cancel {
  188. padding-left: 10px;
  189. line-height: 36px;
  190. font-size: 14px;
  191. color: #333;
  192. }
  193. </style>