Skip to content

Commit a7e7f0f

Browse files
committed
fix #1094 Datatable SortPlugin: Sort Icon does not represent the current sort state when triggering via clicking the column header
1 parent 79f7350 commit a7e7f0f

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

domino-ui/src/main/java/org/dominokit/domino/ui/datatable/plugins/pagination/SortDirection.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,19 @@ public enum SortDirection {
4848
*
4949
* @see org.dominokit.domino.ui.datatable.plugins.pagination.SortPlugin
5050
*/
51-
NONE
51+
NONE;
52+
53+
public SortDirection next(boolean triState) {
54+
if (this == NONE) return ASC;
55+
if (this == ASC) return DESC;
56+
if (this == DESC) return triState ? NONE : ASC;
57+
return NONE;
58+
}
59+
60+
public SortDirection previous(boolean triState) {
61+
if (this == NONE) return DESC;
62+
if (this == DESC) return ASC;
63+
if (this == ASC) return triState ? NONE : DESC;
64+
return NONE;
65+
}
5266
}

domino-ui/src/main/java/org/dominokit/domino/ui/datatable/plugins/pagination/SortPlugin.java

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@
1818

1919
import static java.util.Objects.nonNull;
2020
import static org.dominokit.domino.ui.datatable.plugins.PluginsConstants.DUI_DT_COL_RESIZING;
21-
import static org.dominokit.domino.ui.utils.Domino.*;
2221

2322
import elemental2.dom.HTMLElement;
2423
import java.util.HashMap;
2524
import java.util.Map;
26-
import java.util.Optional;
2725
import java.util.function.Consumer;
2826
import org.dominokit.domino.ui.datatable.ColumnConfig;
2927
import org.dominokit.domino.ui.datatable.DataTable;
@@ -81,11 +79,7 @@ public void onHeaderAdded(DataTable<T> dataTable, ColumnConfig<T> column) {
8179
EventType.click.getName(),
8280
evt -> {
8381
applySort(
84-
column,
85-
sortContext,
86-
Optional.ofNullable(currentSortContext)
87-
.map(c -> c.sortDirection)
88-
.orElse(SortDirection.DESC));
82+
column, sortContext, sortContext.sortDirection.next(config.isTriStateSort()));
8983
});
9084
if (config.isShowSortOptionsInColumnMenu()) {
9185
column
@@ -121,18 +115,18 @@ private void applySort(
121115
if (config.isShowIconOnSortedColumnOnly() && nonNull(currentSortContext)) {
122116
currentSortContext.sortElement.clearElement();
123117
}
124-
sortContext.sortElement.appendChild(sortContext.sortIcon);
125-
updateSort(sortContext);
118+
updateSort(sortContext, sortDirection);
126119
fireSortEvent(sortDirection, column);
120+
currentSortContext.sortElement.appendChild(sortContext.sortIcon);
127121
}
128122
}
129123

130-
private void updateSort(SortContext sortContext) {
124+
private void updateSort(SortContext sortContext, SortDirection sortDirection) {
131125
if (nonNull(currentSortContext)
132126
&& !currentSortContext.columnName.equals(sortContext.columnName)) {
133127
currentSortContext.clear();
134128
}
135-
sortContext.update(true);
129+
sortContext.update(sortDirection);
136130
currentSortContext = sortContext;
137131
}
138132

@@ -145,7 +139,7 @@ private void updateSort(SortContext sortContext) {
145139
public void sort(SortDirection direction, ColumnConfig<T> column) {
146140
SortContext sortContext = sortContainers.get(column.getSortKey());
147141
sortContext.sortDirection = direction;
148-
updateSort(sortContext);
142+
updateSort(sortContext, direction);
149143
fireSortEvent(direction, column);
150144
}
151145

@@ -214,7 +208,7 @@ public SortPlugin<T> configure(Consumer<SortPluginConfig> handler) {
214208
private static class SortContext {
215209
private final String columnName;
216210
private SortPluginConfig config;
217-
private SortDirection sortDirection = SortDirection.DESC;
211+
private SortDirection sortDirection = SortDirection.NONE;
218212
private DominoElement<HTMLElement> sortElement;
219213
private StateIcon sortIcon;
220214

@@ -243,7 +237,9 @@ public void clear() {
243237
* Updates the sorting state of the column, optionally flipping the sort direction.
244238
*
245239
* @param flip Whether to flip the sort direction.
240+
* @deprecated use
246241
*/
242+
@Deprecated
247243
public void update(boolean flip) {
248244
if (flip) {
249245
if (sortDirection.NONE.equals(sortDirection)) {
@@ -260,5 +256,15 @@ public void update(boolean flip) {
260256
}
261257
sortIcon.setState(sortDirection.name());
262258
}
259+
260+
/**
261+
* Updates the sorting state of the column, optionally flipping the sort direction.
262+
*
263+
* @param flip Whether to flip the sort direction.
264+
*/
265+
void update(SortDirection sortDirection) {
266+
this.sortDirection = sortDirection;
267+
sortIcon.setState(sortDirection.name());
268+
}
263269
}
264270
}

0 commit comments

Comments
 (0)