Skip to content

Commit

Permalink
refactor(comp:locales): use useGlobalConfig instead of useI18n (#764)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `useI18n` was removed.
  • Loading branch information
danranVm authored Feb 24, 2022
1 parent 825066e commit 4f50728
Show file tree
Hide file tree
Showing 58 changed files with 821 additions and 1,110 deletions.
2 changes: 1 addition & 1 deletion .ls-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ ignore:
- packages/pro/node_modules
- packages/site/node_modules
- packages/cdk/forms/src/messages
- packages/components/i18n/src/locales
- packages/components/locales/src/langs
- packages/site/components.d.ts
3 changes: 1 addition & 2 deletions packages/components/_private/date-panel/src/DatePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import { computed, defineComponent, provide } from 'vue'

import { useDateConfig, useGlobalConfig } from '@idux/components/config'
import { getLocale } from '@idux/components/i18n'

import { useActiveDate } from './composables/useActiveDate'
import { useActiveType } from './composables/useActiveType'
Expand All @@ -21,8 +20,8 @@ import { datePanelProps } from './types'
export default defineComponent({
props: datePanelProps,
setup(props, { slots }) {
const locale = getLocale('datePicker')
const common = useGlobalConfig('common')
const locale = useGlobalConfig('locale')
const mergedPrefixCls = computed(() => `${common.prefixCls}-date-panel`)
const dateConfig = useDateConfig()
const { activeType, setActiveType } = useActiveType(props)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import type { DatePanelType } from '../types'
import type { DateConfig } from '@idux/components/config'
import type { DatePickerLocale } from '@idux/components/i18n'
import type { Locale } from '@idux/components/locales'
import type { ComputedRef } from 'vue'

import { computed, defineComponent, inject } from 'vue'
Expand Down Expand Up @@ -44,7 +44,7 @@ export default defineComponent({
}

return () => {
const { previousDecade, previousYear, previousMonth, nextDecade, nextYear, nextMonth } = locale.value
const { previousDecade, previousYear, previousMonth, nextDecade, nextYear, nextMonth } = locale.datePicker
const currType = activeType.value
const superPrev = true
const prev = !hidePrevNextTypes.includes(activeType.value)
Expand Down Expand Up @@ -109,7 +109,7 @@ export default defineComponent({
function useContents(
activeType: ComputedRef<DatePanelType>,
activeDate: ComputedRef<Date>,
locale: ComputedRef<DatePickerLocale>,
locale: Locale,
dateConfig: DateConfig,
setActiveType: (type: DatePanelType) => void,
) {
Expand All @@ -120,7 +120,7 @@ function useContents(
return computed(() => {
const currType = activeType.value
const currDate = activeDate.value
const { yearSelect, monthSelect, yearFormat, monthFormat } = locale.value
const { yearSelect, monthSelect, yearFormat, monthFormat } = locale.datePicker
const { format, get } = dateConfig
switch (currType) {
case 'date':
Expand Down
4 changes: 2 additions & 2 deletions packages/components/_private/date-panel/src/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import type { ActiveTypeContext } from './composables/useActiveType'
import type { MaxIndexContext } from './composables/useMaxIndex'
import type { DatePanelProps } from './types'
import type { DateConfig } from '@idux/components/config'
import type { DatePickerLocale } from '@idux/components/i18n'
import type { Locale } from '@idux/components/locales'
import type { ComputedRef, InjectionKey, Slots } from 'vue'

export interface DatePanelContext extends ActiveTypeContext, ActiveDateContext, MaxIndexContext {
props: DatePanelProps
slots: Slots
locale: ComputedRef<DatePickerLocale>
locale: Locale
mergedPrefixCls: ComputedRef<string>
dateConfig: DateConfig
}
Expand Down
26 changes: 13 additions & 13 deletions packages/components/config/src/dateConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* found in the LICENSE file at https://github.com/IDuxFE/idux/blob/main/LICENSE
*/

import { InjectionKey, inject } from 'vue'
import { type InjectionKey, inject } from 'vue'

import { isNil, isString } from 'lodash-es'

Expand Down Expand Up @@ -57,7 +57,7 @@ import {
toDate,
} from 'date-fns'

import { getLocale } from '@idux/components/i18n'
import { useGlobalConfig } from './globalConfig'

export type TimeConfigType = 'hour' | 'minute' | 'second'
export type DateConfigType = 'year' | 'quarter' | 'month' | 'week' | 'date' | 'day'
Expand Down Expand Up @@ -101,12 +101,12 @@ export function useDateConfig(): DateConfig<number | Date, Date> {
}

function createDefaultDateConfig(): DateConfig<number | Date, Date> {
const locale = getLocale('date')
const locale = useGlobalConfig('locale')
const now = () => new Date()
const parse = (dateString: string, format: string) => parseDate(dateString, format, now(), { locale: locale.value })
const parse = (dateString: string, format: string) => parseDate(dateString, format, now(), { locale: locale.date })
return {
now,
weekStartsOn: () => locale.value.options?.weekStartsOn || 1,
weekStartsOn: () => locale.date.options?.weekStartsOn || 1,
get: (date, type) => {
switch (type) {
case 'year':
Expand All @@ -116,7 +116,7 @@ function createDefaultDateConfig(): DateConfig<number | Date, Date> {
case 'month':
return getMonth(date)
case 'week':
return getWeek(date, { locale: locale.value })
return getWeek(date, { locale: locale.date })
case 'date':
return getDate(date)
case 'day':
Expand All @@ -138,11 +138,11 @@ function createDefaultDateConfig(): DateConfig<number | Date, Date> {
case 'month':
return setMonth(date, amount)
case 'week':
return setWeek(date, amount, { locale: locale.value })
return setWeek(date, amount, { locale: locale.date })
case 'date':
return setDate(date, amount)
case 'day':
return setDay(date, amount, { locale: locale.value })
return setDay(date, amount, { locale: locale.date })
case 'hour':
return setHours(date, amount)
case 'minute':
Expand Down Expand Up @@ -175,7 +175,7 @@ function createDefaultDateConfig(): DateConfig<number | Date, Date> {
case 'month':
return startOfMonth(date)
case 'week':
return startOfWeek(date, { locale: locale.value })
return startOfWeek(date, { locale: locale.date })
case 'date':
case 'day':
return startOfDay(date)
Expand All @@ -190,7 +190,7 @@ function createDefaultDateConfig(): DateConfig<number | Date, Date> {
case 'month':
return endOfMonth(date)
case 'week':
return endOfWeek(date, { locale: locale.value })
return endOfWeek(date, { locale: locale.date })
case 'date':
case 'day':
return endOfDay(date)
Expand All @@ -206,7 +206,7 @@ function createDefaultDateConfig(): DateConfig<number | Date, Date> {
case 'month':
return isSameMonth(date, dateToCompare)
case 'week':
return isSameWeek(date, dateToCompare, { locale: locale.value })
return isSameWeek(date, dateToCompare, { locale: locale.date })
case 'date':
case 'day':
return isSameDay(date, dateToCompare)
Expand All @@ -220,7 +220,7 @@ function createDefaultDateConfig(): DateConfig<number | Date, Date> {
},
isValid: date => isValid(date),

format: (date, format) => formatDate(date, format, { locale: locale.value }),
format: (date, format) => formatDate(date, format, { locale: locale.date }),
parse: parse,
convert: (date, format) => {
if (isNil(date)) {
Expand All @@ -229,7 +229,7 @@ function createDefaultDateConfig(): DateConfig<number | Date, Date> {
return isString(date) ? parse(date, format!) : toDate(date as number | Date)
},
getLocalizedLabels: (type, length, width) => {
const localize = locale.value.localize!
const localize = locale.date.localize!
switch (type) {
case 'month':
return Array.from({ length }).map((_, i) => localize.month(i, { width }))
Expand Down
Loading

0 comments on commit 4f50728

Please sign in to comment.