Skip to content

Commit 6298d5d

Browse files
elPeirettijavier-godoy
authored andcommitted
feat: add option to disable/enable flag rendering
1 parent 9089c58 commit 6298d5d

File tree

1 file changed

+48
-3
lines changed

1 file changed

+48
-3
lines changed

src/main/java/com/flowingcode/vaadin/addons/localecombobox/LocaleComboBox.java

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,12 @@ public enum DisplayMode {
8282
private Locale customDisplayLocale = Locale.getDefault();
8383

8484
/**
85-
* Creates a new instance of {@code LocaleComboBox}.
85+
* Indicates whether the flags should be displayed alongside the locale names.
86+
*/
87+
private Boolean hasFlags = true;
88+
89+
/**
90+
* * Creates a new instance of {@code LocaleComboBox}.
8691
*/
8792
public LocaleComboBox() {
8893
setItemLabelGenerator(item -> item.getDisplayName(getLocaleForDisplay()));
@@ -125,6 +130,29 @@ public void setDisplayLocale(Locale displayLocale) {
125130
this.customDisplayLocale = displayLocale == null ? Locale.getDefault() : displayLocale;
126131
}
127132

133+
/**
134+
* Returns the current flag display status.
135+
*
136+
* @return {@code true} if flags are displayed alongside the locale names, {@code false} otherwise
137+
*/
138+
public Boolean hasFlags() {
139+
return hasFlags;
140+
}
141+
142+
/**
143+
* Sets whether flags should be displayed alongside locale names.
144+
* <p>
145+
* This method updates the internal state to reflect whether flags should be displayed and updates
146+
* the rendering based on the new state.
147+
*
148+
* @param hasFlags A {@code Boolean} indicating whether flags should be displayed or not.
149+
*/
150+
public void setHasFlags(Boolean hasFlags) {
151+
this.hasFlags = hasFlags;
152+
this.setRenderer(this.hasFlags ? getLocaleRenderer() : getLocaleRendererWithoutFlags());
153+
this.setPrefixFlag(this.hasFlags ? this.getValue() : null);
154+
}
155+
128156
private LitRenderer<Locale> getLocaleRenderer() {
129157
return LitRenderer
130158
.<Locale>of(
@@ -140,6 +168,14 @@ private LitRenderer<Locale> getLocaleRenderer() {
140168
.withProperty("displayName", loc -> loc.getDisplayName(getLocaleForDisplay()));
141169
}
142170

171+
private LitRenderer<Locale> getLocaleRendererWithoutFlags() {
172+
return LitRenderer.<Locale>of("""
173+
<vaadin-horizontal-layout class="${item.layoutClass}">
174+
<span>${item.displayName}</span>
175+
</vaadin-horizontal-layout>""").withProperty("layoutClass", loc -> ITEM_LAYOUT_CLASS_NAME)
176+
.withProperty("displayName", loc -> loc.getDisplayName());
177+
}
178+
143179
private Locale getLocaleForDisplay() {
144180

145181
switch (displayMode) {
@@ -162,13 +198,22 @@ private String getFlagCode(Locale locale) {
162198
}
163199

164200
private void onValueChange(ComponentValueChangeEvent<ComboBox<Locale>, Locale> event) {
201+
202+
if (!this.hasFlags)
203+
return;
204+
165205
Locale newValue = event.getValue();
166-
if (newValue == null) {
206+
this.setPrefixFlag(newValue);
207+
}
208+
209+
private void setPrefixFlag(Locale locale) {
210+
if (locale == null) {
167211
setPrefixComponent(null);
168212
return;
169213
}
214+
170215
Span flagIcon = new Span();
171-
flagIcon.addClassNames("fi", "fi-" + newValue.getCountry().toLowerCase());
216+
flagIcon.addClassNames("fi", "fi-" + locale.getCountry().toLowerCase());
172217
setPrefixComponent(flagIcon);
173218
}
174219

0 commit comments

Comments
 (0)