Skip to content

Commit

Permalink
Add pagination position (#100)
Browse files Browse the repository at this point in the history
Co-authored-by: hfhbd <[email protected]>
  • Loading branch information
hfhbd and hfhbd authored Aug 18, 2021
1 parent 1c7d4e4 commit eb95fff
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/jsMain/kotlin/app/softwork/bootstrapcompose/Table.kt
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ public object Table {
)

public interface Pagination<T> {
public enum class Position {
Top, Bottom
}

public val position: Position
public fun pages(data: List<T>): List<Page<T>>

public val numberOfButtons: Int
Expand All @@ -133,7 +138,7 @@ public object Table {
{ pages, currentPage, goTo ->
Column(horizontalAlignment = HorizontalAlignment.Start) {
ButtonGroup {
Button(title = "<", disabled = currentPage.index == 0) {
Button(title = "<", disabled = currentPage.index == 0, size = ButtonSize.Small) {
val previousIndex = currentPage.index - 1
actionNavigateBack?.invoke(currentPage, pages[previousIndex])
goTo(previousIndex)
Expand All @@ -147,9 +152,9 @@ public object Table {

for (index in buttons) {
if (index == currentPage.index) {
Button(title = "$index", disabled = true) { }
Button(title = "$index", disabled = true, size = ButtonSize.Small) { }
} else {
Button(title = "$index") {
Button(title = "$index", size = ButtonSize.Small) {
if (index < currentPage.index) {
actionNavigateBack?.invoke(currentPage, pages[index])
goTo(index)
Expand All @@ -161,7 +166,7 @@ public object Table {
}
}

Button(title = ">", disabled = currentPage.index == pages.lastIndex) {
Button(title = ">", disabled = currentPage.index == pages.lastIndex, size = ButtonSize.Small) {
val nextIndex = currentPage.index + 1
actionNavigateForward?.invoke(currentPage, pages[nextIndex])
goTo(nextIndex)
Expand All @@ -186,6 +191,7 @@ public object Table {
}

public class OffsetPagination<T>(
public override val position: Pagination.Position = Pagination.Position.Bottom,
public override val entriesPerPageLimit: State<Int>,
public override val numberOfButtons: Int = 5,
public override val actionNavigateBack: ((CurrentPage<T>, Pagination.Page<T>) -> Unit)? = null,
Expand All @@ -195,12 +201,13 @@ public object Table {
override var control: PageControl<T> = defaultControl()

public constructor(
position: Pagination.Position = Pagination.Position.Bottom,
entriesPerPageLimit: State<Int>,
numberOfButtons: Int = 5,
actionNavigateBack: ((CurrentPage<T>, Pagination.Page<T>) -> Unit)? = null,
actionNavigateForward: ((CurrentPage<T>, Pagination.Page<T>) -> Unit)? = null,
control: PageControl<T>
) : this(entriesPerPageLimit, numberOfButtons, actionNavigateBack, actionNavigateForward) {
) : this(position, entriesPerPageLimit, numberOfButtons, actionNavigateBack, actionNavigateForward) {
this.control = control
}

Expand Down Expand Up @@ -281,6 +288,15 @@ public fun <T> Table(
check(rows.all { it.cells.size == footers.size })
}

if (pagination != null && pagination.position == Table.Pagination.Position.Top) {
Row {
val control = pagination.control
control(pages, currentPage) {
currentIndex = it
}
}
}

Table(attrs = {
classes("table")
if (captionTop) {
Expand Down Expand Up @@ -355,7 +371,7 @@ public fun <T> Table(
}
}
}
if (pagination != null) {
if (pagination != null && pagination.position == Table.Pagination.Position.Bottom) {
Row {
val control = pagination.control
control(pages, currentPage) {
Expand Down

0 comments on commit eb95fff

Please sign in to comment.