From fa05ae1230079eeb1b87d900236ceb3260103e90 Mon Sep 17 00:00:00 2001 From: David Luzar <5153846+dwelle@users.noreply.github.com> Date: Wed, 22 Jan 2025 12:43:02 +0100 Subject: [PATCH] refactor: remove `defaultProps` (#9035) --- packages/excalidraw/components/ToolButton.tsx | 262 +++++++++--------- 1 file changed, 134 insertions(+), 128 deletions(-) diff --git a/packages/excalidraw/components/ToolButton.tsx b/packages/excalidraw/components/ToolButton.tsx index 30014d4b1..09b0b6cf8 100644 --- a/packages/excalidraw/components/ToolButton.tsx +++ b/packages/excalidraw/components/ToolButton.tsx @@ -55,146 +55,152 @@ type ToolButtonProps = onPointerDown?(data: { pointerType: PointerType }): void; }); -export const ToolButton = React.forwardRef((props: ToolButtonProps, ref) => { - const { id: excalId } = useExcalidrawContainer(); - const innerRef = React.useRef(null); - React.useImperativeHandle(ref, () => innerRef.current); - const sizeCn = `ToolIcon_size_${props.size}`; +export const ToolButton = React.forwardRef( + ( + { + size = "medium", + visible = true, + className = "", + ...props + }: ToolButtonProps, + ref, + ) => { + const { id: excalId } = useExcalidrawContainer(); + const innerRef = React.useRef(null); + React.useImperativeHandle(ref, () => innerRef.current); + const sizeCn = `ToolIcon_size_${size}`; - const [isLoading, setIsLoading] = useState(false); + const [isLoading, setIsLoading] = useState(false); - const isMountedRef = useRef(true); + const isMountedRef = useRef(true); - const onClick = async (event: React.MouseEvent) => { - const ret = "onClick" in props && props.onClick?.(event); + const onClick = async (event: React.MouseEvent) => { + const ret = "onClick" in props && props.onClick?.(event); - if (isPromiseLike(ret)) { - try { - setIsLoading(true); - await ret; - } catch (error: any) { - if (!(error instanceof AbortError)) { - throw error; - } else { - console.warn(error); - } - } finally { - if (isMountedRef.current) { - setIsLoading(false); + if (isPromiseLike(ret)) { + try { + setIsLoading(true); + await ret; + } catch (error: any) { + if (!(error instanceof AbortError)) { + throw error; + } else { + console.warn(error); + } + } finally { + if (isMountedRef.current) { + setIsLoading(false); + } } } - } - }; - - useEffect(() => { - isMountedRef.current = true; - return () => { - isMountedRef.current = false; }; - }, []); - const lastPointerTypeRef = useRef(null); + useEffect(() => { + isMountedRef.current = true; + return () => { + isMountedRef.current = false; + }; + }, []); + + const lastPointerTypeRef = useRef(null); + + if ( + props.type === "button" || + props.type === "icon" || + props.type === "submit" + ) { + const type = (props.type === "icon" ? "button" : props.type) as + | "button" + | "submit"; + return ( + + ); + } - if ( - props.type === "button" || - props.type === "icon" || - props.type === "submit" - ) { - const type = (props.type === "icon" ? "button" : props.type) as - | "button" - | "submit"; return ( - + { + props.onChange?.({ pointerType: lastPointerTypeRef.current }); + }} + checked={props.checked} + ref={innerRef} + /> +
+ {props.icon} + {props.keyBindingLabel && ( + + {props.keyBindingLabel} + + )} +
+ ); - } - - return ( - - ); -}); - -ToolButton.defaultProps = { - visible: true, - className: "", - size: "medium", -}; + }, +); ToolButton.displayName = "ToolButton";