antd-tiny-vue/components/config-provider/context.ts

143 lines
4.0 KiB
TypeScript
Raw Normal View History

2023-04-17 10:09:13 +08:00
import {
booleanType,
createInjectionState,
functionType,
objectType,
someType,
stringType
} from '@v-c/utils'
2023-03-25 17:49:55 +08:00
import type { ExtractPropTypes } from 'vue'
2023-03-02 07:16:36 +08:00
import { computed } from 'vue'
2023-03-25 17:49:55 +08:00
import type { DerivativeFunc } from '@antd-tiny-vue/cssinjs'
2023-04-17 10:09:13 +08:00
import type {
AliasToken,
MapToken,
OverrideToken,
SeedToken
} from '../theme/interface'
2023-03-25 17:49:55 +08:00
import type { RenderEmptyHandler } from './default-render-empty'
2023-03-26 15:49:59 +08:00
import type { ConfigProviderProps } from './index'
2023-03-02 07:16:36 +08:00
2023-03-25 17:16:46 +08:00
export type SizeType = 'small' | 'middle' | 'large' | undefined
2023-03-25 17:49:55 +08:00
export interface Theme {
primaryColor?: string
infoColor?: string
successColor?: string
processingColor?: string
errorColor?: string
warningColor?: string
}
export interface CSPConfig {
nonce?: string
}
export type DirectionType = 'ltr' | 'rtl' | undefined
export type MappingAlgorithm = DerivativeFunc<SeedToken, MapToken>
export interface ThemeConfig {
token?: Partial<AliasToken>
components?: OverrideToken
algorithm?: MappingAlgorithm | MappingAlgorithm[]
hashed?: boolean
inherit?: boolean
}
2023-03-02 07:16:36 +08:00
export const defaultIconPrefixCls = 'anticon'
2023-04-17 10:09:13 +08:00
const defaultGetPrefixCls = (
suffixCls?: string,
customizePrefixCls?: string
) => {
2023-03-02 07:16:36 +08:00
if (customizePrefixCls) return customizePrefixCls
return suffixCls ? `ant-${suffixCls}` : 'ant'
}
2023-03-25 17:49:55 +08:00
export const configConsumerProps = {
getTargetContainer: functionType<() => HTMLElement>(),
getPopupContainer: functionType<(triggerNode?: HTMLElement) => HTMLElement>(),
rootPrefixCls: stringType(),
iconPrefixCls: stringType(defaultIconPrefixCls),
getPrefixCls: functionType(defaultGetPrefixCls),
renderEmpty: functionType<RenderEmptyHandler>(),
csp: objectType<CSPConfig>(),
autoInsertSpaceInButton: booleanType(),
input: objectType<{
autoComplete?: string
}>(),
pagination: objectType<{
showSizeChanger?: boolean
}>(),
locale: objectType(),
pageHeader: objectType<{
ghost: boolean
}>(),
direction: someType<DirectionType>([String]),
space: objectType<{
size?: SizeType | number
}>(),
virtual: booleanType(),
dropdownMatchSelectWidth: booleanType(),
form: objectType<{
// requiredMark?: RequiredMark
colon?: boolean
// scrollToFirstError?: Options | boolean
}>(),
theme: objectType<ThemeConfig>(),
select: objectType<{
showSearch?: boolean
}>()
}
export type ConfigConsumerProps = ExtractPropTypes<typeof configConsumerProps>
2023-03-26 15:49:59 +08:00
const configState = (props: ConfigProviderProps) => {
const getPrefixCls = (suffixCls?: string, customizePrefixCls?: string) => {
const { prefixCls, getPrefixCls } = props
if (customizePrefixCls) return customizePrefixCls
2023-04-17 10:09:13 +08:00
const mergedPrefixCls =
prefixCls || getPrefixCls?.('') || defaultGetPrefixCls('')
2023-03-26 15:49:59 +08:00
return suffixCls ? `${mergedPrefixCls}-${suffixCls}` : mergedPrefixCls
}
2023-04-17 10:09:13 +08:00
const iconPrefixCls = computed(
() => props?.iconPrefixCls ?? defaultIconPrefixCls
)
const shouldWrapSSR = computed(
() => iconPrefixCls.value !== defaultIconPrefixCls
)
2023-03-26 15:49:59 +08:00
const csp = computed(() => props?.csp)
const componentSize = computed(() => props?.componentSize)
const componentDisabled = computed(() => props?.componentDisabled)
2023-03-27 07:51:19 +08:00
const autoInsertSpaceInButton = computed(() => props?.autoInsertSpaceInButton)
const direction = computed(() => props?.direction)
2023-03-02 07:16:36 +08:00
return {
getPrefixCls,
2023-03-26 15:49:59 +08:00
iconPrefixCls,
shouldWrapSSR,
csp,
componentSize,
2023-03-27 07:51:19 +08:00
componentDisabled,
autoInsertSpaceInButton,
direction
2023-03-02 07:16:36 +08:00
}
2023-03-26 15:49:59 +08:00
}
2023-04-17 10:09:13 +08:00
const [useProviderConfigProvide, useProviderConfigInject] =
createInjectionState(configState)
2023-03-02 07:16:36 +08:00
export { useProviderConfigProvide }
2023-03-26 15:49:59 +08:00
export const useProviderConfigState = (): ReturnType<typeof configState> => {
2023-03-02 07:16:36 +08:00
return (
2023-03-26 15:49:59 +08:00
useProviderConfigInject() ??
({
2023-03-02 07:16:36 +08:00
getPrefixCls: defaultGetPrefixCls,
2023-03-27 07:51:19 +08:00
iconPrefixCls: computed(() => defaultIconPrefixCls),
componentSize: computed(() => undefined),
componentDisabled: computed(() => false),
direction: computed(() => undefined),
2023-04-17 10:09:13 +08:00
autoInsertSpaceInButton: computed(() => true)
2023-03-26 15:49:59 +08:00
} as any)
2023-03-02 07:16:36 +08:00
)
}