Skip to content

Commit

Permalink
feat: expose i18n object by adding setter and getter (#6330)
Browse files Browse the repository at this point in the history
Co-authored-by: Sascha Ißbrücker <sissbruecker@vaadin.com>
  • Loading branch information
vursen and sissbruecker committed Jul 11, 2024
1 parent 25e875e commit cb88298
Show file tree
Hide file tree
Showing 2 changed files with 264 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;
import elemental.json.JsonObject;

import com.vaadin.flow.component.AbstractField;
import com.vaadin.flow.component.AbstractSinglePropertyField;
Expand Down Expand Up @@ -130,6 +131,9 @@ public class DateTimePicker
private DateTimePickerI18n i18n;
private Locale locale;

private String dateAriaLabel;
private String timeAriaLabel;

private final static SerializableFunction<String, LocalDateTime> PARSER = s -> {
return s == null || s.isEmpty() ? null : LocalDateTime.parse(s);
};
Expand Down Expand Up @@ -415,97 +419,67 @@ public Optional<String> getAriaLabel() {
}

/**
* Contains DateTimePicker internalization properties
*/
public static class DateTimePickerI18n implements Serializable {
private String dateLabel;
private String timeLabel;

public String getDateLabel() {
return dateLabel;
}

public DateTimePickerI18n setDateLabel(String dateLabel) {
this.dateLabel = dateLabel;
return this;
}

public String getTimeLabel() {
return timeLabel;
}

public DateTimePickerI18n setTimeLabel(String timeLabel) {
this.timeLabel = timeLabel;
return this;
}
}

/**
* Sets the accessible label for the date picker.
* Sets the aria-label suffix for the date picker.
* <p>
* The final value is a concatenation of the accessible label from
* DateTimePicker's {@link #getAriaLabel()} or {@link #getLabel()} and the
* given accessible label.
* The suffix set with this method takes precedence over the suffix set with
* {@link DateTimePickerI18n#setDateLabel(String)}.
* <p>
* The date picker's final aria-label is a concatenation of DateTimePicker's
* {@link #getAriaLabel()} or {@link #getLabel()} and this suffix.
*
* @param dateLabel
* the value to be used as part of date picker aria-label.
* the value to be used as a suffix in the date picker
* aria-label.
*/
public void setDateAriaLabel(String dateLabel) {
if (i18n == null) {
i18n = new DateTimePickerI18n();
}
i18n.setDateLabel(dateLabel);
getElement().setPropertyJson("i18n", JsonSerializer.toJson(i18n));
dateAriaLabel = dateLabel;
updateI18n();
}

/**
* Gets the accessible label of the date picker.
* Gets the aria-label suffix for the date picker.
* <p>
* Note that this method will return the last value passed to
* Note: this method will return the last value passed to
* {@link #setDateAriaLabel(String)}, not the value currently set on the
* `aria-label` attribute of the date picker input element.
*
* @return an optional label or an empty optional if no label has been set
* with this method before.
*/
public Optional<String> getDateAriaLabel() {
if (i18n == null) {
return Optional.empty();
}
return Optional.ofNullable(i18n.getDateLabel());
return Optional.ofNullable(dateAriaLabel);
}

/**
* Sets the accessible label for the time picker.
* Sets the aria-label suffix for the time picker.
* <p>
* The final value is a concatenation of the accessible label from
* DateTimePicker's {@link #getAriaLabel()} or {@link #getLabel()} and the
* given accessible label.
* The suffix set with this method takes precedence over the suffix set with
* {@link DateTimePickerI18n#setTimeLabel(String)}.
* <p>
* The time picker's final aria-label is a concatenation of DateTimePicker's
* {@link #getAriaLabel()} or {@link #getLabel()} and this suffix.
*
* @param timeLabel
* the value to be used as part of time picker aria-label.
* the value to be used as a suffix in the time picker
* aria-label.
*/
public void setTimeAriaLabel(String timeLabel) {
if (i18n == null) {
i18n = new DateTimePickerI18n();
}
i18n.setTimeLabel(timeLabel);
getElement().setPropertyJson("i18n", JsonSerializer.toJson(i18n));
timeAriaLabel = timeLabel;
updateI18n();
}

/**
* Gets the accessible label of the time picker.
* Gets the aria-label suffix for the time picker.
* <p>
* Note that this method will return the last value passed to
* Note: this method will return the last value passed to
* {@link #setTimeAriaLabel(String)}, not the value currently set on the
* `aria-label` attribute of the time picker input element.
*
* @return an optional label or an empty optional if no label has been set
* with this method before.
*/
public Optional<String> getTimeAriaLabel() {
if (i18n == null) {
return Optional.empty();
}
return Optional.ofNullable(i18n.getTimeLabel());
return Optional.ofNullable(timeAriaLabel);
}

/**
Expand Down Expand Up @@ -888,6 +862,49 @@ public void setDatePickerI18n(DatePickerI18n i18n) {
datePicker.setI18n(i18n);
}

/**
* Gets the internationalization object previously set for this component.
* <p>
* Note: updating the instance that is returned from this method will not
* update the component if not set back using
* {@link DateTimePicker#setI18n(DateTimePickerI18n)}
*
* @return the i18n object. It will be <code>null</code>, If the i18n
* properties weren't set.
*/
public DateTimePickerI18n getI18n() {
return i18n;
}

/**
* Sets the internationalization properties for this component.
*
* @param i18n
* the internationalized properties, not <code>null</code>
*/
public void setI18n(DateTimePickerI18n i18n) {
Objects.requireNonNull(i18n,
"The i18n properties object should not be null");
this.i18n = i18n;
updateI18n();
}

private void updateI18n() {
DateTimePickerI18n i18nObject = i18n != null ? i18n
: new DateTimePickerI18n();
JsonObject i18nJson = (JsonObject) JsonSerializer.toJson(i18nObject);

if (dateAriaLabel != null) {
i18nJson.put("dateLabel", dateAriaLabel);
}

if (timeAriaLabel != null) {
i18nJson.put("timeLabel", timeAriaLabel);
}

getElement().setPropertyJson("i18n", i18nJson);
}

/**
* When auto open is enabled, the dropdown will open when the field is
* clicked.
Expand All @@ -906,4 +923,72 @@ protected void onAttach(AttachEvent attachEvent) {
super.onAttach(attachEvent);
ClientValidationUtil.preventWebComponentFromModifyingInvalidState(this);
}

/**
* The internationalization properties for {@link DateTimePicker}.
*/
public static class DateTimePickerI18n implements Serializable {
private String dateLabel;
private String timeLabel;

/**
* Gets the aria-label suffix for the date picker.
* <p>
* The date picker's final aria-label is a concatanation of the
* DateTimePicker's {@link #getAriaLabel()} or {@link #getLabel()}
* methods and this suffix.
*
* @return the value used as a suffix in the date picker aria-label.
*/
public String getDateLabel() {
return dateLabel;
}

/**
* Sets the aria-label suffix for the date picker.
* <p>
* The date picker's final aria-label is a concatanation of the
* DateTimePicker's {@link #getAriaLabel()} or {@link #getLabel()}
* methods and this suffix.
*
* @param dateLabel
* the value to be used as a suffix in the date picker
* aria-label.
* @return this instance for method chaining
*/
public DateTimePickerI18n setDateLabel(String dateLabel) {
this.dateLabel = dateLabel;
return this;
}

/**
* Gets the aria-label suffix for the time picker.
* <p>
* The time picker's aria-label is a concatanation of the
* DateTimePicker's {@link #getAriaLabel()} or {@link #getLabel()}
* methods and this suffix.
*
* @return the value used as a suffix in the time picker aria-label.
*/
public String getTimeLabel() {
return timeLabel;
}

/**
* Sets the aria-label suffix for the time picker.
* <p>
* The time picker's aria-label is a concatanation of the
* DateTimePicker's {@link #getAriaLabel()} or {@link #getLabel()}
* methods and this suffix.
*
* @param timeLabel
* the value to be used as a suffix in the time picker
* aria-label.
* @return this instance for method chaining
*/
public DateTimePickerI18n setTimeLabel(String timeLabel) {
this.timeLabel = timeLabel;
return this;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Copyright 2000-2024 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.flow.component.datetimepicker;

import elemental.json.JsonObject;
import elemental.json.JsonNull;

import org.junit.Before;
import org.junit.Test;
import org.junit.Assert;

public class DateTimePickerI18nTest {

private DateTimePicker dateTimePicker;

@Before
public void setup() {
dateTimePicker = new DateTimePicker();
}

@Test
public void setDateAriaLabel_removeDateAriaLabel() {
dateTimePicker.setDateAriaLabel("Custom date");
Assert.assertEquals("Custom date",
getI18nPropertyAsJson(dateTimePicker).getString("dateLabel"));

dateTimePicker.setDateAriaLabel(null);
Assert.assertTrue(getI18nPropertyAsJson(dateTimePicker)
.get("dateLabel") instanceof JsonNull);
}

@Test
public void setDateAriaLabel_setI18n() {
dateTimePicker.setDateAriaLabel("Custom date");

dateTimePicker.setI18n(new DateTimePicker.DateTimePickerI18n()
.setDateLabel("I18n date"));
Assert.assertEquals("Custom date",
getI18nPropertyAsJson(dateTimePicker).getString("dateLabel"));
}

@Test
public void setTimeAriaLabel_removeTimeAriaLabel() {
dateTimePicker.setTimeAriaLabel("Custom time");
Assert.assertEquals("Custom time",
getI18nPropertyAsJson(dateTimePicker).getString("timeLabel"));

dateTimePicker.setTimeAriaLabel(null);
Assert.assertTrue(getI18nPropertyAsJson(dateTimePicker)
.get("timeLabel") instanceof JsonNull);
}

@Test
public void setTimeAriaLabel_setI18n() {
dateTimePicker.setTimeAriaLabel("Custom time");

dateTimePicker.setI18n(new DateTimePicker.DateTimePickerI18n()
.setTimeLabel("I18n time"));
Assert.assertEquals("Custom time",
getI18nPropertyAsJson(dateTimePicker).getString("timeLabel"));
}

@Test
public void setI18n() {
DateTimePicker.DateTimePickerI18n i18n = new DateTimePicker.DateTimePickerI18n()
.setDateLabel("I18n date").setTimeLabel("I18n time");
dateTimePicker.setI18n(i18n);

Assert.assertEquals("I18n date",
getI18nPropertyAsJson(dateTimePicker).getString("dateLabel"));
Assert.assertEquals("I18n time",
getI18nPropertyAsJson(dateTimePicker).getString("timeLabel"));
}

@Test
public void setI18n_setDateAriaLabel_removeDateAriaLabel() {
DateTimePicker.DateTimePickerI18n i18n = new DateTimePicker.DateTimePickerI18n()
.setDateLabel("I18n date").setTimeLabel("I18n time");
dateTimePicker.setI18n(i18n);

dateTimePicker.setDateAriaLabel("Custom date");
Assert.assertEquals("Custom date",
getI18nPropertyAsJson(dateTimePicker).getString("dateLabel"));

dateTimePicker.setDateAriaLabel(null);
Assert.assertEquals("I18n date",
getI18nPropertyAsJson(dateTimePicker).getString("dateLabel"));
}

@Test
public void setI18n_setTimeAriaLabel_removeTimeAriaLabel() {
DateTimePicker.DateTimePickerI18n i18n = new DateTimePicker.DateTimePickerI18n()
.setDateLabel("I18n date").setTimeLabel("I18n time");
dateTimePicker.setI18n(i18n);

dateTimePicker.setTimeAriaLabel("Custom time");
Assert.assertEquals("Custom time",
getI18nPropertyAsJson(dateTimePicker).getString("timeLabel"));

dateTimePicker.setTimeAriaLabel(null);
Assert.assertEquals("I18n time",
getI18nPropertyAsJson(dateTimePicker).getString("timeLabel"));
}

private JsonObject getI18nPropertyAsJson(DateTimePicker dateTimePicker) {
return (JsonObject) dateTimePicker.getElement().getPropertyRaw("i18n");
}
}

0 comments on commit cb88298

Please sign in to comment.