index.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { Button, Icon, Search } from 'vant'
  2. import { defineComponent, PropType } from 'vue'
  3. import styles from './index.module.less'
  4. import iconSearch from '@common/images/icon_search.png'
  5. import iconFilter from '@common/images/icon_filter.png'
  6. type inputBackground = 'default' | 'white' | 'transparent'
  7. export default defineComponent({
  8. name: 'ColSearch',
  9. props: {
  10. modelValue: {
  11. type: String,
  12. default: ''
  13. },
  14. showAction: {
  15. type: Boolean,
  16. default: false
  17. },
  18. disabled: {
  19. type: Boolean,
  20. default: false
  21. },
  22. autofocus: {
  23. type: Boolean,
  24. default: false
  25. },
  26. placeholder: {
  27. type: String,
  28. default: '请输入搜索关键词'
  29. },
  30. background: {
  31. type: String,
  32. default: '#fff'
  33. },
  34. inputBackground: {
  35. type: String as PropType<inputBackground>,
  36. default: 'default'
  37. },
  38. onSearch: {
  39. type: Function,
  40. default: (val: any) => {}
  41. },
  42. onFilter: {
  43. type: Function,
  44. default: () => {}
  45. },
  46. filterDot: {
  47. type: Boolean,
  48. default: false
  49. },
  50. leftIcon: {
  51. type: String,
  52. default: iconSearch
  53. }
  54. },
  55. emits: ['click'],
  56. watch: {
  57. modelValue() {
  58. this.search = this.modelValue
  59. }
  60. },
  61. data() {
  62. return {
  63. search: this.modelValue || ''
  64. }
  65. },
  66. render() {
  67. return (
  68. <div>
  69. <Search
  70. class={[styles['col-search'], styles[this.inputBackground]]}
  71. v-model={this.search}
  72. background={this.background}
  73. showAction={this.showAction}
  74. shape="round"
  75. placeholder={this.placeholder}
  76. disabled={this.disabled}
  77. autofocus={this.autofocus}
  78. onSearch={(val: string) => {
  79. this.onSearch(val)
  80. }}
  81. onClear={() => {
  82. this.search = ''
  83. this.onSearch()
  84. }}
  85. onClick={() => this.$emit('click')}
  86. v-slots={{
  87. left: () => this.$slots.left && this.$slots.left(),
  88. 'left-icon': () => <Icon name={this.leftIcon} size={16} />,
  89. 'right-icon': () => (
  90. <Button
  91. class={styles.searchBtn}
  92. round
  93. type="primary"
  94. size="mini"
  95. onClick={() => {
  96. this.onSearch(this.search)
  97. }}
  98. >
  99. 搜索
  100. </Button>
  101. ),
  102. action: () => (
  103. <Icon
  104. name={iconFilter}
  105. size={28}
  106. dot={this.filterDot}
  107. onClick={() => {
  108. this.onFilter()
  109. }}
  110. />
  111. )
  112. }}
  113. />
  114. </div>
  115. )
  116. }
  117. })