Skip to content

Commit 86c7dc5

Browse files
Added objects limit
1 parent cd2c5d1 commit 86c7dc5

File tree

2 files changed

+62
-40
lines changed

2 files changed

+62
-40
lines changed

examples/interactive2-layer-and-step-filtering.fixture.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import exampleGraphics from "site/assets/exampleGraphics.json"
44
export default () => {
55
return (
66
<InteractiveGraphics
7+
objectLimit={2}
78
graphics={{
89
rects: [
910
{

site/components/InteractiveGraphics/InteractiveGraphics.tsx

Lines changed: 61 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ export type GraphicsObjectClickEvent = {
2929
export const InteractiveGraphics = ({
3030
graphics,
3131
onObjectClicked,
32+
objectLimit,
3233
}: {
3334
graphics: GraphicsObject
3435
onObjectClicked?: (event: GraphicsObjectClickEvent) => void
36+
objectLimit?: number
3537
}) => {
3638
const [activeLayers, setActiveLayers] = useState<string[] | null>(null)
3739
const [activeStep, setActiveStep] = useState<number | null>(null)
@@ -144,6 +146,18 @@ export const InteractiveGraphics = ({
144146
size,
145147
)
146148

149+
const filteredObjects = [
150+
...(graphics.lines?.filter(filterLines) ?? []),
151+
...(graphics.rects?.filter(filterRects) ?? []),
152+
...(graphics.points?.filter(filterPoints) ?? []),
153+
...(graphics.circles?.filter(filterCircles) ?? []),
154+
]
155+
156+
const limitedObjects = objectLimit
157+
? filteredObjects.slice(0, objectLimit)
158+
: filteredObjects
159+
const isLimitReached = objectLimit && filteredObjects.length > objectLimit
160+
147161
return (
148162
<div>
149163
{showToolbar && (
@@ -193,6 +207,12 @@ export const InteractiveGraphics = ({
193207
/>
194208
Filter by step
195209
</label>
210+
{isLimitReached && (
211+
<span style={{ color: "red", fontSize: "12px" }}>
212+
Display limited to {objectLimit} objects. Received:{" "}
213+
{filteredObjects.length}.
214+
</span>
215+
)}
196216
</div>
197217
)}
198218
</div>
@@ -207,46 +227,47 @@ export const InteractiveGraphics = ({
207227
}}
208228
>
209229
<DimensionOverlay transform={realToScreen}>
210-
{graphics.lines?.map((l, originalIndex) =>
211-
filterLines(l) ? (
212-
<Line
213-
key={originalIndex}
214-
line={l}
215-
index={originalIndex}
216-
interactiveState={interactiveState}
217-
/>
218-
) : null,
219-
)}
220-
{graphics.rects?.map((r, originalIndex) =>
221-
filterRects(r) ? (
222-
<Rect
223-
key={originalIndex}
224-
rect={r}
225-
index={originalIndex}
226-
interactiveState={interactiveState}
227-
/>
228-
) : null,
229-
)}
230-
{graphics.points?.map((p, originalIndex) =>
231-
filterPoints(p) ? (
232-
<Point
233-
key={originalIndex}
234-
point={p}
235-
index={originalIndex}
236-
interactiveState={interactiveState}
237-
/>
238-
) : null,
239-
)}
240-
{graphics.circles?.map((c, originalIndex) =>
241-
filterCircles(c) ? (
242-
<Circle
243-
key={originalIndex}
244-
circle={c}
245-
index={originalIndex}
246-
interactiveState={interactiveState}
247-
/>
248-
) : null,
249-
)}
230+
{limitedObjects.map((obj, index) => {
231+
if ("points" in obj) {
232+
return (
233+
<Line
234+
key={index}
235+
line={obj}
236+
index={index}
237+
interactiveState={interactiveState}
238+
/>
239+
)
240+
} else if ("width" in obj && "height" in obj) {
241+
return (
242+
<Rect
243+
key={index}
244+
rect={obj}
245+
index={index}
246+
interactiveState={interactiveState}
247+
/>
248+
)
249+
} else if ("x" in obj && "y" in obj) {
250+
return (
251+
<Point
252+
key={index}
253+
point={obj}
254+
index={index}
255+
interactiveState={interactiveState}
256+
/>
257+
)
258+
} else if ("radius" in obj) {
259+
return (
260+
<Circle
261+
key={index}
262+
circle={obj}
263+
index={index}
264+
interactiveState={interactiveState}
265+
/>
266+
)
267+
} else {
268+
return null
269+
}
270+
})}
250271
<SuperGrid
251272
stringifyCoord={(x, y) => `${x.toFixed(2)}, ${y.toFixed(2)}`}
252273
width={size.width}

0 commit comments

Comments
 (0)