index.tsx 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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: string) => {}
  41. },
  42. onFilter: {
  43. type: Function,
  44. default: () => {}
  45. },
  46. filterDot: {
  47. type: Boolean,
  48. default: false
  49. }
  50. },
  51. watch: {
  52. modelValue() {
  53. this.search = this.modelValue
  54. }
  55. },
  56. data() {
  57. return {
  58. search: this.modelValue || ''
  59. }
  60. },
  61. render() {
  62. return (
  63. <Search
  64. class={[styles['col-search'], styles[this.inputBackground]]}
  65. v-model={this.search}
  66. background={this.background}
  67. showAction={this.showAction}
  68. shape="round"
  69. placeholder={this.placeholder}
  70. disabled={this.disabled}
  71. autofocus={this.autofocus}
  72. onSearch={(val: string) => {
  73. this.onSearch(val)
  74. }}
  75. v-slots={{
  76. 'left-icon': () => <Icon name={iconSearch} size={16} />,
  77. 'right-icon': () => (
  78. <Button
  79. class={styles.searchBtn}
  80. round
  81. type="primary"
  82. size="mini"
  83. onClick={() => {
  84. this.onSearch(this.search)
  85. }}
  86. >
  87. 搜索
  88. </Button>
  89. ),
  90. action: () => (
  91. <Icon
  92. name={iconFilter}
  93. size={28}
  94. dot={this.filterDot}
  95. onClick={() => {
  96. this.onFilter()
  97. }}
  98. />
  99. )
  100. }}
  101. />
  102. )
  103. }
  104. })