index.tsx 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. },
  15. emits: ['search'],
  16. setup(props, { emit }) {
  17. const searchData = reactive({
  18. value: ''
  19. });
  20. return () => (
  21. <NInput
  22. class={styles.TheSearch}
  23. round={props.round}
  24. placeholder="请输入搜索关键词"
  25. clearable
  26. v-model:value={searchData.value}
  27. onClear={() =>
  28. emit('search', searchData.value ? searchData.value.trim() : '')
  29. }
  30. onKeyup={(e: KeyboardEvent) => {
  31. e.stopPropagation();
  32. if (e.code === 'Enter') {
  33. emit('search', searchData.value ? searchData.value.trim() : '');
  34. }
  35. }}>
  36. {{
  37. prefix: () => (
  38. <>
  39. <img class={styles.default} src={icon_search} />
  40. <img class={styles.active} src={icon_searchActive} />
  41. </>
  42. ),
  43. suffix: () => (
  44. <NButton
  45. size="small"
  46. round
  47. type="primary"
  48. onClick={() =>
  49. emit('search', searchData.value ? searchData.value.trim() : '')
  50. }>
  51. 搜索
  52. </NButton>
  53. )
  54. }}
  55. </NInput>
  56. );
  57. }
  58. });