uni-search-bar.vue 5.1 KB

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