From e2e5fcb10d16436ee134727d270a465c2eb0c32b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?w=C5=AB=20y=C4=81ng?= Date: Thu, 1 Aug 2024 15:56:18 +0800 Subject: [PATCH] feat(rate): texts support i18n (#3023) * feat(rate): texts support i18n * chore: fix typo check * chore: update snapshot --- .github/workflows/typos-config.toml | 2 +- src/_common | 2 +- src/config-provider/type.ts | 11 +++++++++++ src/rate/Rate.tsx | 11 ++++++++--- src/rate/defaultProps.ts | 1 - test/snap/__snapshots__/csr.test.jsx.snap | 4 ++-- test/snap/__snapshots__/ssr.test.jsx.snap | 2 +- 7 files changed, 24 insertions(+), 9 deletions(-) diff --git a/.github/workflows/typos-config.toml b/.github/workflows/typos-config.toml index 3fa11b791..a74f2402b 100644 --- a/.github/workflows/typos-config.toml +++ b/.github/workflows/typos-config.toml @@ -4,4 +4,4 @@ default.check-filename = true actived="actived" [files] -extend-exclude = ["CHANGELOG.md", "*.snap"] +extend-exclude = ["CHANGELOG.md", "*.snap","jest.base.conf.js"] diff --git a/src/_common b/src/_common index 1878b0313..17eeded85 160000 --- a/src/_common +++ b/src/_common @@ -1 +1 @@ -Subproject commit 1878b0313fa7dcdf5486f5adb7d2581c1c0d1c27 +Subproject commit 17eeded859b0c1bce4477e0f2d8b913afa773720 diff --git a/src/config-provider/type.ts b/src/config-provider/type.ts index f6ab18955..e4e06c05b 100644 --- a/src/config-provider/type.ts +++ b/src/config-provider/type.ts @@ -98,6 +98,10 @@ export interface GlobalConfigProvider { * 气泡确认框全局配置 */ popconfirm?: PopconfirmConfig; + /** + * 评分组件全局配置 + */ + rate?: RateConfig; /** * 选择器组件全局配置 */ @@ -912,6 +916,13 @@ export interface TypographyConfig { copiedText?: string; } +export interface RateConfig { + /** + * 评分描述,默认值:['极差', '失望', '一般', '满意', '惊喜'] + */ + rateText?: string[]; +} + export type AnimationType = 'ripple' | 'expand' | 'fade'; export type IconConfig = GlobalIconConfig; diff --git a/src/rate/Rate.tsx b/src/rate/Rate.tsx index c716c42f5..3e3a36eb1 100644 --- a/src/rate/Rate.tsx +++ b/src/rate/Rate.tsx @@ -4,6 +4,8 @@ import { StarFilledIcon as TdStarFilledIcon } from 'tdesign-icons-react'; import { TooltipLite } from '../tooltip'; import { TdRateProps } from './type'; import { StyledProps } from '../common'; + +import { useLocaleReceiver } from '../locale/LocalReceiver'; import useConfig from '../hooks/useConfig'; import useGlobalIcon from '../hooks/useGlobalIcon'; import useControlled from '../hooks/useControlled'; @@ -31,7 +33,10 @@ const RateIcon: React.FC = ({ icon, ...props }) => { const Rate = React.forwardRef((originalProps, ref) => { const props = useDefaultProps(originalProps, rateDefaultProps); - const { allowHalf, color, count, disabled, gap, showText, size, texts, icon, className, style, onChange } = props; + const { allowHalf, color, count, disabled, gap, showText, size, icon, className, style, onChange, texts } = props; + const [locale, t] = useLocaleReceiver('rate'); + + const displayTexts = texts || t(locale.rateText); const { classPrefix } = useConfig(); const [starValue = 0, setStarValue] = useControlled(props, 'value', onChange); @@ -112,7 +117,7 @@ const Rate = React.forwardRef((originalProps, ref) => onMouseMove={(event) => mouseEnterHandler(event, index + 1)} > {showText ? ( - +
@@ -133,7 +138,7 @@ const Rate = React.forwardRef((originalProps, ref) => ))} - {showText &&
{texts[displayValue - 1]}
} + {showText &&
{displayTexts[displayValue - 1]}
} ); }); diff --git a/src/rate/defaultProps.ts b/src/rate/defaultProps.ts index 20c06610b..9f0e2da13 100644 --- a/src/rate/defaultProps.ts +++ b/src/rate/defaultProps.ts @@ -12,5 +12,4 @@ export const rateDefaultProps: TdRateProps = { count: 5, gap: 4, showText: false, - texts: ['极差', '失望', '一般', '满意', '惊喜'], }; diff --git a/test/snap/__snapshots__/csr.test.jsx.snap b/test/snap/__snapshots__/csr.test.jsx.snap index 6dec44b7a..758173fb7 100644 --- a/test/snap/__snapshots__/csr.test.jsx.snap +++ b/test/snap/__snapshots__/csr.test.jsx.snap @@ -72173,7 +72173,7 @@ exports[`csr snapshot test > csr test src/rate/_example/texts.tsx 1`] = `
- 满意 + satisfied
@@ -135889,7 +135889,7 @@ exports[`ssr snapshot test > ssr test src/rate/_example/size.tsx 1`] = `"
ssr test src/rate/_example/status.tsx 1`] = `"

未评分状态

满分状态

半星状态

"`; -exports[`ssr snapshot test > ssr test src/rate/_example/texts.tsx 1`] = `"
满意
"`; +exports[`ssr snapshot test > ssr test src/rate/_example/texts.tsx 1`] = `"
satisfied
"`; exports[`ssr snapshot test > ssr test src/select/_example/base.tsx 1`] = `"
"`; diff --git a/test/snap/__snapshots__/ssr.test.jsx.snap b/test/snap/__snapshots__/ssr.test.jsx.snap index e044f9a7e..71f9831db 100644 --- a/test/snap/__snapshots__/ssr.test.jsx.snap +++ b/test/snap/__snapshots__/ssr.test.jsx.snap @@ -748,7 +748,7 @@ exports[`ssr snapshot test > ssr test src/rate/_example/size.tsx 1`] = `"
ssr test src/rate/_example/status.tsx 1`] = `"

未评分状态

满分状态

半星状态

"`; -exports[`ssr snapshot test > ssr test src/rate/_example/texts.tsx 1`] = `"
满意
"`; +exports[`ssr snapshot test > ssr test src/rate/_example/texts.tsx 1`] = `"
satisfied
"`; exports[`ssr snapshot test > ssr test src/select/_example/base.tsx 1`] = `"
"`;