We are trying to use material3 `DateRangePicker` f...
# compose-android
u
We are trying to use material3
DateRangePicker
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:
Copy code
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:
Copy code
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?
c
Curious what version you're on, I don't see a dateValidator param on the
2023.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 dateRangeState
u
Currently we're on
2023.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:
Copy code
key(range) {
    DateRangePicker(
        ...
        dateValidator = { range.contains(it) }
   ...
}