Skip to content

Commit

Permalink
fix(rate): 修复Rate组件鼠标移走,未恢复到之前的状态的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon-Millent committed Oct 17, 2023
1 parent 5f4f5cf commit 11d8ac9
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/rate/Rate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const Rate = forwardRef((props: RateProps, ref: React.Ref<HTMLDivElement>) => {
const [hoverValue = undefined, setHoverValue] = useState<number | undefined>(undefined);
const displayValue = hoverValue || starValue;
const rootRef = React.useRef(null);
const hoverCheckRef = React.useRef(null);
const checkTimeHandler = React.useRef(null);

const activeColor = Array.isArray(color) ? color[0] : color;
const defaultColor = Array.isArray(color) ? color[1] : 'var(--td-bg-color-component)';
Expand Down Expand Up @@ -58,6 +60,19 @@ const Rate = forwardRef((props: RateProps, ref: React.Ref<HTMLDivElement>) => {
setHoverValue(undefined);
};

const mouseOutHandler = () => {
if (disabled) return;
// fix: 2250 鼠标移走,未恢复到之前的状态
clearTimeout(checkTimeHandler.current);
checkTimeHandler.current = setTimeout(() => {
const computedStyle = getComputedStyle(hoverCheckRef.current);
const fontSize = computedStyle.getPropertyValue('font-size');
if (fontSize === '0px') {
setHoverValue(undefined);
}
}, 100);
};

const clickHandler = (event: MouseEvent<HTMLElement>, index: number) => {
if (disabled) return;
setStarValue(getStarValue(event, index));
Expand All @@ -75,6 +90,7 @@ const Rate = forwardRef((props: RateProps, ref: React.Ref<HTMLDivElement>) => {
style={style}
className={classNames(`${classPrefix}-rate`, className)}
onMouseLeave={mouseLeaveHandler}
onMouseOut={mouseOutHandler}
>
<ul className={`${classPrefix}-rate__list`} style={{ gap }} ref={rootRef}>
{[...Array(count)].map((_, index) => (
Expand Down Expand Up @@ -106,6 +122,7 @@ const Rate = forwardRef((props: RateProps, ref: React.Ref<HTMLDivElement>) => {
</li>
))}
</ul>
<span ref={hoverCheckRef} className={`${classPrefix}-rate__hover__check`}></span>
{showText && <div className={`${classPrefix}-rate__text`}>{texts[displayValue - 1]}</div>}
</div>
);
Expand Down

0 comments on commit 11d8ac9

Please sign in to comment.