cl-search.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <template>
  2. <view class="cl-search" :style="[customStyle]">
  3. <view class="cl-search__prepend" v-if="$slots.prepend">
  4. <slot name="prepend"></slot>
  5. </view>
  6. <view class="cl-search__input">
  7. <cl-input
  8. :prefix-icon="searchIcon"
  9. v-model="value2"
  10. :type="type"
  11. :placeholder="placeholder"
  12. :disabled="disabled"
  13. :clearable="clearable"
  14. :focus="focus2"
  15. :placeholder-style="placeholderStyle"
  16. :placeholder-class="placeholderClass"
  17. :maxlength="maxlength"
  18. :cursor-spacing="cursorSpacing"
  19. :confirm-type="confirmType"
  20. :confirm-hold="confirmHold"
  21. :adjust-position="adjustPosition"
  22. :holdKeyboard="holdKeyboard"
  23. @input="onInput"
  24. @focus="onFocus"
  25. @blur="onBlur"
  26. @confirm="onConfirm"
  27. @clear="onClear"
  28. @keyboardheightchange="onKeyboardheightchange"
  29. ></cl-input>
  30. <cl-button type="primary" round v-if="showSearchButton" @tap="search">
  31. {{ searchButtonText }}
  32. </cl-button>
  33. </view>
  34. <view class="cl-search__append" v-if="$slots.append">
  35. <slot name="append"></slot>
  36. </view>
  37. </view>
  38. </template>
  39. <script>
  40. /**
  41. * search 搜索框
  42. * @description 搜索框,可自定义前后内容
  43. * @tutorial https://docs.cool-js.com/uni/components/operate/search.html
  44. * @example <cl-search v-model="val" />
  45. */
  46. export default {
  47. props: {
  48. value: [String, Number],
  49. type: {
  50. type: String,
  51. default: "text",
  52. },
  53. password: Boolean,
  54. placeholder: String,
  55. placeholderStyle: String,
  56. placeholderClass: String,
  57. disabled: Boolean,
  58. focus: Boolean,
  59. clearable: {
  60. type: Boolean,
  61. default: true,
  62. },
  63. maxlength: {
  64. type: Number,
  65. default: 140,
  66. },
  67. cursorSpacing: {
  68. type: Number,
  69. default: 0,
  70. },
  71. confirmType: {
  72. type: String,
  73. default: "done",
  74. },
  75. confirmHold: Boolean,
  76. adjustPosition: {
  77. type: Boolean,
  78. default: true,
  79. },
  80. holdKeyboard: {
  81. type: Boolean,
  82. default: false,
  83. },
  84. searchIcon: {
  85. type: String,
  86. default: "cl-icon-search",
  87. },
  88. showSearchButton: {
  89. type: Boolean,
  90. default: true,
  91. },
  92. searchButtonText: {
  93. type: String,
  94. default: "搜索",
  95. },
  96. customStyle: Object,
  97. },
  98. data() {
  99. return {
  100. value2: null,
  101. focus2: false,
  102. };
  103. },
  104. watch: {
  105. value: {
  106. immediate: true,
  107. handler(val) {
  108. this.value2 = val;
  109. },
  110. },
  111. focus: {
  112. immediate: true,
  113. handler(val) {
  114. this.focus2 = val;
  115. },
  116. },
  117. },
  118. methods: {
  119. onInput(e) {
  120. this.$emit("input", e);
  121. },
  122. onFocus(e) {
  123. this.$emit("focus", e);
  124. },
  125. onBlur(e) {
  126. this.$emit("blur", e);
  127. },
  128. onConfirm(e) {
  129. this.$emit("confirm", e);
  130. this.search();
  131. },
  132. onClear() {
  133. this.search();
  134. },
  135. onKeyboardheightchange(e) {
  136. this.$emit("keyboardheightchange", e);
  137. },
  138. search() {
  139. this.$emit("search", this.value2);
  140. },
  141. },
  142. };
  143. </script>