|
| 1 | +/* |
| 2 | + * Copyright 2000-2021 Vaadin Ltd. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | +package com.vaadin.flow.component.datepicker; |
| 19 | + |
| 20 | +import com.vaadin.flow.component.html.NativeButton; |
| 21 | +import com.vaadin.flow.component.html.Span; |
| 22 | +import com.vaadin.flow.component.orderedlayout.VerticalLayout; |
| 23 | +import com.vaadin.flow.router.Route; |
| 24 | +import com.vaadin.flow.component.datepicker.DatePicker.DatePickerI18n; |
| 25 | + |
| 26 | +import java.time.LocalDate; |
| 27 | +import java.time.Month; |
| 28 | +import java.time.format.DateTimeFormatter; |
| 29 | +import java.util.Locale; |
| 30 | + |
| 31 | +@Route("vaadin-date-picker/date-picker-format") |
| 32 | +public class DatePickerFormatPage extends VerticalLayout { |
| 33 | + public static final String PRIMARY_FORMAT_DATE_PICKER = "PRIMARY_FORMAT_DATE_PICKER"; |
| 34 | + public static final String PRIMARY_FORMAT_OUTPUT = "PRIMARY_FORMAT_OUTPUT"; |
| 35 | + public static final String MULTIPLE_FORMAT_DATE_PICKER = "MULTIPLE_FORMAT_DATE_PICKER"; |
| 36 | + public static final String MULTIPLE_FORMAT_OUTPUT = "MULTIPLE_FORMAT_OUTPUT"; |
| 37 | + public static final String CHANGE_BETWEEN_FORMATS_DATE_PICKER = "CHANGE_BETWEEN_FORMATS_DATE_PICKER"; |
| 38 | + public static final String CHANGE_BETWEEN_FORMATS_BUTTON = "CHANGE_BETWEEN_FORMATS_BUTTON"; |
| 39 | + public static final String CHANGE_BETWEEN_FORMATS_OUTPUT = "CHANGE_BETWEEN_FORMATS_OUTPUT"; |
| 40 | + public static final String REMOVE_DATE_FORMAT_DATE_PICKER = "REMOVE_DATE_FORMAT_DATE_PICKER"; |
| 41 | + public static final String REMOVE_DATE_FORMAT_BUTTON = "REMOVE_DATE_FORMAT_BUTTON"; |
| 42 | + public static final String REMOVE_DATE_FORMAT_OUTPUT = "REMOVE_DATE_FORMAT_OUTPUT"; |
| 43 | + public static final String SET_LOCALE_AFTER_FORMAT_DATE_PICKER = "SET_LOCALE_AFTER_FORMAT_DATE_PICKER"; |
| 44 | + public static final String SET_LOCALE_AFTER_FORMAT_OUTPUT = "SET_LOCALE_AFTER_FORMAT_OUTPUT"; |
| 45 | + public static final String SET_DATE_FORMAT_AFTER_LOCALE_DATE_PICKER = "SET_DATE_FORMAT_AFTER_LOCALE_DATE_PICKER"; |
| 46 | + public static final String SET_DATE_FORMAT_AFTER_LOCALE_OUTPUT = "SET_DATE_FORMAT_AFTER_LOCALE_OUTPUT"; |
| 47 | + public static final String SERVER_SIDE_VALUE_CHANGE_DATE_PICKER = "SERVER_SIDE_VALUE_CHANGE_DATE_PICKER"; |
| 48 | + public static final String SERVER_SIDE_VALUE_CHANGE_BUTTON = "SERVER_SIDE_VALUE_CHANGE_BUTTON"; |
| 49 | + |
| 50 | + public static final LocalDate may13 = LocalDate.of(2018, Month.MAY, 13); |
| 51 | + |
| 52 | + public DatePickerFormatPage() { |
| 53 | + setupPrimaryFormat(); |
| 54 | + setupMultipleFormats(); |
| 55 | + setupChangeBetweenFormats(); |
| 56 | + setupRemoveDateFormat(); |
| 57 | + setupSetLocaleAfterFormat(); |
| 58 | + setupSetFormatAfterLocale(); |
| 59 | + setupServerSideValueChange(); |
| 60 | + } |
| 61 | + |
| 62 | + public void setupPrimaryFormat() { |
| 63 | + DatePicker datePicker = new DatePicker(may13); |
| 64 | + DatePickerI18n i18n = new DatePickerI18n(); |
| 65 | + i18n.setDateFormat("yyyy-MM-dd"); |
| 66 | + datePicker.setI18n(i18n); |
| 67 | + |
| 68 | + Span output = createOutputSpan(datePicker); |
| 69 | + datePicker.setId(PRIMARY_FORMAT_DATE_PICKER); |
| 70 | + output.setId(PRIMARY_FORMAT_OUTPUT); |
| 71 | + add(datePicker, output); |
| 72 | + } |
| 73 | + |
| 74 | + private void setupMultipleFormats() { |
| 75 | + DatePicker datePicker = new DatePicker(may13); |
| 76 | + DatePickerI18n i18n = new DatePickerI18n(); |
| 77 | + i18n.setDateFormats("yyyy-MM-dd", "dd.MM.yyyy", "MM/dd/yyyy"); |
| 78 | + datePicker.setI18n(i18n); |
| 79 | + |
| 80 | + Span output = createOutputSpan(datePicker); |
| 81 | + datePicker.setId(MULTIPLE_FORMAT_DATE_PICKER); |
| 82 | + output.setId(MULTIPLE_FORMAT_OUTPUT); |
| 83 | + add(datePicker, output); |
| 84 | + } |
| 85 | + |
| 86 | + private void setupChangeBetweenFormats() { |
| 87 | + DatePicker datePicker = new DatePicker(may13); |
| 88 | + |
| 89 | + DatePickerI18n i18n = new DatePickerI18n(); |
| 90 | + i18n.setDateFormat("dd.yyyy.MM"); |
| 91 | + datePicker.setI18n(i18n); |
| 92 | + |
| 93 | + NativeButton btn = new NativeButton("change format", clickEvent -> { |
| 94 | + datePicker.setI18n(new DatePickerI18n().setDateFormat("M/d/yy")); |
| 95 | + }); |
| 96 | + |
| 97 | + Span output = createOutputSpan(datePicker); |
| 98 | + btn.setId(CHANGE_BETWEEN_FORMATS_BUTTON); |
| 99 | + datePicker.setId(CHANGE_BETWEEN_FORMATS_DATE_PICKER); |
| 100 | + output.setId(CHANGE_BETWEEN_FORMATS_OUTPUT); |
| 101 | + add(datePicker, btn, output); |
| 102 | + } |
| 103 | + |
| 104 | + private void setupRemoveDateFormat() { |
| 105 | + DatePicker datePicker = new DatePicker(may13); |
| 106 | + datePicker.setLocale(Locale.GERMANY); |
| 107 | + |
| 108 | + DatePickerI18n i18n = new DatePickerI18n(); |
| 109 | + i18n.setDateFormat("dd yyyy MM"); |
| 110 | + datePicker.setI18n(i18n); |
| 111 | + |
| 112 | + NativeButton btn = new NativeButton("set format to null", |
| 113 | + clickEvent -> { |
| 114 | + datePicker |
| 115 | + .setI18n(new DatePickerI18n().setDateFormat(null)); |
| 116 | + }); |
| 117 | + |
| 118 | + Span output = createOutputSpan(datePicker); |
| 119 | + btn.setId(REMOVE_DATE_FORMAT_BUTTON); |
| 120 | + datePicker.setId(REMOVE_DATE_FORMAT_DATE_PICKER); |
| 121 | + output.setId(REMOVE_DATE_FORMAT_OUTPUT); |
| 122 | + add(datePicker, btn, output); |
| 123 | + } |
| 124 | + |
| 125 | + private void setupSetLocaleAfterFormat() { |
| 126 | + DatePicker datePicker = new DatePicker(may13); |
| 127 | + DatePickerI18n i18n = new DatePickerI18n(); |
| 128 | + i18n.setDateFormat("yyyy/MM/dd"); |
| 129 | + datePicker.setI18n(i18n); |
| 130 | + |
| 131 | + datePicker.setLocale(Locale.GERMANY); // should have no effect |
| 132 | + |
| 133 | + Span output = createOutputSpan(datePicker); |
| 134 | + datePicker.setId(SET_LOCALE_AFTER_FORMAT_DATE_PICKER); |
| 135 | + output.setId(SET_LOCALE_AFTER_FORMAT_OUTPUT); |
| 136 | + add(datePicker, output); |
| 137 | + } |
| 138 | + |
| 139 | + private void setupSetFormatAfterLocale() { |
| 140 | + DatePicker datePicker = new DatePicker(may13); |
| 141 | + DatePickerI18n i18n = new DatePickerI18n(); |
| 142 | + datePicker.setLocale(Locale.GERMANY); |
| 143 | + |
| 144 | + i18n.setDateFormat("yyyy/MM/dd"); |
| 145 | + datePicker.setI18n(i18n); |
| 146 | + |
| 147 | + Span output = createOutputSpan(datePicker); |
| 148 | + datePicker.setId(SET_DATE_FORMAT_AFTER_LOCALE_DATE_PICKER); |
| 149 | + output.setId(SET_DATE_FORMAT_AFTER_LOCALE_OUTPUT); |
| 150 | + add(datePicker, output); |
| 151 | + } |
| 152 | + |
| 153 | + private void setupServerSideValueChange() { |
| 154 | + DatePicker datePicker = new DatePicker(); |
| 155 | + DatePickerI18n i18n = new DatePickerI18n(); |
| 156 | + i18n.setDateFormat("d.M.yyyy"); |
| 157 | + datePicker.setI18n(i18n); |
| 158 | + |
| 159 | + NativeButton btn = new NativeButton("change date", clickEvent -> { |
| 160 | + datePicker.setValue(may13); |
| 161 | + }); |
| 162 | + |
| 163 | + btn.setId(SERVER_SIDE_VALUE_CHANGE_BUTTON); |
| 164 | + datePicker.setId(SERVER_SIDE_VALUE_CHANGE_DATE_PICKER); |
| 165 | + add(datePicker, btn); |
| 166 | + } |
| 167 | + |
| 168 | + private static Span createOutputSpan(DatePicker datePicker) { |
| 169 | + Span output = new Span(); |
| 170 | + datePicker.addValueChangeListener(event -> { |
| 171 | + LocalDate newValue = datePicker.getValue(); |
| 172 | + if (newValue != null) { |
| 173 | + output.setText(newValue.format(DateTimeFormatter.ISO_DATE)); |
| 174 | + } else { |
| 175 | + output.setText(""); |
| 176 | + } |
| 177 | + }); |
| 178 | + return output; |
| 179 | + } |
| 180 | +} |
0 commit comments