Skip to content

Commit 647c0ef

Browse files
committed
basic usage
1 parent d080634 commit 647c0ef

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed

src/components/Rate/index.scss

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
@import '../../styles/colors.scss';
2+
@import '../../styles/spacing.scss';
3+
@import '../../styles/typography.scss';
4+
@import '../../styles/opacity.scss';
5+
@import '../../styles/variables.scss';
6+
7+
$baseClass: 'vant-rate';
8+
9+
.#{$baseClass} {
10+
display: flex;
11+
cursor: pointer;
12+
}

src/components/Rate/index.stories.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import React, { useState } from 'react';
2+
import Rate from '../Rate';
3+
import '../../styles/stories.scss';
4+
5+
export default {
6+
title: 'Rate',
7+
component: Rate
8+
};
9+
10+
export const BasicUsage = () => (
11+
<div className='container column grey'>
12+
<Rate currentRate={4} disabled />
13+
</div>
14+
);

src/components/Rate/index.tsx

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import React, { useState, useEffect } from 'react';
2+
3+
import classnames from '../../utils/classNames';
4+
5+
import './index.scss';
6+
import Icon from '../Icons';
7+
import { IProps } from './types';
8+
9+
const baseClass = 'vant-rate';
10+
11+
const renderIcons = (icons, iconCount, iconName, iconColor, iconSize) => {
12+
for (let i = 0; i < iconCount; i++) {
13+
icons.push(<Icon color={iconColor} size={iconSize} name={iconName} />);
14+
}
15+
return icons;
16+
};
17+
18+
const Rate = ({
19+
currentRate = 5,
20+
count = 5,
21+
size = '20px',
22+
icon = 'star',
23+
voidIcon = 'star-o',
24+
gutter = '4px',
25+
color = '#ffd21e',
26+
voidColor = '#c8c9cc',
27+
disabledColor = '#c8c9cc',
28+
allowHalf,
29+
disabled,
30+
readonly,
31+
touchable
32+
}: IProps) => {
33+
const [rateIcons, setRateIcons] = useState([]);
34+
35+
useEffect(() => {
36+
let currentIcons = [...rateIcons];
37+
if (disabled) {
38+
currentIcons = renderIcons(
39+
currentIcons,
40+
count,
41+
icon,
42+
disabledColor,
43+
size
44+
);
45+
} else {
46+
currentIcons = renderIcons(currentIcons, currentRate, icon, color, size);
47+
currentIcons = renderIcons(
48+
currentIcons,
49+
count - currentRate,
50+
voidIcon,
51+
voidColor,
52+
size
53+
);
54+
}
55+
setRateIcons(currentIcons);
56+
}, []);
57+
58+
const rateProps = {
59+
className: classnames(baseClass, [
60+
{
61+
allowHalf,
62+
disabled,
63+
readonly
64+
}
65+
])
66+
};
67+
68+
return (
69+
<div {...rateProps}>
70+
{rateIcons.map((v, i) => (
71+
<div key={i} className={`${baseClass}__icon`}>
72+
{v}
73+
</div>
74+
))}
75+
</div>
76+
);
77+
};
78+
79+
export default Rate;

src/components/Rate/types.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export interface IProps {
2+
currentRate?: number;
3+
count?: number;
4+
size?: string;
5+
icon?: string;
6+
gutter?: string;
7+
voidIcon?: string;
8+
allowHalf?: boolean;
9+
disabled?: boolean;
10+
readonly?: boolean;
11+
color?: string;
12+
voidColor?: string;
13+
disabledColor?: string;
14+
touchable?: boolean;
15+
}

0 commit comments

Comments
 (0)