[英]How can I add a dynamic date time picker to my app?
I'm trying to create a web app that supports online reservation/booking system.我正在尝试创建一个支持在线预订/预订系统的 web 应用程序。 I want to create a dynamic date picker that allows the user to pick some specific dates, based on the availability.
我想创建一个动态日期选择器,允许用户根据可用性选择一些特定日期。
Let's say the data for available dates is stored in an array slots
.假设可用日期的数据存储在数组
slots
中。 How can modify the date picker to only allow the user to choose from set of these dates, while disabling the rest.如何修改日期选择器以仅允许用户从这些日期集中进行选择,同时禁用 rest。
I'm use DatePicker from @material-ui/pickers
and I was able to find disablePast
which disables all the previous dates but couldn't find any prop, that fits my use case.我使用来自
@material-ui/pickers
pickers 的 DatePicker,我能够找到disablePast
禁用所有以前的日期但找不到任何适合我的用例的道具。
Can someone help me out?有人可以帮我吗?
What you can do for your this is, you can use the shouldDisableDate
prop in which you have to pass a function that accepts date
as an argument and return a boolean true/false
whether the date should be rendered or not.您可以为此做的是,您可以使用
shouldDisableDate
道具,您必须在其中传递接受date
作为参数的 function 并返回 boolean true/false
,无论是否应呈现日期。
For example:例如:
In your component's shouldDisableDate
prop you can pass a function like this在组件的
shouldDisableDate
道具中,您可以像这样传递 function
<DatePicker
shouldDisableDate={renderDateFunction}
...
and then define the function as然后将 function 定义为
const renderDateFunction = (renderDate) => {
// If the current date passed to this function is included in the slots
// then enable selection of that date
if(slots?.includes(renderDate)) return true;
// else prevent the user from selecting that date
return false;
};
You can read more about the DatePicker Component API from the documentation here: https://material-ui-pickers.dev/api/DatePicker您可以从此处的文档中阅读有关 DatePicker 组件 API 的更多信息: https://material-ui-pickers.dev/api/DatePicker
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.