Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rate): 修复Rate组件鼠标移走,未恢复到之前的状态的bug #2549

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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