diff --git a/src/rate/Rate.tsx b/src/rate/Rate.tsx index b9699354be..47ea10edb4 100644 --- a/src/rate/Rate.tsx +++ b/src/rate/Rate.tsx @@ -33,13 +33,14 @@ 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, icon, className, style, onChange, texts } = props; + const { allowHalf, color, count, disabled, gap, showText, size, icon, className, style, onChange, texts, clearable } = + props; const [locale, t] = useLocaleReceiver('rate'); const displayTexts = texts.length ? texts : t(locale.rateText); const { classPrefix } = useConfig(); - const [starValue = 0, setStarValue] = useControlled(props, 'value', onChange); + const [starValue, setStarValue] = useControlled(props, 'value', onChange); const [hoverValue, setHoverValue] = useState(undefined); const displayValue = hoverValue || starValue; @@ -86,7 +87,14 @@ const Rate = React.forwardRef((originalProps, ref) => if (disabled) { return; } - setStarValue(getStarValue(event, index)); + + let value = getStarValue(event, index); + if (clearable && value === starValue) { + value = 0; + setHoverValue(undefined); + } + + setStarValue(value); }; const getStarCls = (index: number) => { diff --git a/src/rate/__tests__/rate.test.tsx b/src/rate/__tests__/rate.test.tsx index 445623cb0c..149c1a640e 100644 --- a/src/rate/__tests__/rate.test.tsx +++ b/src/rate/__tests__/rate.test.tsx @@ -78,5 +78,21 @@ describe('Rate 组件测试', () => { }); expect(document.querySelector('.t-rate__item--half')).toBeTruthy(); }); + + test('clearable', () => { + const { container } = render(); + + // 默认展示 + expect(container.querySelectorAll('.t-rate__item--full')).toHaveLength(1); + + // 点击不同区域选中 + fireEvent.click(container.querySelectorAll('.t-rate__item')[1]); + expect(container.querySelectorAll('.t-rate__item--full')).toHaveLength(2); + + // 点击相同区域清除选中 + fireEvent.click(container.querySelectorAll('.t-rate__item')[1]); + + expect(container.querySelectorAll('.t-rate__item--full')).toHaveLength(0); + }); }); }); diff --git a/src/rate/_example/clearable.tsx b/src/rate/_example/clearable.tsx new file mode 100644 index 0000000000..d05cbb828c --- /dev/null +++ b/src/rate/_example/clearable.tsx @@ -0,0 +1,13 @@ +import React from 'react'; +import { Rate, Space } from 'tdesign-react'; + +export default function BasicRate() { + return ( + +

clearable: true

+ +

clearable: false

+ +
+ ); +} diff --git a/src/rate/defaultProps.ts b/src/rate/defaultProps.ts index 80b99cf60d..5475a3a929 100644 --- a/src/rate/defaultProps.ts +++ b/src/rate/defaultProps.ts @@ -6,6 +6,7 @@ import { TdRateProps } from './type'; export const rateDefaultProps: TdRateProps = { allowHalf: false, + clearable: false, color: '#ED7B2F', count: 5, disabled: undefined, diff --git a/src/rate/rate.en-US.md b/src/rate/rate.en-US.md index f783b4fd90..d8221a7e16 100644 --- a/src/rate/rate.en-US.md +++ b/src/rate/rate.en-US.md @@ -9,6 +9,7 @@ name | type | default | description | required className | String | - | className of component | N style | Object | - | CSS(Cascading Style Sheets),Typescript:`React.CSSProperties` | N allowHalf | Boolean | false | \- | N +clearable | Boolean | false | \- | N color | String / Array | '#ED7B2F' | Typescript:`string \| Array` | N count | Number | 5 | \- | N disabled | Boolean | undefined | \- | N diff --git a/src/rate/rate.md b/src/rate/rate.md index 532e1aede6..2cd9aa3d0d 100644 --- a/src/rate/rate.md +++ b/src/rate/rate.md @@ -9,6 +9,7 @@ className | String | - | 类名 | N style | Object | - | 样式,TS 类型:`React.CSSProperties` | N allowHalf | Boolean | false | 是否允许半选 | N +clearable | Boolean | false | 是否允许清除 | N color | String / Array | '#ED7B2F' | 评分图标的颜色,样式中默认为 #ED7B2F。一个值表示设置选中高亮的五角星颜色,示例:[选中颜色]。数组则表示分别设置 选中高亮的五角星颜色 和 未选中暗灰的五角星颜色,[选中颜色,未选中颜色]。示例:['#ED7B2F', '#E3E6EB']。TS 类型:`string \| Array` | N count | Number | 5 | 评分的数量 | N disabled | Boolean | undefined | 是否禁用评分 | N diff --git a/src/rate/type.ts b/src/rate/type.ts index d7e7410d7b..f77df8d444 100644 --- a/src/rate/type.ts +++ b/src/rate/type.ts @@ -12,6 +12,11 @@ export interface TdRateProps { * @default false */ allowHalf?: boolean; + /** + * 是否允许清除 + * @default false + */ + clearable?: boolean; /** * 评分图标的颜色,样式中默认为 #ED7B2F。一个值表示设置选中高亮的五角星颜色,示例:[选中颜色]。数组则表示分别设置 选中高亮的五角星颜色 和 未选中暗灰的五角星颜色,[选中颜色,未选中颜色]。示例:['#ED7B2F', '#E3E6EB'] * @default '#ED7B2F'