Ulf
07/06/2023, 11:15 AMDateRangePicker
for picking a date range that should be limited to X days. So whenever you select a new start date, the end date needs to be within startDate..startDate+X days. So we have something like:
val range = remember(dateRangeState.selectedStartDateMillis) { dateRangeState.selectedStartDateMillis .. dateRangeState.selectedStartDateMillis + maxAllowedDaysInMs }
and then using it like:
DateRangePicker(
...
state = dateRangeState,
dateValidator = { range.contains(it) },
However, the component only validates dates once so it won't update the enabled days even though the validator has changed.
If we look inside the DatePicker.kt
we can see why in the code for Month:
Day(
...
enabled = remember(dateInMillis) {
dateValidator.invoke(dateInMillis)
}
...
Now to the question; should this be considered a bug? Wouldn't this be a valid enough use-case for the component to be able to support it?Christopher Mederos
07/07/2023, 5:52 AM2023.06.01
BOM?
In any case, you would need to add a way to update the value of your range
val when a date selection is made. Although your val is initialized with a value from dateRangeState and is remembered itself - it isn't setup to observe changes from dateRangeStateUlf
07/07/2023, 1:07 PM2023.05.01
, so material3:1.1.0
.
(2023.06.01
has 1.1.1
).
my pseudo code above is skipping a lot of details, but our updating of range
does work fine - whenever selectedStartDateMillis
changes range will be recalculated.
We actually found a workaround to make it kind of work but it's not pretty, by forcing a recomposition when range changes:
key(range) {
DateRangePicker(
...
dateValidator = { range.contains(it) }
...
}