Skip to content

Commit 0890b76

Browse files
authored
fix(Rating): Rating 评分组件 使用繁琐(#244) (#231)
* 编写选项卡组件 * 类型名称请调整,添加组件文档在网站上展示 * #162修复报错 * fix:#162修复报错 * SpeedDial 悬浮标记 * 优化类型 * feat(ActionSheet): Add new component. * fix: ActionSheet 实例优化,组件优化 * feat(SearchInputBar): Add new component. * feat(Pagination): Add new component. * fix(Tooltip): 修复cloud弹出元素位置问题 * doc(Drawer): Update README.md * doc: Update README.md * fix(Rating): Rating 评分组件 使用繁琐(#244) * fix(Rating): Rating 评分组件 使用繁琐(#244)
1 parent bbad461 commit 0890b76

File tree

4 files changed

+45
-57
lines changed

4 files changed

+45
-57
lines changed

example/examples/src/routes/Rating/index.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@ export default class Index extends Component<IndexProps, IndexState> {
3333
<Body>
3434
<View style={styles.divider} />
3535
<Rating
36-
defaultRating={5}
37-
// count={10}
36+
defaultRating={2}
37+
resultRating={10}
3838
icon={[<Icon name="star-off" />, <Icon name="star-on" />]}
3939
// icon={["star-off", "star-on"]}
40+
onPress={console.log}
4041
/>
4142
<View style={styles.divider} />
4243
<Rating />

packages/core/src/Drawer/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ function Demo() {
5555
```
5656

5757
## 注意事项 - 抽屉高度是页面有效高度
58+
5859
```jsx
5960
import { Fragment } from 'react';
6061
import { View, Text, SafeAreaView } from 'react-native';
@@ -77,7 +78,9 @@ function Demo() {
7778
);
7879
}
7980
```
81+
8082
## 抽屉覆盖全屏
83+
8184
- 可查看 [react-native-root-siblings](https://www.npmjs.com/package/react-native-root-siblings) 文档
8285
```jsx
8386
// 在 App.js 文件中
@@ -123,6 +126,7 @@ function Demo() {
123126
);
124127
}
125128
```
129+
126130
## props
127131

128132
| 参数 | 说明 | 类型 | 默认值 |

packages/core/src/Rating/README.md

+9-11
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,20 @@ import { TabsItemIconTypes } from '@uiw/react-native'
4949

5050
export type icoType = [TabsItemIconTypes, TabsItemIconTypes]
5151
export interface RatingProps {
52-
/** 默认几个 */
53-
defaultRating?: number,
54-
/** 总分 */
55-
count?: number,
56-
/** 默认分数 */
57-
defaultCount?: number,
52+
/** 默认选中几个 */
53+
defaultRating?: number;
54+
/** 总数*/
55+
resultRating?: number;
5856
/** icon 大小 */
59-
size?: number,
57+
size?: number;
6058
/** icon 颜色 */
61-
color?: string,
59+
color?: string;
6260
/** [未选中, 已选中] */
63-
icon?: icoType,
61+
icon?: icoType;
6462
/**
6563
* void
66-
* @param score type: number 得到几分
64+
* @param score type: number 选中几个
6765
*/
68-
onPress?: (score: number) => void
66+
onPress?: (score: number) => void;
6967
}
7068
```

packages/core/src/Rating/index.tsx

+29-44
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@ import { TabsItemIconTypes } from '../Tabs/TabsItem';
66

77
export type icoType = [TabsItemIconTypes, TabsItemIconTypes];
88
export interface RatingProps {
9-
/** 默认几个 */
9+
/** 默认选中几个 */
1010
defaultRating?: number;
11-
/** 总分 */
12-
count?: number;
13-
/** 默认分数 */
14-
defaultCount?: number;
11+
/** 总数*/
12+
resultRating?: number;
1513
/** icon 大小 */
1614
size?: number;
1715
/** icon 颜色 */
@@ -27,83 +25,70 @@ export interface RatingProps {
2725

2826
export interface RatingState {
2927
icon: Array<IconsName | React.ReactElement | React.ReactNode>;
30-
count: number;
31-
defaultCount: number;
28+
resultRating: number;
3229
size: number;
3330
color: string;
3431
defaultRating: number;
3532
typeIcon: icoType;
3633
}
3734

3835
export default class Rating extends React.Component<RatingProps, RatingState> {
39-
// static state: RatingState
4036
constructor(props: RatingProps) {
4137
super(props);
4238
let start = (props.icon && props.icon[0]) || 'star-off';
4339
let end = (props.icon && props.icon[1]) || 'star-on';
4440
this.state = {
45-
defaultRating: props.defaultRating || 5,
41+
defaultRating: props.defaultRating || 0,
42+
resultRating: props.resultRating || 5,
4643
icon: [],
47-
count: props.count ?? 5,
48-
defaultCount: props.defaultCount ?? 1,
4944
size: props.size ?? 24,
5045
color: props.color || '#ebc445',
5146
typeIcon: [start, end],
5247
};
5348
}
5449
componentDidMount() {
55-
const { defaultCount } = this.state;
56-
this.updateIcon(defaultCount);
50+
const { defaultRating } = this.state;
51+
this.updateIcon(defaultRating);
5752
}
5853
flag = true;
5954
updateIcon = (index: number) => {
60-
const { defaultRating } = this.state;
55+
const { resultRating } = this.state;
56+
const { onPress } = this.props;
6157
let start = this.state.typeIcon[0];
6258
let end = this.state.typeIcon[1];
6359
if (index === 1 && this.flag) {
64-
this.setState({ icon: [...new Array(index).fill(end), ...new Array(defaultRating - index).fill(start)] });
60+
this.setState({ icon: [...new Array(index).fill(end), ...new Array(resultRating - index).fill(start)] });
61+
onPress?.(1);
6562
this.flag = false;
6663
} else if (index === 1 && !this.flag) {
67-
this.setState({ icon: [...new Array(index - 1).fill(end), ...new Array(defaultRating - index + 1).fill(start)] });
64+
this.setState({ icon: [...new Array(index - 1).fill(end), ...new Array(resultRating - index + 1).fill(start)] });
6865
this.flag = true;
66+
onPress?.(0);
6967
} else {
70-
this.setState({ icon: [...new Array(index).fill(end), ...new Array(defaultRating - index).fill(start)] });
68+
this.setState({ icon: [...new Array(index).fill(end), ...new Array(resultRating - index).fill(start)] });
7169
this.flag = true;
70+
onPress?.(index);
7271
}
7372
};
7473
render() {
7574
return (
7675
<View>
77-
{/* <Text >{this.state.count}</Text> */}
7876
<View style={styles.RatingContainer}>
7977
{this.state.icon.map((item, index) => {
80-
if (typeof item === 'string') {
81-
return (
82-
<TouchableOpacity
83-
onPress={() => {
84-
this.updateIcon(index + 1);
85-
this.props.onPress &&
86-
this.props.onPress((this.state.count / this.state.defaultRating) * (index + 1));
87-
}}
88-
key={index}
89-
>
78+
return (
79+
<TouchableOpacity
80+
onPress={() => {
81+
this.updateIcon(index + 1);
82+
}}
83+
key={index}
84+
>
85+
{typeof item === 'string' ? (
9086
<Icon name={item as IconsName} color="#ebc445" size={this.state.size} />
91-
</TouchableOpacity>
92-
);
93-
} else {
94-
return (
95-
<TouchableOpacity
96-
onPress={() => {
97-
this.updateIcon(index + 1);
98-
this.props.onPress &&
99-
this.props.onPress((this.state.count / this.state.defaultRating) * (index + 1));
100-
}}
101-
key={index}
102-
>
103-
{item}
104-
</TouchableOpacity>
105-
);
106-
}
87+
) : (
88+
item
89+
)}
90+
</TouchableOpacity>
91+
);
10792
})}
10893
</View>
10994
</View>

0 commit comments

Comments
 (0)