import React, { ForwardedRef, forwardRef, useImperativeHandle, useState, useMemo } from "react"; import "./index.less"; const pointSideLength = 2.5; const halfPointSideLength = pointSideLength / 2; const svgWH = [200, 50]; const rowNum = 4; const rowPoints = 20; const getPoints = () => { const [w, h] = svgWH; const horizontalGap = w / (rowPoints + 1); const verticalGap = h / (rowNum + 1); const points = new Array(rowNum) .fill(0) .map((_, i) => new Array(rowPoints) .fill(0) .map((_, j) => [horizontalGap * (j + 1), verticalGap * (i + 1)]) ); return points.reduce((all, item) => [...all, ...item], []); } interface ComponentStyle { mainColor?: string; subColor?: string; } 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" } = style || {}; useImperativeHandle(ref, () => ({ updateConfig: (newConfig) => { setStyle({ ...newConfig.style }); setWidth(newConfig.base.width); setHeight(newConfig.base.height); }, })); const calcSVGData = () => { const points = getPoints(); return { points: points.reduce((prev, point, i) => Math.random() > 0.6 ? [ ...prev, {Math.random() > 0.6 && ( )} ] as any : prev, []), rects: [points[rowPoints * 2 - 1], points[rowPoints * 2 - 3]], svgScale: [width / svgWH[0], height / svgWH[1]] }; } const { svgScale, points, rects } = useMemo(calcSVGData, [width, height, mainColor]); return
{points} {!!rects[0] && ( )} {!!rects[1] && ( )}
; }); export default Index;