I have a grid structure like this (for a weekly calendar):
<div className="grid flex-auto grid-cols-1 grid-rows-1">
{/* Horizontal lines */}
<div
className="col-start-1 col-end-2 row-start-1 grid divide-y divide-gray-100"
style={{ gridTemplateRows: 'repeat(48, minmax(3.5rem, 1fr))' }}
>
<div className="sticky left-0 z-20 -ml-14 -mt-2.5 w-14 pr-2 text-right text-xs leading-5 text-gray-400">
12AM
</div>
<div />
<div className="sticky left-0 z-20 -ml-14 -mt-2.5 w-14 pr-2 text-right text-xs leading-5 text-gray-400">
1AM
</div>
{/* More rows here like this for 24 hours */}
</div>
{/* Vertical lines */}
<div className="col-start-1 col-end-2 row-start-1 hidden grid-cols-7 grid-rows-1 divide-x divide-gray-100 sm:grid sm:grid-cols-7">
<div className="col-start-1 row-span-full" />
<div className="col-start-2 row-span-full" />
<div className="col-start-3 row-span-full" />
<div className="col-start-4 row-span-full" />
<div className="col-start-5 row-span-full" />
<div className="col-start-6 row-span-full" />
<div className="col-start-7 row-span-full" />
<div className="col-start-8 row-span-full w-8" />
</div>
<Events events={params.events} />
</div>
I am trying to add a click event that will capture where in the grid I clicked (so I know the time and day) and that will open a Dialog for further action.
If I add the event to specific time, then I dont know the day of the week, and if I add event to day of week, I don’t know the time.
Any ideas on how to approach capturing both of these onClick?
Added Codepen: https://codepen.io/eventslooped/pen/KKbbpBw
2
Answers
To capture the grid where you clicked, Add an onClick event to the grid element. In handleGridClick function, We can use the
event.clientX
andevent.clientY
properties to get the click coordinates with that you can get the respective grid where you clicked.Fixed it. Check it at https://codepen.io/rachitiitr/pen/eYbbpgG?editors=0010
Added the below
onClick
to the entire grid, then figured out where the user clicked (x,y) relative to grid and used grid’s height and width to figure out the rowIndex and colIndex where click happened.Then rowIndex can tell the time while colIndex can tell the day of week.