Skip to content

Commit b897d6b

Browse files
committed
➕ Shape of star
1 parent 9da0d54 commit b897d6b

File tree

5 files changed

+68
-17
lines changed

5 files changed

+68
-17
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ export default App;
6565
<td>0</td>
6666
<td>The default value. Use when the component is not controlled.</td>
6767
</tr>
68+
<tr>
69+
<td>shape</td>
70+
<td>'thin' | 'fat'</td>
71+
<td>'thin'</td>
72+
<td>The shape of the star.</td>
73+
</tr>
6874
<tr>
6975
<td>fraction</td>
7076
<td>number</td>

examples/javascript/src/App.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class App extends React.Component {
2727
<Star
2828
fraction={2}
2929
onChange={(rate) => console.log(rate)}
30+
shape={'fat'}
3031
// emptyIcon={<SVGIcon href="#icon-star-empty" className="icon" />}
3132
// fullIcon={<SVGIcon1 href="#icon-star-full" className="icon" />}
3233
/>

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"reactjs",
1717
"react-star",
1818
"star",
19-
"rating"
19+
"rating",
20+
"react-rating"
2021
],
2122
"homepage": "https://github.com/Bunlong/react-star",
2223
"main": "dist/react-star.js",

src/Star.tsx

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import StarIcon from './StarIcon';
33
import { calculateTotalIcons } from './utils';
4-
import { ThinStar } from './stars';
4+
import { ThinStar, FatStar } from './stars';
55
import {
66
DEFAULT_MIN,
77
DEFAULT_MAX,
@@ -12,6 +12,7 @@ import {
1212
NO_OPERATION,
1313
DEFAULT_OTHER,
1414
} from './models';
15+
import { emptyIconStyle, iconStyle } from './styles';
1516

1617
interface Props {
1718
id?: number;
@@ -36,6 +37,7 @@ interface Props {
3637
onHover: (index?: number, event?: any) => void;
3738
onClick: (index?: number, event?: any) => void;
3839
onChange: (index?: number, event?: any) => void;
40+
shape: 'thin' | 'fat';
3941
}
4042

4143
interface State {
@@ -50,15 +52,6 @@ class Star extends React.PureComponent<Props, State> {
5052
step: DEFAULT_STEP,
5153
readOnly: DEFAULT_READONLY,
5254
fraction: DEFAULT_FRACTION,
53-
icon: <ThinStar style={{ fill: '#ffb400', stroke: '#ffb400' }} />,
54-
emptyIcon: (
55-
<ThinStar
56-
style={{ fill: '#000000', stroke: '#000000', opacity: '26%' }}
57-
/>
58-
),
59-
placeholderIcon: (
60-
<ThinStar style={{ fill: '#ffb400', stroke: '#ffb400' }} />
61-
),
6255
quiet: DEFAULT_QUIET,
6356
onHover: NO_OPERATION,
6457
onClick: NO_OPERATION,
@@ -129,12 +122,45 @@ class Star extends React.PureComponent<Props, State> {
129122
: translatedValue;
130123
}
131124

125+
renderEmptyIcon() {
126+
const { shape, emptyIcon } = this.props;
127+
if (!emptyIcon) {
128+
return shape === 'fat' ? (
129+
<FatStar style={emptyIconStyle} />
130+
) : (
131+
<ThinStar style={emptyIconStyle} />
132+
);
133+
}
134+
return emptyIcon;
135+
}
136+
137+
renderIcon() {
138+
const { shape, icon } = this.props;
139+
if (!icon) {
140+
return shape === 'fat' ? (
141+
<FatStar style={iconStyle} />
142+
) : (
143+
<ThinStar style={iconStyle} />
144+
);
145+
}
146+
return icon;
147+
}
148+
149+
renderPlaceholderIcon() {
150+
const { shape, placeholderIcon } = this.props;
151+
if (!placeholderIcon) {
152+
return shape === 'fat' ? (
153+
<FatStar style={iconStyle} />
154+
) : (
155+
<ThinStar style={iconStyle} />
156+
);
157+
}
158+
return placeholderIcon;
159+
}
160+
132161
render() {
133162
const {
134163
step,
135-
emptyIcon,
136-
icon,
137-
placeholderIcon,
138164
readOnly,
139165
quiet,
140166
fraction,
@@ -165,9 +191,9 @@ class Star extends React.PureComponent<Props, State> {
165191
readOnly={readOnly || DEFAULT_READONLY}
166192
quiet={quiet || DEFAULT_QUIET}
167193
fraction={fraction || DEFAULT_FRACTION}
168-
emptyIcon={emptyIcon || ''}
169-
icon={icon || ''}
170-
placeholderIcon={placeholderIcon || ''}
194+
emptyIcon={this.renderEmptyIcon()}
195+
icon={this.renderIcon()}
196+
placeholderIcon={this.renderPlaceholderIcon}
171197
onClick={this.handleClick}
172198
onHover={this.handleHover}
173199
/>

src/styles.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { CSSProperties } from 'react';
2+
3+
export const color = {
4+
black: '#000000',
5+
selectiveYellow: '#ffb400',
6+
};
7+
8+
export const emptyIconStyle = {
9+
fill: color.black,
10+
stroke: color.black,
11+
opacity: '26%',
12+
} as CSSProperties;
13+
14+
export const iconStyle = {
15+
fill: color.selectiveYellow,
16+
stroke: color.selectiveYellow,
17+
} as CSSProperties;

0 commit comments

Comments
 (0)