import React, { ForwardedRef, forwardRef, useImperativeHandle, useState, useMemo } from "react"; import "./index.less"; interface ComponentStyle { mainColor?: string; subColor?: string; reverse?: boolean; } export interface ComponentProps { style?: ComponentStyle; base?: { width?: number, height?: number, id: string } } export interface ComponentRef { updateConfig: (config: ComponentProps) => void; } const Index = forwardRef((props: ComponentProps, ref: ForwardedRef) => { const { style: defStyle, base: defBase } = props; const [style, setStyle] = useState({ ...defStyle }); const [width, setWidth] = useState(defBase?.width || 320); const [height, setHeight] = useState(defBase?.height || 200); const { mainColor = "#83BFF6", subColor = "#00CED1", reverse } = style || {}; const [{ path, gradient, mask }] = useState(() => { const id = props.base.id; return { path: `border-box-8-path-${id}`, gradient: `border-box-8-gradient-${id}`, mask: `border-box-8-mask-${id}` } }); const pathD = useMemo(() => reverse ? `M 2.5, 2.5 L 2.5, ${height - 2.5} L ${width - 2.5}, ${height - 2.5} L ${width - 2.5}, 2.5 L 2.5, 2.5` : `M2.5, 2.5 L${width - 2.5}, 2.5 L${width - 2.5}, ${height - 2.5} L2.5, ${height - 2.5} L2.5, 2.5` , [width, height, reverse]); const length = useMemo(() => (width + height - 5) * 2, [width, height]); useImperativeHandle(ref, () => ({ updateConfig: (newConfig) => { const { width, height } = newConfig.base; setStyle({ ...newConfig.style }); setWidth(width); setHeight(height); }, })); return
; }); export default Index;