Skip to content

Commit

Permalink
fix(calendar): 修复初次打开默认值定位问题
Browse files Browse the repository at this point in the history
  • Loading branch information
eiinu committed Jan 10, 2024
1 parent 0a4788b commit 137afab
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
32 changes: 27 additions & 5 deletions src/packages/__VUE/calendar/index.taro.vue
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<template>
<nut-popup
v-if="poppable"
:visible="visible"
v-model:visible="visible"
position="bottom"
round
closeable
v-bind="$attrs"
:style="{ height: '85vh' }"
:lock-scroll="lockScroll"
:catch-move="lockScroll"
@click-overlay="closePopup"
@click-close-icon="closePopup"
:destroy-on-close="false"
@opened="opened"
>
<nut-calendar-item
ref="calendarRef"
Expand Down Expand Up @@ -90,7 +90,7 @@
</nut-calendar-item>
</template>
<script lang="ts">
import { ref, computed } from 'vue';
import { ref, computed, PropType } from 'vue';
import { createComponent } from '@/packages/utils/create';
const { create } = createComponent('calendar');
import NutCalendarItem from '../calendaritem/index.taro.vue';
Expand Down Expand Up @@ -153,7 +153,7 @@ export default create({
default: ''
},
defaultValue: {
type: [String, Array]
type: [String, Array] as PropType<string | string[]>
},
startDate: {
type: String,
Expand All @@ -176,6 +176,14 @@ export default create({
},
emits: ['choose', 'close', 'update:visible', 'select'],
setup(props, { emit, slots, expose }) {
const visible = computed({
get() {
return props.visible;
},
set(val) {
emit('update:visible', val);
}
});
const showTopBtn = computed(() => {
return slots.btn;
});
Expand Down Expand Up @@ -226,8 +234,22 @@ export default create({
emit('select', param);
};
const opened = () => {
if (props.defaultValue) {
if (Array.isArray(props.defaultValue)) {
if (props.defaultValue?.length) {
calendarRef.value?.scrollToDate(props.defaultValue?.[0]);
}
} else {
calendarRef.value?.scrollToDate(props.defaultValue);
}
}
};
return {
visible,
closePopup,
opened,
update,
close,
select,
Expand Down
19 changes: 16 additions & 3 deletions src/packages/__VUE/calendar/index.vue
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
<template>
<nut-popup
v-if="poppable"
:visible="visible"
v-model:visible="visible"
position="bottom"
round
closeable
v-bind="$attrs"
:style="{ height: '85vh' }"
:lock-scroll="lockScroll"
@click-overlay="closePopup"
@click-close-icon="closePopup"
@opened="opened"
>
<nut-calendar-item
ref="calendarRef"
Expand Down Expand Up @@ -176,6 +175,14 @@ export default create({
},
emits: ['choose', 'close', 'update:visible', 'select'],
setup(props, { emit, slots, expose }) {
const visible = computed({
get() {
return props.visible;
},
set(val) {
emit('update:visible', val);
}

Check warning on line 184 in src/packages/__VUE/calendar/index.vue

View check run for this annotation

Codecov / codecov/patch

src/packages/__VUE/calendar/index.vue#L183-L184

Added lines #L183 - L184 were not covered by tests
});
const showTopBtn = computed(() => {
return slots.btn;
});
Expand Down Expand Up @@ -226,8 +233,14 @@ export default create({
emit('select', param);
};
const opened = () => {
calendarRef.value?.initPosition();
};

Check warning on line 238 in src/packages/__VUE/calendar/index.vue

View check run for this annotation

Codecov / codecov/patch

src/packages/__VUE/calendar/index.vue#L237-L238

Added lines #L237 - L238 were not covered by tests
return {
visible,
closePopup,
opened,
update,
close,
select,
Expand Down
7 changes: 5 additions & 2 deletions src/packages/__VUE/calendaritem/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
</view>
</template>
<script lang="ts">
import { reactive, ref, watch, toRefs, computed } from 'vue';
import { reactive, ref, watch, toRefs, computed, onMounted } from 'vue';
import { createComponent } from '@/packages/utils/create';
import Utils from '@/packages/utils/date';
import requestAniFrame from '@/packages/utils/raf';
Expand Down Expand Up @@ -519,6 +519,7 @@ export default create({
// 初始化数据
const initData = () => {
console.log('initData');
// 初始化开始结束数据
let propStartDate = props.startDate ? props.startDate : Utils.getDay(0);
let propEndDate = props.endDate ? props.endDate : Utils.getDay(365);
Expand Down Expand Up @@ -810,7 +811,9 @@ export default create({
};
// 初始化数据
initData();
onMounted(() => {
initData();
});
//监听 默认值更改
watch(
Expand Down

0 comments on commit 137afab

Please sign in to comment.