index.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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'
  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. },
  51. emits: ['click'],
  52. watch: {
  53. modelValue() {
  54. this.search = this.modelValue
  55. }
  56. },
  57. data() {
  58. return {
  59. search: this.modelValue || ''
  60. }
  61. },
  62. render() {
  63. return (
  64. <div>
  65. <Search
  66. class={[styles['col-search'], styles[this.inputBackground]]}
  67. v-model={this.search}
  68. background={this.background}
  69. showAction={this.showAction}
  70. shape="round"
  71. placeholder={this.placeholder}
  72. disabled={this.disabled}
  73. autofocus={this.autofocus}
  74. onSearch={(val: string) => {
  75. this.onSearch(val)
  76. }}
  77. onClear={() => {
  78. this.search = ''
  79. this.onSearch()
  80. }}
  81. onClick={() => this.$emit('click')}
  82. v-slots={{
  83. left: () => this.$slots.left && this.$slots.left(),
  84. 'left-icon': () => <Icon name={iconSearch} size={16} />,
  85. 'right-icon': () => (
  86. <Button
  87. class={styles.searchBtn}
  88. round
  89. type="primary"
  90. size="mini"
  91. onClick={() => {
  92. this.onSearch(this.search)
  93. }}
  94. >
  95. 搜索
  96. </Button>
  97. ),
  98. action: () => (
  99. <Icon
  100. name={iconFilter}
  101. size={28}
  102. dot={this.filterDot}
  103. onClick={() => {
  104. this.onFilter()
  105. }}
  106. />
  107. )
  108. }}
  109. />
  110. </div>
  111. )
  112. }
  113. })