Skip to content

Commit

Permalink
feat(rate): texts support i18n (#3023)
Browse files Browse the repository at this point in the history
* feat(rate): texts support i18n

* chore: fix typo check

* chore: update snapshot
  • Loading branch information
uyarn authored Aug 1, 2024
1 parent b65a0bb commit e2e5fcb
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/typos-config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
11 changes: 11 additions & 0 deletions src/config-provider/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ export interface GlobalConfigProvider {
* 气泡确认框全局配置
*/
popconfirm?: PopconfirmConfig;
/**
* 评分组件全局配置
*/
rate?: RateConfig;
/**
* 选择器组件全局配置
*/
Expand Down Expand Up @@ -912,6 +916,13 @@ export interface TypographyConfig {
copiedText?: string;
}

export interface RateConfig {
/**
* 评分描述,默认值:['极差', '失望', '一般', '满意', '惊喜']
*/
rateText?: string[];
}

export type AnimationType = 'ripple' | 'expand' | 'fade';

export type IconConfig = GlobalIconConfig;
Expand Down
11 changes: 8 additions & 3 deletions src/rate/Rate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -31,7 +33,10 @@ const RateIcon: React.FC<RateIconProps> = ({ icon, ...props }) => {
const Rate = React.forwardRef<HTMLDivElement, RateProps>((originalProps, ref) => {
const props = useDefaultProps<RateProps>(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);
Expand Down Expand Up @@ -112,7 +117,7 @@ const Rate = React.forwardRef<HTMLDivElement, RateProps>((originalProps, ref) =>
onMouseMove={(event) => mouseEnterHandler(event, index + 1)}
>
{showText ? (
<TooltipLite key={index} content={texts[displayValue - 1]}>
<TooltipLite key={index} content={displayTexts[displayValue - 1]}>
<div className={`${classPrefix}-rate__star-top`}>
<RateIcon size={size} color={activeColor} icon={icon} />
</div>
Expand All @@ -133,7 +138,7 @@ const Rate = React.forwardRef<HTMLDivElement, RateProps>((originalProps, ref) =>
</li>
))}
</ul>
{showText && <div className={`${classPrefix}-rate__text`}>{texts[displayValue - 1]}</div>}
{showText && <div className={`${classPrefix}-rate__text`}>{displayTexts[displayValue - 1]}</div>}
</div>
);
});
Expand Down
1 change: 0 additions & 1 deletion src/rate/defaultProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ export const rateDefaultProps: TdRateProps = {
count: 5,
gap: 4,
showText: false,
texts: ['极差', '失望', '一般', '满意', '惊喜'],
};
4 changes: 2 additions & 2 deletions test/snap/__snapshots__/csr.test.jsx.snap

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion test/snap/__snapshots__/ssr.test.jsx.snap

Large diffs are not rendered by default.

0 comments on commit e2e5fcb

Please sign in to comment.