import {computed, inject, onBeforeUnmount, PropType, provide} from "@peryl/react-compose"; import {useFunctionWrapper} from "./useFunctionWrapper"; import {createEnum} from "@peryl/utils/createEnum"; import {ApplicationConfigurationProvider} from "../components/Application/utils/application.utils"; /** * 模式:fill:填充;flat:扁平(默认),outline:线性;text:文字 * @author 韦胜健 * @date 2022.4.27 0:22 */ export const ThemeMode = createEnum(['fill', 'flat', 'outline', 'text', 'stroke'] as const); export const ThemeStatus = createEnum(['secondary', 'primary', 'success', 'warn', 'error'] as const); export const ThemeShape = createEnum(['square', 'round', 'none'] as const); export const ThemeSize = createEnum(['large', 'normal', 'small', 'mini'] as const); export const InputMode = createEnum(['flat', 'stroke', 'text'] as const); export const StyleProps = { shape: { type: String as PropType }, // 'square', 'round' size: { type: String as PropType }, // 'large', 'normal', 'small', 'mini' status: { type: String as PropType }, // 'secondary', 'primary', 'success', 'warn', 'error' inputMode: { type: String as PropType }, // input的样式类型,flat填充,stroke线型 } as const; export interface UseStyleProvideData { shape: typeof ThemeShape.TYPE, size: typeof ThemeSize.TYPE, status?: typeof ThemeStatus.TYPE, inputMode?: typeof InputMode.TYPE, default: { shape: typeof ThemeShape.TYPE, size: typeof ThemeSize.TYPE, inputMode: typeof InputMode.TYPE, buttonMode: typeof ThemeMode.TYPE, }, } interface UseStyleOption extends Partial { adjust?: (data: UseStyleProvideData) => void | UseStyleProvideData; } const USE_STYLE_PROVIDER = '@@USE_STYLE_PROVIDER'; export const useStyle = useFunctionWrapper('style', (ctx, option: UseStyleOption = {}): { styleComputed: { value: UseStyleProvideData } } => { const applicationConfiguration = ApplicationConfigurationProvider.inject(); const parent = inject(USE_STYLE_PROVIDER, null) as null | { value: UseStyleProvideData }; const styleComputed = computed(() => { if (ctx.isDestroyed) {return {} as UseStyleProvideData;} const defaultData = Object.assign({ shape: applicationConfiguration.value.default.shape, size: applicationConfiguration.value.default.size, inputMode: applicationConfiguration.value.default.inputMode }, option); const { shape, size, status, inputMode } = ctx.props; const parentData = !!parent ? parent.value : {} as any; let data: UseStyleProvideData = { shape: shape || parentData.shape || defaultData.shape, size: size || parentData.size || defaultData.size, status: status || parentData.status || defaultData.status, inputMode: inputMode || parentData.inputMode || defaultData.inputMode, default: applicationConfiguration.value.default }; if (!!defaultData.adjust) { data = defaultData.adjust(data) || data; } (data as any).ctx = ctx; (data as any).parent = parent; return data; }); provide(USE_STYLE_PROVIDER, styleComputed); onBeforeUnmount(() => { setTimeout(() => { styleComputed.effect.stop(); }); }); return { styleComputed }; }); export type tStyleComputed = ReturnType['styleComputed']