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 } } 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 || 300); const [height, setHeight] = useState(defBase?.height || 100); const { mainColor = "#83BFF6", subColor = "#00CED1", reverse } = style || {}; useImperativeHandle(ref, () => ({ updateConfig: (newConfig) => { setStyle({ ...newConfig.style }); setWidth(newConfig.base.width); setHeight(newConfig.base.height); }, })); const xPos = pos => (!reverse ? pos : width - pos) const [pointsOne, pointsTwo, pointsThree] = useMemo( () => [ `${xPos(0)}, 0 ${xPos(30)}, ${height / 2}`, `${xPos(20)}, 0 ${xPos(50)}, ${height / 2} ${xPos(width)}, ${height / 2}`, `${xPos(0)}, ${height - 3}, ${xPos(200)}, ${height - 3}` ], [reverse, width, height] ) return
; }); export default Index;