Skip to content

Commit

Permalink
Merge pull request #1317 from gemini-hlsw/table-multi-row-handler
Browse files Browse the repository at this point in the history
General multi row handler for tables
  • Loading branch information
rpiaggio authored Oct 23, 2024
2 parents 9c9ee78 + 1325fdf commit 9e3fc74
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion modules/ui/src/main/scala/lucuma/ui/syntax/all.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

package lucuma.ui.syntax

object all extends render, components, pot, pprint, mod, css, util, view, effect, toast
object all extends render, components, pot, pprint, mod, css, util, view, effect, toast, table
50 changes: 50 additions & 0 deletions modules/ui/src/main/scala/lucuma/ui/syntax/table.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) 2016-2023 Association of Universities for Research in Astronomy, Inc. (AURA)
// For license information see LICENSE or https://opensource.org/licenses/BSD-3-Clause

package lucuma.ui.syntax

import cats.syntax.eq.*
import japgolly.scalajs.react.Callback
import japgolly.scalajs.react.ReactMouseEvent
import lucuma.react.table.*

trait table:
extension [T, TM](row: Row[T, TM])
def getMultiRowSelectedHandler(table: Table[T, TM]): ReactMouseEvent => Callback =
(e: ReactMouseEvent) =>
val isShiftPressed: Boolean = e.shiftKey
val isCmdCtrlPressed: Boolean = e.metaKey || e.ctrlKey

val selectedRows: List[Row[T, TM]] = table.getSelectedRowModel().rows

// If cmd is pressed add to the selection
table.toggleAllRowsSelected(false).unless(isCmdCtrlPressed) >> (
if (isShiftPressed && selectedRows.nonEmpty) {
// If shift is pressed extend
val allRows: List[(Row[T, TM], Int)] = table.getRowModel().rows.zipWithIndex
val currentId: RowId = row.id
// selectedRow is not empty, these won't fail
val firstId: RowId = selectedRows.head.id
val lastId: RowId = selectedRows.last.id
val indexOfCurrent: Int = allRows.indexWhere(_._1.id == currentId)
val indexOfFirst: Int = allRows.indexWhere(_._1.id == firstId)
val indexOfLast: Int = allRows.indexWhere(_._1.id == lastId)
if (indexOfCurrent =!= -1 && indexOfFirst =!= -1 && indexOfLast =!= -1)
table.setRowSelection:
if (indexOfCurrent < indexOfFirst)
RowSelection(
allRows
.slice(indexOfCurrent, indexOfLast + 1)
.map { case (row, _) => row.id -> true }*
)
else
RowSelection(
allRows
.slice(indexOfFirst, indexOfCurrent + 1)
.map { case (row, _) => row.id -> true }*
)
else Callback.empty
} else row.toggleSelected()
)

object table extends table

0 comments on commit 9e3fc74

Please sign in to comment.