Overview
TheInputPoint component provides a user interface for adding new points or centroids to the clustering visualization. It supports both manual coordinate entry and random point generation.
Props
Callback function that receives the new point when the user clicks “Add” or “Random”. This function should handle adding the point to the application state.
The label for the component, typically “Point” or “Centroid”. This appears in the heading and button labels.
Point Type
The component uses the globalPoint type:
Usage
Basic Example
For Centroids
Features
Manual Input Mode
Users can enter specific X and Y coordinates using numeric input fields:- Input fields accept values between -1000 and 1000
- Uses Ant Design’s
InputNumbercomponent for validation - Clicking “Add ” creates a point with the entered coordinates
- Input fields are cleared after successful addition
Random Input Mode
Users can generate random points without manual input:- Clicking “Random ” generates a random point
- Uses the
generateRandomPointsutility function - Automatically adds the generated point without requiring coordinate input
- Useful for quickly populating the visualization with test data
Component State
The component maintains internal state for the input fields:xAxis- Current X coordinate value (number or undefined)yAxis- Current Y coordinate value (number or undefined)
undefined and are reset after adding a point.
UI Layout
The component renders:- Heading - “Add ” using the provided title prop
- Input Fields - Two numeric inputs for X and Y coordinates, side by side
- Action Buttons - Two buttons:
- “Add ” - Submits manually entered coordinates
- “Random ” - Generates and submits a random point
Error Handling
The component includes basic error handling:- Try-catch blocks around the action callback
- Errors are logged to the console
- Input state is only cleared on successful point addition
- Numeric inputs enforce min/max constraints (-1000 to 1000)
Source Code
View the full implementation in the repository:src/components/InputPoint.tsx