-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
79 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,50 @@ | ||
import React, { useCallback, useRef, useState } from 'react'; | ||
import type { ProFieldLightProps } from '../index'; | ||
|
||
function fieldHOC<T extends ProFieldLightProps>( | ||
FieldComponent: React.ForwardRefExoticComponent<T>, | ||
function FieldHOC<T extends ProFieldLightProps>( | ||
props: T & { | ||
children: React.ReactNode; | ||
}, | ||
) { | ||
function Wrap(props: T, ref: React.ForwardedRef<any>) { | ||
const [labelTrigger, setLabelTrigger] = useState(false); | ||
const [labelTrigger, setLabelTrigger] = useState(false); | ||
|
||
const lightLabel = useRef<{ | ||
labelRef: React.RefObject<HTMLElement>; | ||
clearRef: React.RefObject<HTMLElement>; | ||
}>(null); | ||
const lightLabel = useRef<{ | ||
labelRef: React.RefObject<HTMLElement>; | ||
clearRef: React.RefObject<HTMLElement>; | ||
}>(null); | ||
|
||
// 是label且不是label里面的clear图标触发事件 | ||
const isTriggeredByLabel = useCallback( | ||
(e: React.MouseEvent) => { | ||
// 两条语句结果分别命名,可读性好点 | ||
const isLabelMouseDown = lightLabel.current?.labelRef?.current?.contains( | ||
e.target as HTMLElement, | ||
); | ||
const isClearMouseDown = lightLabel.current?.clearRef?.current?.contains( | ||
e.target as HTMLElement, | ||
); | ||
return isLabelMouseDown && !isClearMouseDown; | ||
}, | ||
[lightLabel], | ||
); | ||
// 是label且不是label里面的clear图标触发事件 | ||
const isTriggeredByLabel = useCallback( | ||
(e: React.MouseEvent) => { | ||
// 两条语句结果分别命名,可读性好点 | ||
const isLabelMouseDown = lightLabel.current?.labelRef?.current?.contains( | ||
e.target as HTMLElement, | ||
); | ||
const isClearMouseDown = lightLabel.current?.clearRef?.current?.contains( | ||
e.target as HTMLElement, | ||
); | ||
return isLabelMouseDown && !isClearMouseDown; | ||
}, | ||
[lightLabel], | ||
); | ||
|
||
const handleMouseDown = (e: React.MouseEvent<HTMLDivElement, MouseEvent>) => { | ||
if (isTriggeredByLabel(e)) { | ||
setLabelTrigger(true); | ||
} | ||
}; | ||
const handleMouseUp = () => { | ||
setLabelTrigger(false); | ||
}; | ||
return ( | ||
<div onMouseDown={handleMouseDown} onMouseUp={handleMouseUp}> | ||
<FieldComponent {...props} labelTrigger={labelTrigger} lightLabel={lightLabel} ref={ref} /> | ||
</div> | ||
); | ||
} | ||
return React.forwardRef(Wrap); | ||
const handleMouseDown = (e: React.MouseEvent<HTMLDivElement, MouseEvent>) => { | ||
if (isTriggeredByLabel(e)) { | ||
setLabelTrigger(true); | ||
} | ||
}; | ||
const handleMouseUp = () => { | ||
setLabelTrigger(false); | ||
}; | ||
|
||
return ( | ||
<div onMouseDown={handleMouseDown} onMouseUp={handleMouseUp}> | ||
{React.cloneElement(props.children as JSX.Element, { | ||
labelTrigger, | ||
lightLabel, | ||
})} | ||
</div> | ||
); | ||
} | ||
|
||
export default fieldHOC; | ||
export default FieldHOC; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters