-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
95 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 0 additions & 24 deletions
24
...anstack-react-table/src/jsMain/kotlin/wrappers/example/table/selection/SelectionHeader.kt
This file was deleted.
Oops, something went wrong.
27 changes: 27 additions & 0 deletions
27
...ck-react-table/src/jsMain/kotlin/wrappers/example/table/selection/TableSelectionModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package wrappers.example.table.selection | ||
|
||
import preact.signals.react.useSignal | ||
import react.FC | ||
import react.PropsWithChildren | ||
import react.useCallback | ||
import wrappers.example.entities.Key | ||
|
||
internal val TableSelectionModule: FC<PropsWithChildren> = FC { props -> | ||
val selection = useSignal(EMPTY_SELECTION) | ||
|
||
val selectionHandler: SelectionHandler<Key> = useCallback { keys -> | ||
val (add, remove) = keys.entries | ||
.partition { it.value } | ||
|
||
selection.value = selection.value - remove.toSelectedKeys() + add.toSelectedKeys() | ||
} | ||
|
||
SelectedKeysContext(selection) { | ||
SelectionHandlerContext(selectionHandler) { | ||
+props.children | ||
} | ||
} | ||
} | ||
|
||
private fun List<Map.Entry<Key, Boolean>>.toSelectedKeys(): SelectedKeys = | ||
map { it.key }.toSet() |
20 changes: 8 additions & 12 deletions
20
...k-react-table/src/jsMain/kotlin/wrappers/example/table/selection/createSelectionColumn.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,34 @@ | ||
package wrappers.example.table.selection | ||
|
||
import js.array.ReadonlyArray | ||
import js.objects.jso | ||
import preact.signals.core.Signal | ||
import preact.signals.core.signal | ||
import react.create | ||
import tanstack.table.core.ColumnDef | ||
import tanstack.table.core.ColumnDefTemplate | ||
import tanstack.table.core.StringOrTemplateHeader | ||
|
||
internal typealias SelectedKeys = ReadonlyArray<String> | ||
|
||
internal val selectedKeys: Signal<SelectedKeys> = signal(emptyArray()) | ||
internal val EMPTY_SELECTION: SelectedKeys = emptySet() | ||
internal typealias SelectedKeys = Set<String> | ||
|
||
internal fun <T : Any> createSelectionColumn(): ColumnDef<T, String> = | ||
jso { | ||
id = "selection" | ||
size = 32 | ||
header = StringOrTemplateHeader( | ||
ColumnDefTemplate { context -> | ||
val allRowIds = context.table.getRowModel().rows | ||
val keys = context.table.getRowModel().rows | ||
.map { it.id } | ||
.toTypedArray() | ||
.toSet() | ||
|
||
SelectionHeader.create { | ||
value = allRowIds | ||
SelectionCell.create { | ||
value = keys | ||
} | ||
} | ||
) | ||
cell = ColumnDefTemplate { context -> | ||
val row = context.cell.row | ||
val keys = setOf(context.cell.row.id) | ||
|
||
SelectionCell.create { | ||
value = row | ||
value = keys | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...s/tanstack-react-table/src/jsMain/kotlin/wrappers/example/table/selection/useIsChecked.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package wrappers.example.table.selection | ||
|
||
import preact.signals.core.ReadonlySignal | ||
import preact.signals.react.useComputed | ||
|
||
internal fun useIsChecked(keys: SelectedKeys): ReadonlySignal<Boolean> { | ||
val selection = useSelectedKeys() | ||
|
||
return useComputed { | ||
selection.value.containsAll(keys) | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...anstack-react-table/src/jsMain/kotlin/wrappers/example/table/selection/useSelectedKeys.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package wrappers.example.table.selection | ||
|
||
import preact.signals.core.Signal | ||
import react.RequiredContext | ||
import react.createRequiredContext | ||
import react.useRequired | ||
|
||
internal val SelectedKeysContext: RequiredContext<Signal<SelectedKeys>> = | ||
createRequiredContext() | ||
|
||
internal fun useSelectedKeys(): Signal<SelectedKeys> = | ||
useRequired(SelectedKeysContext) |
13 changes: 13 additions & 0 deletions
13
...act-table/src/jsMain/kotlin/wrappers/example/table/selection/useSelectionChangeHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package wrappers.example.table.selection | ||
|
||
import react.dom.events.ChangeEvent | ||
import react.useCallback | ||
import web.html.HTMLInputElement | ||
|
||
internal fun useSelectionChangeHandler(keys: SelectedKeys): (ChangeEvent<HTMLInputElement>) -> Unit { | ||
val selectionHandler = useSelectionHandler() | ||
|
||
return useCallback(keys) { event -> | ||
selectionHandler(keys.associateWith { event.target.checked }) | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...ack-react-table/src/jsMain/kotlin/wrappers/example/table/selection/useSelectionHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package wrappers.example.table.selection | ||
|
||
import react.RequiredContext | ||
import react.createRequiredContext | ||
import react.useRequired | ||
import wrappers.example.entities.Key | ||
|
||
typealias SelectionHandler<T> = (Map<T, Boolean>) -> Unit | ||
|
||
internal val SelectionHandlerContext: RequiredContext<SelectionHandler<Key>> = | ||
createRequiredContext() | ||
|
||
internal fun useSelectionHandler(): SelectionHandler<Key> = | ||
useRequired(SelectionHandlerContext) |