Skip to content

Commit

Permalink
feat(Rate): support placement
Browse files Browse the repository at this point in the history
  • Loading branch information
liweijie0812 committed Jul 16, 2024
1 parent fce4f2c commit ed1e5b5
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 6 deletions.
38 changes: 38 additions & 0 deletions src/rate/__test__/index.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,42 @@ describe('Rate', () => {
await move($target);
expect(onChange).toHaveBeenCalledTimes(1);
});

it(': placement',async () => {
const wrapper = mount(() => <Rate />);
const icons = wrapper.findAll(`.${name}__icon`);
await icons[0].trigger('click');
const tips = wrapper.find(`.${name}__tips`);
expect(tips.exists()).toBeTruthy();
const placement = wrapper.find(`.${name}__tips--top`);
expect(placement.exists()).toBeTruthy();
});

it(': placement=top',async () => {
const wrapper = mount(() => <Rate placement="top" />);
const icons = wrapper.findAll(`.${name}__icon`);
await icons[0].trigger('click');
const tips = wrapper.find(`.${name}__tips`);
expect(tips.exists()).toBeTruthy();
const placement = wrapper.find(`.${name}__tips--top`);
expect(placement.exists()).toBeTruthy();
});

it(': placement=bottom',async () => {
const wrapper = mount(() => <Rate placement="bottom" />);
const icons = wrapper.findAll(`.${name}__icon`);
await icons[0].trigger('click');
const tips = wrapper.find(`.${name}__tips`);
expect(tips.exists()).toBeTruthy();
const placement = wrapper.find(`.${name}__tips--bottom`);
expect(placement.exists()).toBeTruthy();
});

it(': placement=""',async () => {
const wrapper = mount(() => <Rate placement="" />);
const icons = wrapper.findAll(`.${name}__icon`);
await icons[0].trigger('click');
const tips = wrapper.find(`.${name}__tips`);
expect(tips.exists()).toBeFalsy();
});
});
11 changes: 10 additions & 1 deletion src/rate/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,22 @@ export default {
},
/** 评分图标的间距 */
gap: {
type: Number,
type: [String, Number] as PropType<TdRateProps['gap']>,
default: 8,
},
/** 自定义评分图标,[选中图标,未选中图标] */
icon: {
type: [Array, Function] as PropType<TdRateProps['icon']>,
},
/** 选择评分弹框的位置,值为空字符表示不显示评分弹窗 */
placement: {
type: String as PropType<TdRateProps['placement']>,
default: 'top' as TdRateProps['placement'],
validator(val: TdRateProps['placement']): boolean {
if (!val) return true;
return ['top', 'bottom', ''].includes(val);
},
},
/** 是否显示对应的辅助文字 */
showText: Boolean,
/** 评分图标的大小 */
Expand Down
3 changes: 2 additions & 1 deletion src/rate/rate.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ allowHalf | Boolean | false | \- | N
color | String / Array | '#ED7B2F' | Typescript:`string \| Array<string>` | N
count | Number | 5 | \- | N
disabled | Boolean | undefined | \- | N
gap | Number | 8 | \- | N
gap | String / Number | 8 | \- | N
icon | Array / Slot / Function | - | Typescript:`Array<TNode \| Function>`[see more ts definition](https://github.com/Tencent/tdesign-mobile-vue/blob/develop/src/common.ts) | N
placement | String | top | options: top / bottom / '' | N
showText | Boolean | false | \- | N
size | String | 24px | \- | N
texts | Array | [] | Typescript:`Array<string>` | N
Expand Down
3 changes: 2 additions & 1 deletion src/rate/rate.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ allowHalf | Boolean | false | 是否允许半选 | N
color | String / Array | '#ED7B2F' | 评分图标的颜色,样式中默认为 #ED7B2F。一个值表示设置选中高亮的五角星颜色,示例:[选中颜色]。数组则表示分别设置 选中高亮的五角星颜色 和 未选中暗灰的五角星颜色,[选中颜色,未选中颜色]。示例:['#ED7B2F', '#E3E6EB']。TS 类型:`string \| Array<string>` | N
count | Number | 5 | 评分的数量 | N
disabled | Boolean | undefined | 是否禁用评分 | N
gap | Number | 8 | 评分图标的间距 | N
gap | String / Number | 8 | 评分图标的间距 | N
icon | Array / Slot / Function | - | 自定义评分图标,[选中图标,未选中图标]。TS 类型:`Array<TNode \| Function>`[通用类型定义](https://github.com/Tencent/tdesign-mobile-vue/blob/develop/src/common.ts) | N
placement | String | top | 选择评分弹框的位置,值为空字符表示不显示评分弹窗。可选项:top / bottom / '' | N
showText | Boolean | false | 是否显示对应的辅助文字 | N
size | String | 24px | 评分图标的大小 | N
texts | Array | [] | 评分等级对应的辅助文字。组件内置默认值为:['极差', '失望', '一般', '满意', '惊喜']。自定义值示例:['1分', '2分', '3分', '4分', '5分']。TS 类型:`Array<string>` | N
Expand Down
11 changes: 9 additions & 2 deletions src/rate/rate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,16 @@ export default defineComponent({
};

const renderRateTips = () => {
if (!tipsVisible.value) return null;
if (!tipsVisible.value || props.placement === '') return null;
return (
<div ref={ratePopoverRef} class={`${rateClass.value}__tips`} style={{ left: `${tipsLeft.value}px` }}>
<div
ref={ratePopoverRef}
class={{
[`${rateClass.value}__tips`]: true,
[`${rateClass.value}__tips--${props.placement}`]: props.placement,
}}
style={{ left: `${tipsLeft.value}px` }}
>
{actionType.value === 'tap' ? (
<div style="display: flex">
{props.allowHalf && (
Expand Down
7 changes: 6 additions & 1 deletion src/rate/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,16 @@ export interface TdRateProps {
* 评分图标的间距
* @default 8
*/
gap?: number;
gap?: string | number;
/**
* 自定义评分图标,[选中图标,未选中图标]
*/
icon?: Array<TNode | Function>;
/**
* 选择评分弹框的位置,值为空字符表示不显示评分弹窗
* @default top
*/
placement?: 'top' | 'bottom' | '';
/**
* 是否显示对应的辅助文字
* @default false
Expand Down

0 comments on commit ed1e5b5

Please sign in to comment.