Skip to content

Commit

Permalink
fix(cascader): cascader hover state error (#1931)
Browse files Browse the repository at this point in the history
  • Loading branch information
berber1016 committed Mar 28, 2022
1 parent c428149 commit 5cd947e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/list/inner/CascaderItem.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { DOMAttributes, useContext, useEffect, useMemo } from 'react';
import React, { DOMAttributes, useContext, useEffect, useMemo, useState } from 'react';
import { isEmpty } from 'lodash';
import classNames from 'classnames';
import { RightFilled } from '@gio-design/icons';
Expand All @@ -22,6 +22,8 @@ const CascaderItem: React.ForwardRefRenderFunction<
) => {
const prefixCls = usePrefixCls('cascader');
const popoverClassName = `${prefixCls}--content`;
const [hovered,setHovered] = useState(false);

/** context */
const context = useContext(ListContext);
const popoverContext = useContext(TriggerContext);
Expand Down Expand Up @@ -96,6 +98,7 @@ const CascaderItem: React.ForwardRefRenderFunction<
<TriggerContext.Provider value={popoverContext}>
<Popover
placement="rightTop"
onVisibleChange={(v) => setHovered(v)}
overlayClassName={popoverClassName}
// document click contains node
getContainer={() => document.body}
Expand Down Expand Up @@ -123,6 +126,7 @@ const CascaderItem: React.ForwardRefRenderFunction<
value={value}
disabled={mergedDisabled}
suffix={React.isValidElement(children) || !isEmpty(childrens) ? <RightFilled size="14px" /> : undefined}
hovered={hovered}
onClick={
React.isValidElement(children) || !isEmpty(childrens)
? (itemValue, event) => propsOnClick?.(itemValue, event)
Expand Down
23 changes: 21 additions & 2 deletions src/list/inner/baseItem.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import classNames from 'classnames';
import React, { DOMAttributes, ReactElement, useContext, useMemo } from 'react';
import React, { DOMAttributes, ReactElement, useContext, useEffect, useMemo, useState } from 'react';
import { isEmpty, isString } from 'lodash';
import usePrefixCls from '../../utils/hooks/use-prefix-cls';
import { PREFIX } from '../constants';
Expand All @@ -26,6 +26,9 @@ const InnerBaseItem = WithRef<HTMLLIElement, BaseItemProps & Omit<DOMAttributes<
onClick,
contentRender = defaultContentRender,
wrapper = defaultContentRender,
onMouseEnter,
onMouseLeave,
hovered:propsHovered,
...rest
} = props;
const prefixCls = `${usePrefixCls(PREFIX)}--item`;
Expand All @@ -39,6 +42,7 @@ const InnerBaseItem = WithRef<HTMLLIElement, BaseItemProps & Omit<DOMAttributes<
selectParent,
} = useContext(ListContext);
const mergedDisabled = disabled ?? contextDisabled;
const [hovered,setHovered] = useState(false);
const selected = useMemo(() => {
if (model === 'cascader') {
// 最顶级
Expand All @@ -53,6 +57,12 @@ const InnerBaseItem = WithRef<HTMLLIElement, BaseItemProps & Omit<DOMAttributes<
}
return selectStatus?.(value, contextValue);
}, [contextValue, model, selectParent, value]);

useEffect(() => () => {
setHovered(false)
}, [])


/** ============ prefix suffix ================ */
const prefix = useMemo(
() => propPrefix ?? contextPrefix?.({ label, value, disabled, disabledTooltip }),
Expand Down Expand Up @@ -102,12 +112,21 @@ const InnerBaseItem = WithRef<HTMLLIElement, BaseItemProps & Omit<DOMAttributes<
<li
data-testid="item-base"
style={style}
onMouseEnter={(e) => {
setHovered(true);
onMouseEnter?.(e);
}}
onMouseLeave={(e) => {
setHovered(false);
onMouseLeave?.(e);
}}
className={classNames(
className,
prefixCls,
{
[`${prefixCls}--disabled`]: mergedDisabled,
[`${prefixCls}--actived`]: selected,
[`${prefixCls}--hovered`]: !mergedDisabled ? (propsHovered || hovered) : false,
},
className
)}
Expand All @@ -126,7 +145,7 @@ const InnerBaseItem = WithRef<HTMLLIElement, BaseItemProps & Omit<DOMAttributes<
}
);
const BaseItem: React.ForwardRefExoticComponent<
BaseItemProps & Omit<React.RefAttributes<HTMLLIElement>, 'onClick' | 'onChange'>
BaseItemProps & Omit<React.RefAttributes<HTMLLIElement>, 'onClick' | 'onChange' | 'onMouseEnter' | 'onMouseLeave'>
> & {
isItem?: boolean;
} = InnerBaseItem;
Expand Down
5 changes: 5 additions & 0 deletions src/list/interfance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ export interface ItemProps
| 'selected'
| 'disabledTooltip'
| 'wrapper'
| 'onMouseLeave'
| 'onMouseEnter'
> {
selectValue?: string | string[];
strategy?: 'fixed' | 'absolute';
Expand All @@ -142,7 +144,10 @@ export interface BaseItemProps extends Pick<OptionProps, 'value' | 'disabled' |
children?: React.ReactNode;
disabledTooltip?: string;
selected?: boolean;
hovered?: boolean;
onClick?: (value?: string | number, event?: React.MouseEvent<HTMLLIElement | HTMLInputElement>) => void;
onMouseEnter?: (event?: React.MouseEvent<HTMLLIElement | HTMLInputElement>) => void;
onMouseLeave?: (event?: React.MouseEvent<HTMLLIElement | HTMLInputElement>) => void;
}

export interface TriggerProps {
Expand Down
3 changes: 3 additions & 0 deletions src/list/style/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@
&:not(&--disabled):hover {
background-color: @gray-1;
}
&--hovered:not(&--disabled):not(&--multiple) {
background-color: @gray-1;
}
&:not(&--disabled):not(&--drag):active {
color: @blue-3;
}
Expand Down

1 comment on commit 5cd947e

@vercel
Copy link

@vercel vercel bot commented on 5cd947e Mar 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.