f5d46923a7f20e490614b157fb0f18d64da4198ac0dfb389b72463ad3afd4fceec19eac6778dc59c9e646427527a7cc7ddbb0c2e866aa26f0b8bf181a3fab9 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { ref, computed } from 'vue';
  2. import { isEqual } from 'lodash-unified';
  3. import { useLocale } from '../../../../hooks/use-locale/index.mjs';
  4. import { valueEquals, formatter, parseDate, dayOrDaysToDate } from '../utils.mjs';
  5. import { isArray } from '@vue/shared';
  6. import { UPDATE_MODEL_EVENT } from '../../../../constants/event.mjs';
  7. const useCommonPicker = (props, emit) => {
  8. const { lang } = useLocale();
  9. const pickerVisible = ref(false);
  10. const pickerActualVisible = ref(false);
  11. const userInput = ref(null);
  12. const valueIsEmpty = computed(() => {
  13. const { modelValue } = props;
  14. return !modelValue || isArray(modelValue) && !modelValue.filter(Boolean).length;
  15. });
  16. const emitInput = (input) => {
  17. if (!valueEquals(props.modelValue, input)) {
  18. let formatted;
  19. if (isArray(input)) {
  20. formatted = input.map((item) => formatter(item, props.valueFormat, lang.value));
  21. } else if (input) {
  22. formatted = formatter(input, props.valueFormat, lang.value);
  23. }
  24. const emitVal = input ? formatted : input;
  25. emit(UPDATE_MODEL_EVENT, emitVal, lang.value);
  26. }
  27. };
  28. const parsedValue = computed(() => {
  29. var _a;
  30. let dayOrDays;
  31. if (valueIsEmpty.value) {
  32. if (pickerOptions.value.getDefaultValue) {
  33. dayOrDays = pickerOptions.value.getDefaultValue();
  34. }
  35. } else {
  36. if (isArray(props.modelValue)) {
  37. dayOrDays = props.modelValue.map((d) => parseDate(d, props.valueFormat, lang.value));
  38. } else {
  39. dayOrDays = parseDate((_a = props.modelValue) != null ? _a : "", props.valueFormat, lang.value);
  40. }
  41. }
  42. if (pickerOptions.value.getRangeAvailableTime) {
  43. const availableResult = pickerOptions.value.getRangeAvailableTime(dayOrDays);
  44. if (!isEqual(availableResult, dayOrDays)) {
  45. dayOrDays = availableResult;
  46. if (!valueIsEmpty.value) {
  47. emitInput(dayOrDaysToDate(dayOrDays));
  48. }
  49. }
  50. }
  51. if (isArray(dayOrDays) && dayOrDays.some((day) => !day)) {
  52. dayOrDays = [];
  53. }
  54. return dayOrDays;
  55. });
  56. const pickerOptions = ref({});
  57. const onSetPickerOption = (e) => {
  58. pickerOptions.value[e[0]] = e[1];
  59. pickerOptions.value.panelReady = true;
  60. };
  61. const onCalendarChange = (e) => {
  62. emit("calendar-change", e);
  63. };
  64. const onPanelChange = (value, mode, view) => {
  65. emit("panel-change", value, mode, view);
  66. };
  67. const onPick = (date = "", visible = false) => {
  68. pickerVisible.value = visible;
  69. let result;
  70. if (isArray(date)) {
  71. result = date.map((_) => _.toDate());
  72. } else {
  73. result = date ? date.toDate() : date;
  74. }
  75. userInput.value = null;
  76. emitInput(result);
  77. };
  78. return {
  79. parsedValue,
  80. pickerActualVisible,
  81. pickerOptions,
  82. pickerVisible,
  83. userInput,
  84. valueIsEmpty,
  85. emitInput,
  86. onCalendarChange,
  87. onPanelChange,
  88. onPick,
  89. onSetPickerOption
  90. };
  91. };
  92. export { useCommonPicker };
  93. //# sourceMappingURL=use-common-picker.mjs.map