2
0
mirror of https://github.com/tenrok/vue2-datepicker.git synced 2026-06-22 23:50:36 +03:00

fix: prop lang should be reactive (#391)

This commit is contained in:
mengxiong10
2019-12-05 11:01:57 +08:00
parent e4cf24c991
commit 3c76fc36d7
10 changed files with 94 additions and 67 deletions
+8 -1
View File
@@ -30,7 +30,14 @@ describe('Locale', () => {
}, },
}); });
expect(wrapper.find('.mx-table-date th').text()).toBe('Su'); expect(wrapper.find('.mx-table-date th').text()).toBe('Su');
expect(wrapper.find('.mx-table-date td').element.title).toBe('Sep 29, 2019'); expect(wrapper.find('.mx-table-month td').text()).toBe('Jan');
expect(wrapper.find('.mx-btn-current-month').text()).toBe('Oct');
expect(wrapper.find('.mx-table-date .active').element.title).toBe('Oct 10, 2019');
wrapper.setProps({ lang: 'zh-cn' });
expect(wrapper.find('.mx-table-date th').text()).toBe('一');
expect(wrapper.find('.mx-table-month td').text()).toBe('1月');
expect(wrapper.find('.mx-btn-current-month').text()).toBe('10月');
expect(wrapper.find('.mx-table-date .active').element.title).toBe('10月 10, 2019');
}); });
it('prop: lang - object', () => { it('prop: lang - object', () => {
+10 -3
View File
@@ -106,12 +106,12 @@ import {
startOfMonth, startOfMonth,
startOfDay, startOfDay,
} from 'date-fns'; } from 'date-fns';
import localeMixin from '../mixin/locale'; import { format } from 'date-format-parse';
import formatMixin from '../mixin/format';
import { getValidDate, isValidDate, createDate } from '../util/date'; import { getValidDate, isValidDate, createDate } from '../util/date';
import TableDate from './table-date'; import TableDate from './table-date';
import TableMonth from './table-month'; import TableMonth from './table-month';
import TableYear from './table-year'; import TableYear from './table-year';
import { getLocaleFieldValue } from '../locale';
export default { export default {
name: 'CalendarPanel', name: 'CalendarPanel',
@@ -120,7 +120,11 @@ export default {
TableMonth, TableMonth,
TableYear, TableYear,
}, },
mixins: [localeMixin, formatMixin], inject: {
t: {
default: () => getLocaleFieldValue,
},
},
props: { props: {
value: {}, value: {},
defaultValue: { defaultValue: {
@@ -222,6 +226,9 @@ export default {
}, },
}, },
methods: { methods: {
formatDate(date, fmt) {
return format(date, fmt, { locale: this.t('formatLocale') });
},
initCalendar() { initCalendar() {
let calendarDate = this.calendar; let calendarDate = this.calendar;
if (!isValidDate(calendarDate)) { if (!isValidDate(calendarDate)) {
+11 -7
View File
@@ -27,18 +27,19 @@
</template> </template>
<script> <script>
import { getWeek } from 'date-format-parse'; import { getWeek, format } from 'date-format-parse';
import localeMixin from '../mixin/locale';
import formatMixin from '../mixin/format';
import { chunk } from '../util/base'; import { chunk } from '../util/base';
import { createDate } from '../util/date'; import { createDate } from '../util/date';
import { getLocaleFieldValue } from '../locale';
export default { export default {
name: 'TableDate', name: 'TableDate',
mixins: [localeMixin, formatMixin],
inject: { inject: {
t: {
default: () => getLocaleFieldValue,
},
getWeek: { getWeek: {
default: getWeek, default: () => getWeek,
}, },
}, },
props: { props: {
@@ -116,6 +117,9 @@ export default {
}, },
}, },
methods: { methods: {
formatDate(date, fmt) {
return format(date, fmt, { locale: this.t('formatLocale') });
},
handleCellClick(evt) { handleCellClick(evt) {
let { target } = evt; let { target } = evt;
if (target.tagName === 'DIV') { if (target.tagName === 'DIV') {
@@ -129,9 +133,9 @@ export default {
getCellTitle(day) { getCellTitle(day) {
const year = this.calendarYear; const year = this.calendarYear;
const month = this.calendarMonth; const month = this.calendarMonth;
const format = this.titleFormat; const fmt = this.titleFormat;
const date = createDate(year, month, day); const date = createDate(year, month, day);
return this.formatDate(date, format); return this.formatDate(date, fmt);
}, },
getWeekNumber(day) { getWeekNumber(day) {
const year = this.calendarYear; const year = this.calendarYear;
+6 -2
View File
@@ -15,12 +15,16 @@
</template> </template>
<script> <script>
import localeMixin from '../mixin/locale';
import { chunk } from '../util/base'; import { chunk } from '../util/base';
import { getLocaleFieldValue } from '../locale';
export default { export default {
name: 'TableMonth', name: 'TableMonth',
mixins: [localeMixin], inject: {
t: {
default: () => getLocaleFieldValue,
},
},
props: { props: {
getCellClasses: { getCellClasses: {
type: Function, type: Function,
+5 -2
View File
@@ -89,7 +89,7 @@
import { parse, format, getWeek } from 'date-format-parse'; import { parse, format, getWeek } from 'date-format-parse';
import { isValidDate, isValidRangeDate, getValidDate } from './util/date'; import { isValidDate, isValidRangeDate, getValidDate } from './util/date';
import { pick, isObject, mergeDeep } from './util/base'; import { pick, isObject, mergeDeep } from './util/base';
import { getLocale } from './locale'; import { getLocale, getLocaleFieldValue } from './locale';
import Popup from './popup'; import Popup from './popup';
import IconCalendar from './icon/icon-calendar'; import IconCalendar from './icon/icon-calendar';
import IconClose from './icon/icon-close'; import IconClose from './icon/icon-close';
@@ -120,7 +120,7 @@ export default {
}, },
provide() { provide() {
return { return {
locale: this.locale, t: this.getLocaleFieldValue,
getWeek: this.getWeek, getWeek: this.getWeek,
}; };
}, },
@@ -441,6 +441,9 @@ export default {
hasSlot(name) { hasSlot(name) {
return !!(this.$slots[name] || this.$scopedSlots[name]); return !!(this.$slots[name] || this.$scopedSlots[name]);
}, },
getLocaleFieldValue(path) {
return getLocaleFieldValue(path, this.locale);
},
}, },
}; };
</script> </script>
+27
View File
@@ -20,6 +20,33 @@ export function locale(name, object, isLocal) {
return locales[name] || locales[defaultLocale]; return locales[name] || locales[defaultLocale];
} }
/**
* get locale object
* @param {string} name lang
*/
export function getLocale(name) { export function getLocale(name) {
return locale(name, null, true); return locale(name, null, true);
} }
/**
* get locale field value
* @param {string} field field eg: 'formatLocale.shortMonth'
* @param {object} lang locale object
*/
export function getLocaleFieldValue(field, lang) {
const arr = (field || '').split('.');
let current = lang || getLocale();
let value;
for (let i = 0, len = arr.length; i < len; i++) {
const prop = arr[i];
value = current[prop];
if (i === len - 1) {
return value;
}
if (!value) {
return null;
}
current = value;
}
return null;
}
-11
View File
@@ -1,11 +0,0 @@
import { format } from 'date-format-parse';
import LocaleMixin from './locale';
export default {
mixins: [LocaleMixin],
methods: {
formatDate(date, fmt) {
return format(date, fmt, { locale: this.t('formatLocale') });
},
},
};
-28
View File
@@ -1,28 +0,0 @@
import { getLocale } from '../locale';
export default {
inject: {
locale: {
default: getLocale(),
},
},
methods: {
t(path) {
const arr = path.split('.');
let current = this.locale || getLocale();
let value;
for (let i = 0, len = arr.length; i < len; i++) {
const prop = arr[i];
value = current[prop];
if (i === len - 1) {
return value;
}
if (!value) {
return null;
}
current = value;
}
return null;
},
},
};
+12 -5
View File
@@ -13,10 +13,10 @@
</template> </template>
<script> <script>
import localeMixin from '../mixin/locale'; import { format } from 'date-format-parse';
import formatMixin from '../mixin/format';
import ScrollbarVertical from '../scrollbar/scrollbar-vertical'; import ScrollbarVertical from '../scrollbar/scrollbar-vertical';
import { getScrollParent } from '../util/dom'; import { getScrollParent } from '../util/dom';
import { getLocaleFieldValue } from '../locale';
function parseOption(time = '') { function parseOption(time = '') {
const values = time.split(':'); const values = time.split(':');
@@ -40,7 +40,11 @@ const scrollTo = (element, to) => {
export default { export default {
name: 'ListOptions', name: 'ListOptions',
components: { ScrollbarVertical }, components: { ScrollbarVertical },
mixins: [localeMixin, formatMixin], inject: {
t: {
default: () => getLocaleFieldValue,
},
},
props: { props: {
date: Date, date: Date,
options: { options: {
@@ -68,7 +72,7 @@ export default {
const start = parseOption(options.start); const start = parseOption(options.start);
const end = parseOption(options.end); const end = parseOption(options.end);
const step = parseOption(options.step); const step = parseOption(options.step);
const format = options.format || this.format; const fmt = options.format || this.format;
if (start && end && step) { if (start && end && step) {
const startMinutes = start.minutes + start.hours * 60; const startMinutes = start.minutes + start.hours * 60;
const endMinutes = end.minutes + end.hours * 60; const endMinutes = end.minutes + end.hours * 60;
@@ -81,7 +85,7 @@ export default {
const value = new Date(this.date).setHours(hours, minutes, 0); const value = new Date(this.date).setHours(hours, minutes, 0);
result.push({ result.push({
value, value,
text: this.formatDate(value, format), text: this.formatDate(value, fmt),
}); });
} }
} }
@@ -92,6 +96,9 @@ export default {
this.scrollToSelected(); this.scrollToSelected();
}, },
methods: { methods: {
formatDate(date, fmt) {
return format(date, fmt, { locale: this.t('formatLocale') });
},
scrollToSelected() { scrollToSelected() {
const element = this.$el.querySelector('.active'); const element = this.$el.querySelector('.active');
if (!element) return; if (!element) return;
+15 -8
View File
@@ -36,16 +36,20 @@
</template> </template>
<script> <script>
import { format } from 'date-format-parse';
import { getValidDate } from '../util/date'; import { getValidDate } from '../util/date';
import localeMixin from '../mixin/locale';
import formatMixin from '../mixin/format';
import ListColumns from './list-columns'; import ListColumns from './list-columns';
import ListOptions from './list-options'; import ListOptions from './list-options';
import { getLocaleFieldValue } from '../locale';
export default { export default {
name: 'TimePanel', name: 'TimePanel',
components: { ListColumns, ListOptions }, components: { ListColumns, ListOptions },
mixins: [localeMixin, formatMixin], inject: {
t: {
default: () => getLocaleFieldValue,
},
},
props: { props: {
value: {}, value: {},
defaultValue: { defaultValue: {
@@ -124,12 +128,12 @@ export default {
return typeof this.format === 'string' ? this.format : 'HH:mm:ss'; return typeof this.format === 'string' ? this.format : 'HH:mm:ss';
}, },
ShowHourMinuteSecondAMPM() { ShowHourMinuteSecondAMPM() {
const format = this.innerForamt; const fmt = this.innerForamt;
const defaultProps = { const defaultProps = {
showHour: /[HhKk]/.test(format), showHour: /[HhKk]/.test(fmt),
showMinute: /m/.test(format), showMinute: /m/.test(fmt),
showSecond: /s/.test(format), showSecond: /s/.test(fmt),
use12h: /a/i.test(format), use12h: /a/i.test(fmt),
}; };
const obj = {}; const obj = {};
Object.keys(defaultProps).forEach(key => { Object.keys(defaultProps).forEach(key => {
@@ -139,6 +143,9 @@ export default {
}, },
}, },
methods: { methods: {
formatDate(date, fmt) {
return format(date, fmt, { locale: this.t('formatLocale') });
},
isDisabled(date) { isDisabled(date) {
return this.disabledTime(new Date(date)); return this.disabledTime(new Date(date));
}, },