Skip to content

Commit a99a047

Browse files
committed
fix(Typescript): correct type of rating onChange
1 parent a790e71 commit a99a047

File tree

2 files changed

+17
-62
lines changed

2 files changed

+17
-62
lines changed

src/rating/rating.jsx

+15-60
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ class Rating extends Component {
4848
allowHalf: PropTypes.bool,
4949
/**
5050
* 用户点击评分时触发的回调
51-
* @param {String} value 评分值
51+
* @param {Number} value 评分值
5252
*/
5353
onChange: PropTypes.func,
5454
/**
5555
* 用户hover评分时触发的回调
56-
* @param {String} value 评分值
56+
* @param {Number} value 评分值
5757
*/
5858
onHoverChange: PropTypes.func,
5959
/**
@@ -127,12 +127,7 @@ class Rating extends Component {
127127
};
128128
this.timer = null;
129129

130-
bindCtx(this, [
131-
'handleClick',
132-
'handleHover',
133-
'handleLeave',
134-
'onKeyDown',
135-
]);
130+
bindCtx(this, ['handleClick', 'handleHover', 'handleLeave', 'onKeyDown']);
136131
}
137132

138133
static getDerivedStateFromProps(nextProps, prevState) {
@@ -148,9 +143,7 @@ class Rating extends Component {
148143
'renderPreview' in nextProps
149144
) {
150145
state.disabled =
151-
nextProps.disabled ||
152-
nextProps.readOnly ||
153-
(nextProps.isPreview && !('renderPreview' in nextProps));
146+
nextProps.disabled || nextProps.readOnly || (nextProps.isPreview && !('renderPreview' in nextProps));
154147
}
155148

156149
return state;
@@ -179,9 +172,7 @@ class Rating extends Component {
179172

180173
if (icon && this.underlayNode) {
181174
const newIconSize = icon.offsetWidth;
182-
const newIconSpace =
183-
(this.underlayNode.offsetWidth - count * newIconSize) /
184-
(count + 1);
175+
const newIconSpace = (this.underlayNode.offsetWidth - count * newIconSize) / (count + 1);
185176

186177
if (newIconSize !== iconSize || newIconSpace !== iconSpace) {
187178
this.setState({
@@ -201,23 +192,16 @@ class Rating extends Component {
201192

202193
const pos = e.pageX - this.underlayNode.getBoundingClientRect().left;
203194
const fullNum = Math.floor(pos / (iconSpace + iconSize));
204-
const surplusNum =
205-
(pos - fullNum * (iconSpace + iconSize) - iconSpace) / iconSize;
195+
const surplusNum = (pos - fullNum * (iconSpace + iconSize) - iconSpace) / iconSize;
206196
let value = Number(fullNum) + Number(surplusNum.toFixed(1));
207197
if (value >= count) {
208198
value = count;
209199
} else if (allowHalf) {
210200
const floorValue = Math.floor(value);
211201
if (rtl) {
212-
value =
213-
value - 0.5 >= floorValue
214-
? floorValue + 1.5
215-
: floorValue + 1;
202+
value = value - 0.5 >= floorValue ? floorValue + 1.5 : floorValue + 1;
216203
} else {
217-
value =
218-
value - 0.5 >= floorValue
219-
? floorValue + 1
220-
: floorValue + 0.5;
204+
value = value - 0.5 >= floorValue ? floorValue + 1 : floorValue + 0.5;
221205
}
222206
} else {
223207
value = Math.floor(value) + 1;
@@ -337,12 +321,7 @@ class Rating extends Component {
337321
return 'auto';
338322
}
339323

340-
const value = Rating.currentValue(
341-
0,
342-
this.props.count,
343-
hoverValue,
344-
this.state.value
345-
);
324+
const value = Rating.currentValue(0, this.props.count, hoverValue, this.state.value);
346325

347326
const floorValue = Math.floor(value);
348327

@@ -387,12 +366,7 @@ class Rating extends Component {
387366
const enableA11y = !!id;
388367

389368
// 获得Value
390-
const value = Rating.currentValue(
391-
0,
392-
count,
393-
hoverValue,
394-
this.state.value
395-
);
369+
const value = Rating.currentValue(0, count, hoverValue, this.state.value);
396370

397371
// icon的sizeMap
398372
const sizeMap = ICON_SIZE_MAP[size];
@@ -407,23 +381,15 @@ class Rating extends Component {
407381
const iconNode = iconType ? (
408382
<Icon type={iconType} size={sizeMap} className={iconCls} />
409383
) : (
410-
<Icon
411-
type="favorites-filling"
412-
size={sizeMap}
413-
className={iconCls}
414-
/>
384+
<Icon type="favorites-filling" size={sizeMap} className={iconCls} />
415385
);
416386

417387
const saveRefs = ref => {
418388
this.saveRef(ref, i);
419389
};
420390

421391
underlay.push(
422-
<span
423-
ref={saveRefs}
424-
key={`underlay-${i}`}
425-
className={`${prefix}rating-icon`}
426-
>
392+
<span ref={saveRefs} key={`underlay-${i}`} className={`${prefix}rating-icon`}>
427393
{iconNode}
428394
</span>
429395
);
@@ -449,11 +415,7 @@ class Rating extends Component {
449415
className={`${prefix}rating-icon`}
450416
>
451417
{iconNode}
452-
{enableA11y ? (
453-
<span className={`${prefix}sr-only`}>
454-
{readAs(i + 1)}
455-
</span>
456-
) : null}
418+
{enableA11y ? <span className={`${prefix}sr-only`}>{readAs(i + 1)}</span> : null}
457419
</label>
458420
);
459421
}
@@ -518,17 +480,10 @@ class Rating extends Component {
518480
aria-label={locale.description}
519481
>
520482
<div className={baseCls} {...finalProps}>
521-
<div
522-
className={`${prefix}rating-underlay`}
523-
ref={n => (this.underlayNode = n)}
524-
aria-hidden
525-
>
483+
<div className={`${prefix}rating-underlay`} ref={n => (this.underlayNode = n)} aria-hidden>
526484
{underlay}
527485
</div>
528-
<div
529-
className={`${prefix}rating-overlay`}
530-
style={overlayStyle}
531-
>
486+
<div className={`${prefix}rating-overlay`} style={overlayStyle}>
532487
{overlay}
533488
</div>
534489
</div>

types/rating/index.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ export interface RatingProps extends HTMLAttributesWeak, CommonProps {
4242
/**
4343
* 用户点击评分时触发的回调
4444
*/
45-
onChange?: (value: string) => void;
45+
onChange?: (value: number) => void;
4646

4747
/**
4848
* 用户hover评分时触发的回调
4949
*/
50-
onHoverChange?: (value: string) => void;
50+
onHoverChange?: (value: number) => void;
5151

5252
/**
5353
* 是否禁用

0 commit comments

Comments
 (0)