index.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { PropType, defineComponent, reactive } from 'vue';
  2. import styles from './index.module.less';
  3. import { InputProps, NButton, NInput } from 'naive-ui';
  4. import icon_search from '/src/common/images/icon_search.svg';
  5. import icon_searchActive from '/src/common/images/icon_searchActive.svg';
  6. export default defineComponent({
  7. name: 'TheSearch',
  8. props: {
  9. /** 圆角 */
  10. round: {
  11. type: Boolean as PropType<InputProps['round']>,
  12. default: false
  13. },
  14. border: {
  15. type: Boolean,
  16. default: true
  17. }
  18. },
  19. emits: ['search'],
  20. setup(props, { emit }) {
  21. const searchData = reactive({
  22. value: ''
  23. });
  24. return () => (
  25. <NInput
  26. class={[styles.TheSearch, props.border ? '' : styles.noBorder]}
  27. round={props.round}
  28. placeholder="请输入搜索关键词"
  29. clearable
  30. v-model:value={searchData.value}
  31. onClear={() => emit('search', '')}
  32. onKeyup={(e: KeyboardEvent) => {
  33. e.stopPropagation();
  34. if (e.code === 'Enter') {
  35. emit('search', searchData.value ? searchData.value.trim() : '');
  36. }
  37. }}>
  38. {{
  39. prefix: () => (
  40. <>
  41. <img class={styles.default} src={icon_search} />
  42. <img class={styles.active} src={icon_searchActive} />
  43. </>
  44. ),
  45. suffix: () => (
  46. <NButton
  47. size="small"
  48. round
  49. type="primary"
  50. onClick={() =>
  51. emit('search', searchData.value ? searchData.value.trim() : '')
  52. }>
  53. 搜索
  54. </NButton>
  55. )
  56. }}
  57. </NInput>
  58. );
  59. }
  60. });