Skip to content

Commit

Permalink
Merge pull request #48 from Lohoyo/master
Browse files Browse the repository at this point in the history
fix: 修改原来用的 Moment.js 的可变数据写法为现在用的 Day.js 的不可变数据写法
  • Loading branch information
jinzhan authored Mar 24, 2021
2 parents 804a5a6 + ddee67d commit 7e1c88b
Show file tree
Hide file tree
Showing 28 changed files with 119 additions and 129 deletions.
4 changes: 4 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

---

## 1.0.2
`2020-3-24`
- 🐞 修复 1.0.0 版本把 Moment.js 换成 Day.js 后引起的和时间日期相关的组件的一系列 bug,涉及的组件包括 Calendar、DatePicker、LocaleProvider、TimePicker [#48](https://github.com/ecomfe/santd/pull/48)

## 1.0.1
`2020-3-23`
- Menu
Expand Down
4 changes: 1 addition & 3 deletions src/calendar/docs/customHeader.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,10 @@ export default {
},
getMonths(value) {
const options = [];
const current = value.clone();
const localeData = value.localeData();
for (let i = 0; i < 12; i++) {
current.month(i);
options.push({label: localeData.monthsShort(current), value: String(i)});
options.push({label: localeData.monthsShort(value.month(i)), value: String(i)});
}
return options;
},
Expand Down
22 changes: 9 additions & 13 deletions src/calendar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ dayjs.extend(require('dayjs/plugin/localeData'));
const prefixCls = classCreator('fullcalendar')();

function getMonthsLocale(value) {
const current = value.clone();
const localeData = value.localeData();
const months = [];
for (let i = 0; i < 12; i++) {
current.month(i);
months.push(localeData.monthsShort(current));
months.push(localeData.monthsShort(value.month(i)));
}
return months;
}
Expand Down Expand Up @@ -103,7 +101,7 @@ export default san.defineComponent({
}
},
setValue(value, way) {
const prevValue = this.data.get('value').clone();
const prevValue = this.data.get('value');
const mode = this.data.get('mode');

this.data.set('value', value);
Expand All @@ -118,14 +116,10 @@ export default san.defineComponent({
}
},
handleMonthChange(month) {
const value = this.data.get('value').clone();
value.month(month);
this.setValue(value, 'changePanel');
this.setValue(this.data.get('value').month(month), 'changePanel');
},
handleYearChange(year) {
const value = this.data.get('value').clone();
value.year(year);
this.setValue(value, 'changePanel');
this.setValue(this.data.get('value').year(year), 'changePanel');
},
handlePanelChange(value, mode) {
this.fire('panelChange', {value, mode});
Expand All @@ -137,19 +131,21 @@ export default san.defineComponent({
localeReceiver.inited.call(this);

const defaultValue = this.data.get('defaultValue');
const value = this.data.get('value') || defaultValue || dayjs();
let value = this.data.get('value') || defaultValue || dayjs();
const localeCode = this.data.get('localeCode');

if (localeCode) {
require(`dayjs/locale/${localeCode}.js`);
dayjs.locale(localeCode);
value.locale(localeCode);
value = value.locale(localeCode);
}
this.data.set('value', value);
this.data.set('hasHeaderRender', !!this.sourceSlots.named.headerRender);

this.watch('localeCode', val => {
require(`dayjs/locale/${val}.js`);
dayjs.locale(val);
value.locale(val);
value = value.locale(val);
this.data.set('value', value, {force: true});
});
},
Expand Down
11 changes: 8 additions & 3 deletions src/calendar/src/calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,13 @@ export default inherits(san.defineComponent({
const localeCode = this.data.get('localeCode');

// 如果有国际化编码,对dayjs进行国际化处理
localeCode && dayjs.locale(localeCode);
localeCode && value && value.locale(localeCode);
if (localeCode) {
require(`dayjs/locale/${localeCode}.js`);
dayjs.locale(localeCode);
if (value) {
value = value.locale(localeCode);
}
}

this.data.set('mode', mode || 'date');
this.data.set('value', value);
Expand All @@ -51,7 +56,7 @@ export default inherits(san.defineComponent({
if (!selectedValue && showTime) {
const timePickerDefaultValue = showTime.defaultValue || dayjs();
if (timePickerDefaultValue) {
syncTime(timePickerDefaultValue, value);
value = syncTime(timePickerDefaultValue, value);
}
}
this.fire('select', {value});
Expand Down
15 changes: 5 additions & 10 deletions src/calendar/src/calendar/calendarHeader.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,19 @@ export default san.defineComponent({
}
},
handlePreviousYear() {
const previous = this.data.get('value').clone();
previous.add(-1, 'years');
const previous = this.data.get('value').add(-1, 'years');
this.fire('valueChange', previous);
},
handleNextYear() {
const next = this.data.get('value').clone();
next.add(1, 'years');
const next = this.data.get('value').add(1, 'years');
this.fire('valueChange', next);
},
handlePreviousMonth() {
const previous = this.data.get('value').clone();
previous.add(-1, 'month');
const previous = this.data.get('value').add(-1, 'month');
this.fire('valueChange', previous);
},
handleNextMonth() {
const next = this.data.get('value').clone();
next.add(1, 'month');
const next = this.data.get('value').add(1, 'month');
this.fire('valueChange', next);
},
handleDecadeSelect(value) {
Expand All @@ -87,8 +83,7 @@ export default san.defineComponent({
this.fire('valueChange', value);
},
goYear(direction) {
const next = this.data.get('value').clone();
next.add(direction, 'years');
const next = this.data.get('value').add(direction, 'years');
this.fire('valueChange', next);
},
handleYearSelect(value) {
Expand Down
6 changes: 3 additions & 3 deletions src/calendar/src/date/dateInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ export default san.defineComponent({
}

// 不合法直接退出
dayjs.extend(require('dayjs/plugin/customParseFormat'));
const parsed = dayjs(showDate, format, true);
if (!parsed.isValid()) {
this.data.set('invalid', true);
this.data.set('showDate', showDate);
return;
}

const value = this.data.get('value').clone();
value.year(parsed.year())
const value = this.data.get('value').year(parsed.year())
.month(parsed.month())
.date(parsed.date())
.hour(parsed.hour())
Expand Down Expand Up @@ -104,7 +104,7 @@ export default san.defineComponent({
if (e.keyCode === KeyCode.ENTER) {
const validateDate = !disabledDate || !disabledDate(value);
if (validateDate) {
this.fire('select', value.clone());
this.fire('select', value);
}
}
},
Expand Down
19 changes: 8 additions & 11 deletions src/calendar/src/date/dateTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,10 @@ export default san.defineComponent({
const selectedValue = this.data.get('selectedValue');
const hoverValue = this.data.get('hoverValue');
const disabledDate = this.data.get('disabledDate');
const month = value.clone();
month.date(1);
const month = value.date(1);
const day = month.day();
const lastMonthDiffDay = (day + 7 - value.localeData().firstDayOfWeek()) % 7;
const lastMonth = month.clone();
lastMonth.add(0 - lastMonthDiffDay, 'days');
const lastMonth = month.add(0 - lastMonthDiffDay, 'days');
const dataTable = [];
let passed = 0;
let current;
Expand All @@ -65,8 +63,7 @@ export default san.defineComponent({
for (let j = 0; j < COL; j++) {
current = lastMonth;
if (passed) {
current = current.clone();
current.add(passed, 'days');
current = current.add(passed, 'days');
}
dataTable[i].current.push({data: current});
dataTable[i].week = current.week();
Expand All @@ -75,10 +72,10 @@ export default san.defineComponent({
let next = null;
let last = null;
if (j < COL - 1) {
next = current.clone().add(passed + 1, 'days');
next = current.add(passed + 1, 'days');
}
if (j > 0) {
last = current.clone().add(passed - 1, 'days');
last = current.add(passed - 1, 'days');
}
let className = [`${prefixCls}-cell`];
let disabled = false;
Expand Down Expand Up @@ -126,7 +123,7 @@ export default san.defineComponent({
isSameDay(current, selectedValue) && className.push(`${prefixCls}-selected-date`);
isBeforeCurrentMonthYear && className.push(`${prefixCls}-last-month-cell`);
isAfterCurrentMonthYear && className.push(`${prefixCls}-next-month-btn-day`);
(current.clone().endOf('month').date() === current.date()) && className.push(` ${prefixCls}-last-day-of-month`);
(current.endOf('month').date() === current.date()) && className.push(` ${prefixCls}-last-day-of-month`);

if (disabledDate) {
if (disabledDate(current, value)) {
Expand Down Expand Up @@ -154,11 +151,11 @@ export default san.defineComponent({
const firstDayOfWeek = localeData.firstDayOfWeek();
const weekDays = [];
const veryShortWeekdays = [];
const now = dayjs();
let now = dayjs();

for (let dateColIndex = 0; dateColIndex < COL; dateColIndex++) {
const index = (firstDayOfWeek + dateColIndex) % COL;
now.day(index);
now = now.day(index);
veryShortWeekdays[dateColIndex] = localeData.weekdaysMin(now);
weekDays[dateColIndex] = localeData.weekdaysShort(now);
}
Expand Down
3 changes: 1 addition & 2 deletions src/calendar/src/decade/decadePanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ export default san.defineComponent({
this.fire('select', value);
},
goYear(year) {
const value = this.data.get('value').clone();
value.add(year, 'year');
const value = this.data.get('value').add(year, 'year');
this.data.set('value', value);
},
components: {
Expand Down
7 changes: 2 additions & 5 deletions src/calendar/src/decade/decadeTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,11 @@ export default san.defineComponent({
}
},
goYear(year) {
const value = this.data.get('value').clone();
value.add(year, 'year');
const value = this.data.get('value').add(year, 'year');
this.data.set('value', value);
},
chooseDecade(year) {
const value = this.data.get('value').clone();
value.year(year);
value.month(this.data.get('value').month());
const value = this.data.get('value').year(year).month(this.data.get('value').month());
this.fire('select', value);
this.nextTick(() => {
this.data.set('refresh', Math.random(), {force: true});
Expand Down
12 changes: 3 additions & 9 deletions src/calendar/src/fullCalendarHeader.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,10 @@ export default san.defineComponent({
return String(this.data.get('value').year());
},
months() {
const value = this.data.get('value').clone();
const result = [];

for (let i = 0; i < 12; i++) {
value.month(i);
result.push({label: getMonthName(value), value: String(i)});
result.push({label: getMonthName(this.data.get('value').month(i)), value: String(i)});
}
return result;
},
Expand Down Expand Up @@ -67,14 +65,10 @@ export default san.defineComponent({
this.fire('typeChange', 'month');
},
handleChangeMonth(month) {
const value = this.data.get('value').clone();
value.month(parseInt(month, 10));
this.fire('valueChange', value);
this.fire('valueChange', this.data.get('value').month(parseInt(month, 10)));
},
handleChangeYear(year) {
const value = this.data.get('value').clone();
value.year(parseInt(year, 10));
this.fire('valueChange', value);
this.fire('valueChange', this.data.get('value').year(parseInt(year, 10)));
},
template: `
<div class="{{prefixCls}}-header">
Expand Down
12 changes: 4 additions & 8 deletions src/calendar/src/month/monthTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ export default san.defineComponent({
for (let rowIndex = 0; rowIndex < ROW; rowIndex++) {
months[rowIndex] = [];
for (let colIndex = 0; colIndex < COL; colIndex++) {
const current = value.clone();
current.month(index);
const current = value.month(index);
const content = getMonthName(current);
months[rowIndex][colIndex] = {
value: index,
Expand All @@ -49,8 +48,7 @@ export default san.defineComponent({

let disabled = false;
if (disabledDate) {
const testValue = value.clone();
testValue.month(monthData.value);
const testValue = value.month(monthData.value);
disabled = disabledDate(testValue);
}

Expand All @@ -66,14 +64,12 @@ export default san.defineComponent({
const disabledDate = this.data.get('disabledDate');
let disabled = false;
if (disabledDate) {
const testValue = value.clone();
testValue.month(monthData.value);
const testValue = value.month(monthData.value);
disabled = disabledDate(testValue);
}

if (!disabled) {
const next = value.clone();
next.month(monthData.value);
const next = value.month(monthData.value);
this.fire('select', next);
}
},
Expand Down
9 changes: 7 additions & 2 deletions src/calendar/src/monthCalendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,19 @@ export default inherits(san.defineComponent({
let selectedValue = this.data.get('selectedValue') || this.data.get('defaultSelectedValue');
const localeCode = this.data.get('localeCode');

localeCode && value.locale(localeCode);
if (localeCode) {
require(`dayjs/locale/${localeCode}.js`);
value = value.locale(localeCode);
}

this.data.set('value', value);
this.data.set('selectedValue', selectedValue);
this.data.set('mode', 'month');

this.watch('selectedValue', val => {
localeCode && val.locale(localeCode);
if (localeCode) {
val = val.locale(localeCode);
}
this.data.set('value', val);
});
},
Expand Down
2 changes: 1 addition & 1 deletion src/calendar/src/range/rangePanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default san.defineComponent({
dateInputValue(selectedValue = []) {
const direction = this.data.get('direction');
const value = selectedValue[direction === 'left' ? 0 : 1];
return value && value.clone();
return value;
},
getTimeConfig(selectedValue, disabledTime, mode) {
const showTimePicker = this.data.get('showTimePicker');
Expand Down
Loading

0 comments on commit 7e1c88b

Please sign in to comment.