Basic
This is the most basic example. A grid, 500px wide and 500px high is created, since those are the default props.
tip
Click and drag on any square in the grid to create a new bar
The code
So whats going on here? In order to allow you to control the state of Reserver we use a reducer
useReserver is a the hook. reserverReducer is the reducer.
The hook returns:
- bars which is an array of objects representing the bars
- addBar a function which takes a single object to add to the array
- setBars a function which sets all the bars
There are more functions returned from the hook. We will start with these.
mouseDownCell is the onMouseDown for all each cell. The prop parameter receives the dimension of the cell and the location in the object cell
The objects dimension and cell get passed to the function createBar
createBar is a helper function that takes dimension and cell as arguments and returns an object containing a new id, the dimension, editing as a boolean set to true, the location which is the cell. All these get passed as props into the bar and are necessary as basic properties for the bars array.
addBar is a function that takes an object representing bar and adds it to the array bars.
mouseEnterCell takes a function that runs when the mouse enters a cell
resizeBar is a function that takes all bars and the props and calculates the new size of the bars that have the property editing = true.
mouseUpCell takes a function that runs on mouse up on a cell Here, the bars are mapped over and all the edited bars that have true on editing becomes false. The other option is to just use finishEditingBars
The children of the Reserver component are an array of the component Bar.
getPosition is a helper function that takes the row, column and dimension and calculates the absolute position (top and left) of the bar and passes it into style.
cellClassName is the className that is passed to all cells. by default it is empty so if you dont add a style it will be invisible
Thats it! Thats the most basic code that allows you to run the example.
caution
This is not the best way to use Reserver. Its only an example to simplify the process of getting started.
tip
Go to Basic++ to see a more robust example of how to use Reserver