Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(comp:locales): use useGlobalConfig instead of useI18n #764

Merged
merged 1 commit into from
Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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