type: fix useLocale type error of site (#48204)

This commit is contained in:
lijianan 2024-04-01 12:07:58 +08:00 committed by GitHub
parent ff231ab557
commit 4eaa664d2e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 14 deletions

View File

@ -1,16 +1,22 @@
import { useLocale as useDumiLocale } from 'dumi';
export interface LocaleMap<Key extends string> {
cn: Record<Key, string>;
en: Record<Key, string>;
export interface LocaleMap<
K extends PropertyKey = PropertyKey,
V extends string | ((...params: any[]) => string) = string,
> {
cn: Record<K, V>;
en: Record<K, V>;
}
function useLocale<Key extends string>(
localeMap?: LocaleMap<Key>,
): [Record<Key, string>, 'cn' | 'en'] {
const useLocale = <
K extends PropertyKey = PropertyKey,
V extends string | ((...params: any[]) => string) = string,
>(
localeMap?: LocaleMap<K, V>,
): [Record<K, V>, 'cn' | 'en'] => {
const { id } = useDumiLocale();
const localeType = id === 'zh-CN' ? 'cn' : 'en';
return [localeMap?.[localeType]!, localeType];
}
return [localeMap?.[localeType]!, localeType] as const;
};
export default useLocale;

View File

@ -1,9 +1,10 @@
import { FormattedMessage } from 'dumi';
import React, { useMemo, useState } from 'react';
import React from 'react';
import { ColorPicker } from 'antd';
import type { Color } from 'antd/es/color-picker';
import ColorPatterns from './ColorPatterns';
import { FormattedMessage } from 'dumi';
import useLocale from '../../../hooks/useLocale';
import ColorPatterns from './ColorPatterns';
const primaryMinSaturation = 70; // 主色推荐最小饱和度
const primaryMinBrightness = 70; // 主色推荐最小亮度
@ -22,8 +23,8 @@ const locales = {
};
const ColorPaletteTool: React.FC = () => {
const [primaryColor, setPrimaryColor] = useState<string>('#1890ff');
const [primaryColorInstance, setPrimaryColorInstance] = useState<Color>(null);
const [primaryColor, setPrimaryColor] = React.useState<string>('#1890ff');
const [primaryColorInstance, setPrimaryColorInstance] = React.useState<Color>();
const [locale] = useLocale(locales);
@ -32,7 +33,7 @@ const ColorPaletteTool: React.FC = () => {
setPrimaryColorInstance(color);
};
const colorValidation = useMemo<React.ReactNode>(() => {
const colorValidation = React.useMemo<React.ReactNode>(() => {
let text = '';
if (primaryColorInstance) {
const { s, b } = primaryColorInstance.toHsb() || {};