Skip to content

Commit 660b83a

Browse files
javier-godoypaodb
authored andcommitted
feat: implement HasValidation
Close #196
1 parent b07121c commit 660b83a

File tree

5 files changed

+53
-10
lines changed

5 files changed

+53
-10
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>com.flowingcode.vaadin.addons</groupId>
55
<artifactId>twincolgrid</artifactId>
6-
<version>3.0.1-SNAPSHOT</version>
6+
<version>3.1.0-SNAPSHOT</version>
77
<name>TwinColGrid add-on</name>
88
<description>Dual list component based on Vaadin Grids</description>
99
<url>https://www.flowingcode.com/en/open-source/</url>

src/main/java/com/flowingcode/vaadin/addons/twincolgrid/TwinColGrid.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* #%L
33
* TwinColGrid add-on
44
* %%
5-
* Copyright (C) 2017 - 2022 Flowing Code
5+
* Copyright (C) 2017 - 2025 Flowing Code
66
* %%
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
@@ -26,6 +26,7 @@
2626
import com.vaadin.flow.component.ComponentUtil;
2727
import com.vaadin.flow.component.HasComponents;
2828
import com.vaadin.flow.component.HasSize;
29+
import com.vaadin.flow.component.HasValidation;
2930
import com.vaadin.flow.component.HasValue;
3031
import com.vaadin.flow.component.HasValue.ValueChangeEvent;
3132
import com.vaadin.flow.component.HasValueAndElement;
@@ -79,7 +80,7 @@
7980
@CssImport(value = "./styles/twin-col-grid-button.css")
8081
@CssImport(value = "./styles/twincol-grid.css")
8182
public class TwinColGrid<T> extends VerticalLayout
82-
implements HasValueAndElement<ValueChangeEvent<Set<T>>, Set<T>>, HasComponents, HasSize {
83+
implements HasValueAndElement<ValueChangeEvent<Set<T>>, Set<T>>, HasComponents, HasSize, HasValidation {
8384

8485
private static final class TwinColModel<T> implements Serializable {
8586
final Grid<T> grid;
@@ -142,6 +143,8 @@ public enum Orientation {
142143

143144
private Span fakeButtonContainerLabel = new Span();
144145

146+
private Span errorMessageSpan = new Span();
147+
145148
private Orientation orientation = Orientation.HORIZONTAL;
146149

147150
private boolean autoResize = false;
@@ -184,7 +187,10 @@ public TwinColGrid(@NonNull Grid<T> availableGrid, @NonNull Grid<T> selectionGri
184187

185188
available = new TwinColModel<>(availableGrid, "twincol-grid-available");
186189
selection = new TwinColModel<>(selectionGrid, "twincol-grid-selection");
187-
190+
191+
errorMessageSpan.setClassName("twincol-grid-error-message");
192+
selection.layout.addComponentAtIndex(1, errorMessageSpan);
193+
188194
setClassName("twincol-grid");
189195

190196
setMargin(false);
@@ -677,6 +683,26 @@ public boolean isRequiredIndicatorVisible() {
677683
return getElement().getAttribute("required") != null;
678684
}
679685

686+
@Override
687+
public void setErrorMessage(String errorMessage) {
688+
errorMessageSpan.setText(errorMessage);
689+
}
690+
691+
@Override
692+
public String getErrorMessage() {
693+
return errorMessageSpan.getText();
694+
}
695+
696+
@Override
697+
public void setInvalid(boolean invalid) {
698+
getElement().setAttribute("invalid", invalid);
699+
}
700+
701+
@Override
702+
public boolean isInvalid() {
703+
return getElement().getAttribute("invalid") != null;
704+
}
705+
680706
@Override
681707
public void setReadOnly(final boolean readOnly) {
682708
getAvailableGrid().setSelectionMode(readOnly ? SelectionMode.NONE : SelectionMode.MULTI);
@@ -956,4 +982,5 @@ private void updateOrientationOnResize(int width, int height) {
956982
this.withOrientation(Orientation.HORIZONTAL);
957983
}
958984
}
985+
959986
}

src/main/java/com/flowingcode/vaadin/addons/twincolgrid/TwinColGridListAdapter.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* #%L
33
* TwinColGrid add-on
44
* %%
5-
* Copyright (C) 2017 - 2022 Flowing Code
5+
* Copyright (C) 2017 - 2025 Flowing Code
66
* %%
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
*/
2020
package com.flowingcode.vaadin.addons.twincolgrid;
2121

22+
import com.vaadin.flow.component.HasValidation;
2223
import com.vaadin.flow.component.HasValue;
2324
import com.vaadin.flow.component.HasValue.ValueChangeEvent;
2425
import com.vaadin.flow.shared.Registration;
@@ -40,9 +41,9 @@
4041
*/
4142
@SuppressWarnings("serial")
4243
@RequiredArgsConstructor
43-
class TwinColGridListAdapter<T> implements HasValue<ValueChangeEvent<List<T>>, List<T>> {
44+
class TwinColGridListAdapter<T> implements HasValue<ValueChangeEvent<List<T>>, List<T>>, HasValidation {
4445

45-
private interface IDelegate {
46+
private interface IDelegate extends HasValidation {
4647
boolean isEmpty();
4748

4849
void clear();

src/main/resources/META-INF/resources/frontend/styles/twincol-grid.css

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* #%L
33
* TwinColGrid add-on
44
* %%
5-
* Copyright (C) 2017 - 2022 Flowing Code
5+
* Copyright (C) 2017 - 2025 Flowing Code
66
* %%
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
@@ -44,6 +44,19 @@
4444
color: var(--lumo-error-text-color);
4545
}
4646

47+
.twincol-grid .twincol-grid-selection .twincol-grid-error-message {
48+
display: none;
49+
}
50+
51+
.twincol-grid[invalid] .twincol-grid-selection .twincol-grid-error-message {
52+
font-size: var(--vaadin-input-field-error-font-size, var(--lumo-font-size-xs));
53+
line-height: var(--lumo-line-height-xs);
54+
font-weight: var(--vaadin-input-field-error-font-weight, 400);
55+
color: var(--vaadin-input-field-error-color, var(--lumo-error-text-color));
56+
display: block;
57+
padding-bottom: 4px;
58+
}
59+
4760
.twincol-grid .fake-button-container-label {
4861
font-size: var(--lumo-font-size-s);
4962
margin-bottom: calc(var(--vaadin-button-margin, var(--lumo-space-xs))* -1);

src/test/java/com/flowingcode/vaadin/addons/twincolgrid/BoundDemo.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* #%L
33
* TwinColGrid add-on
44
* %%
5-
* Copyright (C) 2017 - 2022 Flowing Code
5+
* Copyright (C) 2017 - 2025 Flowing Code
66
* %%
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
@@ -58,7 +58,9 @@ public BoundDemo() {
5858
twinColGrid.setCaption("TwinColGrid demo with Binder and row select without checkbox");
5959

6060
final Binder<Library> binder = new Binder<>();
61-
binder.forField(twinColGrid.asList()).asRequired().bind(Library::getBooks, Library::setBooks);
61+
binder.forField(twinColGrid.asList())
62+
.asRequired("You must add at least one book.")
63+
.bind(Library::getBooks, Library::setBooks);
6264
binder.setBean(library);
6365

6466
add(twinColGrid);

0 commit comments

Comments
 (0)