cm-simple-search.vue 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <template>
  2. <view class="simple-search">
  3. <view class="search-control">
  4. <text class="iconfont icon-sousuo"></text>
  5. <input
  6. class="control"
  7. type="text"
  8. v-model="inputValue"
  9. :placeholder="placeholder"
  10. placeholder-class="placeholder"
  11. @change="$emit('input', $event.target.value)"
  12. @input="$emit('input', $event.target.value)"
  13. @confirm="onConfirm"
  14. :confirm-hold="true"
  15. confirm-type="search"
  16. />
  17. <text class="iconfont icon-quxiao" @click="onCancel" v-if="inputValue"></text>
  18. </view>
  19. <view class="cancel" v-if="isSearch" @click="onCancel">取消</view>
  20. </view>
  21. </template>
  22. <script>
  23. export default {
  24. name: 'cm-simple-search',
  25. props: {
  26. value: {
  27. type: String,
  28. default: ''
  29. },
  30. placeholder: {
  31. type: String,
  32. default: '搜索商品'
  33. }
  34. },
  35. data() {
  36. return {
  37. isSearch: false,
  38. inputValue: ''
  39. }
  40. },
  41. created() {
  42. this.inputValue = this.value
  43. },
  44. methods: {
  45. onConfirm() {
  46. if (!this.inputValue) return
  47. this.$emit('search')
  48. this.isSearch = true
  49. },
  50. onCancel() {
  51. this.$emit('cancel')
  52. this.isSearch = false
  53. this.inputValue = ''
  54. }
  55. }
  56. }
  57. </script>
  58. <style lang="scss" scoped>
  59. .simple-search {
  60. @extend .cm-flex-center;
  61. height: 100rpx;
  62. padding: 0 24rpx;
  63. box-sizing: border-box;
  64. background-color: #fff;
  65. .search-control {
  66. @extend .cm-flex-between;
  67. flex: 1;
  68. height: 66rpx;
  69. line-height: 66rpx;
  70. box-sizing: border-box;
  71. background: #f7f7f7;
  72. border-radius: 33px;
  73. .control {
  74. flex: 1;
  75. color: #333;
  76. font-size: 28rpx;
  77. }
  78. .iconfont {
  79. @extend .cm-flex-center;
  80. width: 66rpx;
  81. color: #999;
  82. }
  83. }
  84. .cancel {
  85. width: 80rpx;
  86. line-height: 66rpx;
  87. text-align: center;
  88. font-size: 28rpx;
  89. color: #333;
  90. }
  91. }
  92. </style>