index.tsx 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { defineComponent, ref, watch } from 'vue';
  2. import styles from './index.module.less';
  3. import { NIcon, NImage, NDatePicker } from 'naive-ui';
  4. import dateIcons from './images/dateIcon.png';
  5. import disDate from './images/disDate.png';
  6. export default defineComponent({
  7. name: 'CDatePicker',
  8. props: {
  9. type: {
  10. type: String,
  11. default: 'daterange'
  12. },
  13. separator: {
  14. type: String,
  15. default: '-'
  16. },
  17. timerValue: {
  18. type: Array,
  19. default: [] as any
  20. }
  21. },
  22. setup(props, { emit, attrs }) {
  23. const isFocus = ref(false);
  24. const timer = ref(null as any);
  25. const updateTimer = () => {
  26. console.log('更新日期', timer.value);
  27. emit('update:value', timer.value);
  28. };
  29. timer.value =
  30. props.timerValue && props.timerValue.length > 0 ? props.timerValue : null;
  31. watch(
  32. () => props.timerValue,
  33. (value: any) => {
  34. timer.value = value && value.length ? value : null;
  35. }
  36. );
  37. return () => (
  38. <>
  39. <div class={styles.CdataWrap}>
  40. <NImage
  41. previewDisabled
  42. class={styles.dateIcons}
  43. src={isFocus.value ? dateIcons : disDate}></NImage>
  44. <NDatePicker
  45. {...attrs}
  46. class={styles.CDatePicker}
  47. v-model:value={timer.value}
  48. separator={props.separator}
  49. type={props.type as any}
  50. onUpdate:value={updateTimer}
  51. onFocus={() => {
  52. isFocus.value = true;
  53. }}
  54. onBlur={() => {
  55. isFocus.value = false;
  56. }}
  57. v-slots={{
  58. 'date-icon': () => (
  59. <>
  60. <span></span>
  61. </>
  62. )
  63. }}></NDatePicker>
  64. </div>
  65. </>
  66. );
  67. }
  68. });