Skip to content

Commit

Permalink
feat!: add constructor for initializing with a locale collection
Browse files Browse the repository at this point in the history
BREAKING CHANGE: removes item population from the empty constructor
  • Loading branch information
elPeiretti authored and javier-godoy committed Aug 22, 2024
1 parent fbf04d9 commit 38dab16
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.data.renderer.LitRenderer;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Optional;

Expand Down Expand Up @@ -80,15 +82,22 @@ public enum DisplayMode {
private Locale customDisplayLocale = Locale.getDefault();

/**
* Creates a new instance of LocaleComboBox with all the installed locales.
* Creates a new instance of LocaleComboBox.
*/
public LocaleComboBox() {
setItemLabelGenerator(item -> item.getDisplayName(getLocaleForDisplay()));
setRenderer(getLocaleRenderer());
addValueChangeListener(this::onValueChange);
setItems(
Arrays.stream(Locale.getAvailableLocales()).filter(loc -> loc.getCountry().length() == 2)
.sorted((l1, l2) -> l1.getDisplayName().compareTo(l2.getDisplayName())).toList());
}

/**
* Creates a new instance of LocaleComboBox with the desired locales
*
* @param locales the {@link Collection} of locales to include in the combobox
*/
public LocaleComboBox(Collection<Locale> items) {
this();
setItems(items);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;

@DemoSource
Expand All @@ -39,21 +41,27 @@ public class LocaleComboBoxDemo extends Div {

public LocaleComboBoxDemo() {

LocaleComboBox defaultDisplayLocale = new LocaleComboBox();
List<Locale> localeList =
Arrays.stream(Locale.getAvailableLocales()).filter(loc -> !loc.getDisplayName().isBlank())
.sorted((l1, l2) -> l1.getDisplayName().compareTo(l2.getDisplayName())).toList();

LocaleComboBox defaultDisplayLocale = new LocaleComboBox(localeList);
LocaleComboBox koreanLocaleCombo = new LocaleComboBox();
LocaleComboBox selectedLocaleCombo = new LocaleComboBox();
LocaleComboBox selectedLocaleCombo = new LocaleComboBox(localeList);

defaultDisplayLocale.setValue(Locale.ITALY);


koreanLocaleCombo.setItems(localeList);
koreanLocaleCombo.setDisplayLocale(Locale.KOREA);
koreanLocaleCombo.setDisplayMode(LocaleComboBox.DisplayMode.CUSTOM);
koreanLocaleCombo.setValue(Locale.ITALY);

selectedLocaleCombo.setDisplayMode(LocaleComboBox.DisplayMode.SELECTED);
selectedLocaleCombo.setValue(Locale.ITALY);

// #if vaadin eq 0
add(createHorizontalContainer("Default display mode (uses default locale):", defaultDisplayLocale),
add(createHorizontalContainer("Default display mode (uses default locale):",
defaultDisplayLocale),
createHorizontalContainer("Display locales with Korean locale:", koreanLocaleCombo),
createHorizontalContainer("Display locales with selected locale:", selectedLocaleCombo));
// #endif
Expand Down

0 comments on commit 38dab16

Please sign in to comment.