ant-design/cd51d303-async.f0a6a922.js
2024-11-20 18:30:54 +00:00

1 line
6.8 KiB
JavaScript

(("undefined"!=typeof globalThis?globalThis:self).makoChunk_antd=("undefined"!=typeof globalThis?globalThis:self).makoChunk_antd||[]).push([["cd51d303"],{cd51d303:function(e,a,t){"use strict";t.d(a,"__esModule",{value:!0}),t.d(a,"texts",{enumerable:!0,get:function(){return n;}}),t("b99e3ea4");let n=[{value:"In antd, there are many question about DatePicker. One is due to the commonality of date selection requirements, and the other is that there are various combinations of disabled selections for business requirements. Today, let's talk about the ",paraId:0},{value:"disabledDate",paraId:0},{value:" API.",paraId:0},{value:"As its name suggests, ",paraId:1,tocIndex:0},{value:"disabledDate",paraId:1,tocIndex:0},{value:" is used to disable dates. So when using DateTimePicker, the time part is not controlled by ",paraId:1,tocIndex:0},{value:"disabledDate",paraId:1,tocIndex:0},{value:". Instead, it needs to be controlled through the ",paraId:1,tocIndex:0},{value:"disabledTime",paraId:1,tocIndex:0},{value:" method. This seems a bit counterintuitive. Why do antd need two APIs to manage it?",paraId:1,tocIndex:0},{value:"In terms of intuition, we only need to execute ",paraId:2,tocIndex:1},{value:"disabledDate",paraId:2,tocIndex:1},{value:" once for a date to determine if it is disabled. However, if we switch the panel to the month panel, how do we know if the current month is selectable? We must execute ",paraId:2,tocIndex:1},{value:"disabledDate",paraId:2,tocIndex:1},{value:" for each date in that month to determine if there are selectable dates in that month, so that month can be selected.",paraId:2,tocIndex:1},{value:"This logic seems fine for now. A month has about 30 days, and there are 12 months in the month panel. In the worst case, we only need to iterate 365 times to know that 12 months are not selectable. But as the panel switches to years and decades, the number of iterations required will increase exponentially, leading to serious performance issues (",paraId:3,tocIndex:1},{value:"#39991",paraId:3,tocIndex:1},{value:")",paraId:3,tocIndex:1},{value:"So after the DatePicker refactoring, ",paraId:4,tocIndex:1},{value:"disabledDate",paraId:4,tocIndex:1},{value:" provides an additional parameter ",paraId:4,tocIndex:1},{value:"info.type",paraId:4,tocIndex:1},{value:", which tells you which Panel the provided date object comes from. This allows developers to provide ",paraId:4,tocIndex:1},{value:"disabled",paraId:4,tocIndex:1},{value:" information based on the Panel, thus avoiding the terrifying call loop.",paraId:4,tocIndex:1},{value:"But as a fallback, DatePicker will call ",paraId:5,tocIndex:1},{value:"disabledDate",paraId:5,tocIndex:1},{value:" for the first and last days of the current Panel unit. This ensures that common range disable scenarios are still met.",paraId:5,tocIndex:1},{value:"Back to time disable, this is the same problem. If we use ",paraId:6,tocIndex:2},{value:"disabledDate",paraId:6,tocIndex:2},{value:" to disable time dimensions, then whether a day is selectable in DateTimePicker, we need to check each second of that day with ",paraId:6,tocIndex:2},{value:"disabledDate",paraId:6,tocIndex:2},{value:". In the worst case, whether a day is selectable needs to be checked 86400 times. The Date Panel needs to execute ~2 million times. Obviously, this is unacceptable.",paraId:6,tocIndex:2},{value:"So for the time dimension, we provide the ",paraId:7,tocIndex:2},{value:"disabledTime",paraId:7,tocIndex:2},{value:" method. This method requires more granular time disable information than ",paraId:7,tocIndex:2},{value:"disabledDate",paraId:7,tocIndex:2},{value:":",paraId:7,tocIndex:2},{value:"type DisabledTime = (now: Dayjs) => {\n disabledHours?: () => number[];\n disabledMinutes?: (selectedHour: number) => number[];\n disabledSeconds?: (selectedHour: number, selectedMinute: number) => number[];\n disabledMilliseconds?: (\n selectedHour: number,\n selectedMinute: number,\n selectedSecond: number,\n ) => number[];\n};\n",paraId:8,tocIndex:2},{value:"(Each unit of the time selection panel is equivalent to the Panel of the date panel, and the latter infers the current disabled unit from the information of the former unit)",paraId:9,tocIndex:2},{value:"After understanding the context, we will find that ",paraId:10,tocIndex:3},{value:"disabledDate",paraId:10,tocIndex:3},{value:" and ",paraId:10,tocIndex:3},{value:"disabledTime",paraId:10,tocIndex:3},{value:" are designed to be reasonable, but they are somewhat low-level. It is more troublesome to use them in business. Let's look at a few examples (of course, you need to consider encapsulating them through HOC in business):",paraId:10,tocIndex:3},{value:"Temporarily ignore the situation of holidays and choose 9:00 ~ 17:00 on working days as the selectable time:",paraId:11,tocIndex:4},{value:"const disabledDate = (date, info) => {\n if (info.type === 'date') {\n return date.day() === 0 || date.day() === 6;\n }\n return false;\n};\n\nconst disabledTime = () => ({\n disabledHours: () => {\n return Array.from({ length: 24 }, (_, i) => i).filter((hour) => hour < 9 || hour > 17);\n },\n});\n",paraId:12,tocIndex:4},{value:"In DatePicker, there are ",paraId:13,tocIndex:5},{value:"minDate",paraId:13,tocIndex:5},{value:" and ",paraId:13,tocIndex:5},{value:"maxDate",paraId:13,tocIndex:5},{value:" to limit the date selection range. But they only limit to date range. Now, suppose we have a scenario that requires a date range selection with time, such as ",paraId:13,tocIndex:5},{value:"2024-01-01 09:00:00",paraId:13,tocIndex:5},{value:" ~ ",paraId:13,tocIndex:5},{value:"2024-01-02 17:00:00",paraId:13,tocIndex:5},{value:", then we can do this:",paraId:13,tocIndex:5},{value:"const disabledDate = (date, info) => {\n if (info.type === 'date') {\n return date.isBefore('2024-01-01', 'day') || date.isAfter('2024-01-02', 'day');\n }\n return !date.isSame('2024-01-01', info.type);\n};\n\nconst disabledTime = (date) => {\n if (date.isSame('2024-01-01', 'day')) {\n return {\n disabledHours: () => Array.from({ length: 24 }, (_, i) => i).filter((hour) => hour < 9),\n };\n }\n\n if (date.isSame('2024-01-02', 'day')) {\n return {\n disabledHours: () => Array.from({ length: 24 }, (_, i) => i).filter((hour) => hour > 17),\n };\n }\n\n // Only need to consider the start and end time\n // the range itself has been disabled by `disabledDate`\n return {};\n};\n",paraId:14,tocIndex:5},{value:"Through ",paraId:15,tocIndex:6},{value:"disabledDate",paraId:15,tocIndex:6},{value:" and ",paraId:15,tocIndex:6},{value:"disabledTime",paraId:15,tocIndex:6},{value:", we can control the date and time more finely to meet different business requirements. With the examples above, I believe you have a deeper understanding of these two APIs. In actual business, you can combine these two APIs to achieve more functions.",paraId:15,tocIndex:6}];}}]);