fs-search.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <template>
  2. <view class="fs-search-box" :style="{backgroundColor: bgColor}">
  3. <view class="fs-input-box" :class="[{round}]" @click="handleLink" :style="{backgroundColor: inputBgColor}">
  4. <view class="sub fs-input" v-if="link">{{placeholder}}</view>
  5. <input
  6. v-else
  7. class="fs-input"
  8. :value="modelValue"
  9. :type="type"
  10. :placeholder="placeholder"
  11. @input="handleChange"
  12. @focus="handleFocus"
  13. @blur="handleBlur"
  14. :focus="autoFocus"
  15. />
  16. <view class="fs-icon fs-icon-search">
  17. <slot name="icon">
  18. <fs-icon type="icon-search" color="#666666" size="32rpx"></fs-icon>
  19. </slot>
  20. </view>
  21. <view class="fs-icon fs-icon-close" v-if="modelValue" @click="handleClear">
  22. <fs-icon type="icon-close" color="#666666"></fs-icon>
  23. </view>
  24. </view>
  25. <view
  26. v-if="showAction"
  27. class="fs-cancel"
  28. :class="[actionColorType]"
  29. :style="{color:actionColor}"
  30. @click="handleAction"
  31. >
  32. {{actionText}}
  33. </view>
  34. </view>
  35. </template>
  36. <script setup>
  37. import { ref } from 'vue'
  38. const props = defineProps({
  39. placeholder: {
  40. type: String,
  41. default: '搜索'
  42. },
  43. actionColor: String,
  44. actionColorType: {
  45. type: String,
  46. validator(value) {
  47. return ['primary', 'success', 'info', 'warning', 'danger'].includes(value)
  48. }
  49. },
  50. actionText: {
  51. type: String,
  52. default: '取消'
  53. },
  54. autoFocus: Boolean,
  55. showAction: Boolean,
  56. round: Boolean,
  57. type: {
  58. type: String,
  59. default: 'text'
  60. },
  61. bgColor: {
  62. type: String,
  63. default: '#fff'
  64. },
  65. inputBgColor: {
  66. type: String,
  67. default: '#f0f0f0'
  68. },
  69. link: String,
  70. linkType: {
  71. type: String,
  72. default: 'navigateTo'
  73. },
  74. modelValue: String
  75. })
  76. const emits = defineEmits(['action', 'focus', 'blur', 'update:modelValue','change'])
  77. const handleChange = (e) => {
  78. emits('update:modelValue', e.detail.value)
  79. emits('change', e.detail.value)
  80. }
  81. const handleFocus = () => {
  82. emits('focus')
  83. }
  84. const handleBlur = () => {
  85. emits('blur')
  86. }
  87. const handleClear = () => {
  88. emits('update:modelValue', '')
  89. emits('change', '')
  90. }
  91. const handleAction = () => {
  92. emits('action', props.modelValue)
  93. }
  94. const handleLink = () => {
  95. if (props.link) {
  96. uni[props.linkType]({
  97. url: props.link
  98. })
  99. }
  100. }
  101. </script>
  102. <style lang="scss" scoped>
  103. .fs-search-box{
  104. width: 100%;
  105. height: 110rpx;
  106. padding: 20rpx var(--gutter);
  107. display: flex;
  108. background-color: #fff;
  109. box-sizing: border-box;
  110. align-items: center;
  111. }
  112. .fs-input-box {
  113. position: relative;
  114. height: 100%;
  115. width: 100%;
  116. flex: 1;
  117. background-color: #f0f0f0;
  118. .sub{
  119. line-height: 70rpx;
  120. color: var(--sub);
  121. }
  122. &.round{
  123. border-radius: 20px;
  124. .fs-input{
  125. border-radius: inherit;
  126. }
  127. }
  128. }
  129. .fs-input{
  130. height: 100%;
  131. width: 100%;
  132. padding-left: 62rpx;
  133. padding-right: 60rpx;
  134. border-radius: 6rpx;
  135. box-sizing: border-box;
  136. outline: none;
  137. /* #ifdef H5 */
  138. background-color: inherit;
  139. border: none;
  140. /* #endif */
  141. }
  142. .fs-cancel{
  143. margin-left: 20rpx;
  144. }
  145. .fs-icon{
  146. position: absolute;
  147. top: 50%;
  148. transform: translateY(-50%);
  149. color: var(--sub);
  150. z-index: 10;
  151. }
  152. .fs-icon-search{
  153. left: 20rpx;
  154. line-height: 1;
  155. }
  156. .fs-icon-close{
  157. right: 20rpx;
  158. }
  159. </style>