Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: DatePicker fallback parser #3948

Merged
merged 6 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions articles/components/date-picker/date-formats.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,20 @@ Other characters, such as separators (e.g., `/`, `.`, `-`) or spaces may be used
Custom date patterns take precedence over the configured locale. When using both at the same time, the custom date pattern is used to display and parse dates.
endif::[]

== Server-Side Parsing in Flow

Additionally, Date Picker allows you to provide a server-side parser that is used as a fallback when user input cannot be parsed using methods described in the previous sections. The parser is a function that receives the user-entered string and returns either a `Result<LocalDate>` object or an error message. The parsed date is then displayed like any other dates, using the date format pattern that was configured in the previous sections.

The server-side parser can be used, for example, to support shortcuts for quickly entering dates, such as `+1` or `tomorrow`. In the following example, try entering `+1`, `-2`, and `foobar` to see the server-side parser in action:

[.example]
--
[source,java]
----
include::{root}/src/main/java/com/vaadin/demo/component/datepicker/DatePickerFallbackParser.java[render,tags=snippet,indent=0]
----
--


== Using Custom Functions in React & Lit

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.vaadin.demo.component.datepicker;

import com.vaadin.demo.DemoExporter; // hidden-source-line
import com.vaadin.flow.component.datepicker.DatePicker;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.data.binder.Result;
import com.vaadin.flow.router.Route;

import java.time.LocalDate;

@Route("date-picker-fallback-parser")
public class DatePickerFallbackParser extends VerticalLayout {
public DatePickerFallbackParser() {
DatePicker datePicker = new DatePicker("Select a date:");
// tag::snippet[]
datePicker.setFallbackParser((s) -> {
LocalDate now = LocalDate.now();
if (s.matches("\\+\\d+")) {
return Result
.ok(now.plusDays(Integer.parseInt(s.substring(1))));
} else if (s.matches("-\\d+")) {
return Result
.ok(now.minusDays(Integer.parseInt(s.substring(1))));
} else {
return Result.error("Invalid date format");
}
});
// end::snippet[]
datePicker.setHelperText("Format: +1, -2 for relative days");
add(datePicker);
}

public static class Exporter
extends DemoExporter<DatePickerFallbackParser> { // hidden-source-line
} // hidden-source-line
}
Loading