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

[pickers] Refactor shouldDisableTime #7299

Merged
merged 14 commits into from
Jan 10, 2023
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default function TimeValidationTimePicker() {
onChange={(newValue) => {
setValue(newValue);
}}
shouldDisableTime={(timeValue, clockType) => {
shouldDisableClock={(timeValue, clockType) => {
if (clockType === 'hours' && timeValue % 2) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default function TimeValidationTimePicker() {
onChange={(newValue) => {
setValue(newValue);
}}
shouldDisableTime={(timeValue, clockType) => {
shouldDisableClock={(timeValue, clockType) => {
if (clockType === 'hours' && timeValue % 2) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { Unstable_NextTimePicker as NextTimePicker } from '@mui/x-date-pickers/NextTimePicker';
import { Unstable_NextDateTimePicker as NextDateTimePicker } from '@mui/x-date-pickers/NextDateTimePicker';

const shouldDisableTime = (timeValue, view) => view === 'minutes' && timeValue >= 45;
const shouldDisableTime = (value, view) =>
view === 'minutes' && value.minute() >= 45;

const defaultValue = dayjs().set('hour', 10).set('minute', 50).startOf('minute');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import {
import { Unstable_NextDateTimePicker as NextDateTimePicker } from '@mui/x-date-pickers/NextDateTimePicker';

const shouldDisableTime: NextTimePickerProps<Dayjs>['shouldDisableTime'] = (
timeValue,
value,
view,
) => view === 'minutes' && timeValue >= 45;
) => view === 'minutes' && value.minute() >= 45;

const defaultValue = dayjs().set('hour', 10).set('minute', 50).startOf('minute');

Expand Down
8 changes: 4 additions & 4 deletions docs/data/date-pickers/validation/validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,21 +161,21 @@ The simplest way to use it is to pass today's date and only care about the hour
For example to disable the afternoon in `dayjs` you can pass `dayjs().set('hour', 12).startOf('hour')`.
:::

### Disable specific times
### Disable specific time

The `shouldDisableTime` prop prevents the selection of all values for which it returns `true`.

This callback receives the current view and the value to be tested:

```tsx
// Disables the hours between 12 AM and 3 PM.
shouldDisableTime={(timeValue, view) => currentView === 'hours' && timeValue > 12 && timeValue < 15}
shouldDisableTime={(value, view) => view === 'hours' && value.hour() > 12 && value.hour() < 15}

// Disables the last quarter of each hour.
shouldDisableTime={(timeValue, view) => view === 'minutes' && timeValue >= 45};
shouldDisableTime={(value, view) => view === 'minutes' && value.minute() >= 45};

// Disables the second half of each minute.
shouldDisableTime={(timeValue, view) => view === 'seconds' && timeValue >= 30};
shouldDisableTime={(value, view) => view === 'seconds' && value.second() > 30};
```

In the example below—the last quarter of each hour is not selectable.
Expand Down
5 changes: 5 additions & 0 deletions docs/pages/x/api/date-pickers/date-time-field.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@
"description": "'all'<br>&#124;&nbsp;'day'<br>&#124;&nbsp;'hours'<br>&#124;&nbsp;'meridiem'<br>&#124;&nbsp;'minutes'<br>&#124;&nbsp;'month'<br>&#124;&nbsp;'seconds'<br>&#124;&nbsp;'year'<br>&#124;&nbsp;number<br>&#124;&nbsp;{ endIndex: number, startIndex: number }"
}
},
"shouldDisableClock": {
"type": { "name": "func" },
"deprecated": true,
"deprecationInfo": "Consider using <code>shouldDisableTime</code>."
},
"shouldDisableDate": { "type": { "name": "func" } },
"shouldDisableMonth": { "type": { "name": "func" } },
"shouldDisableTime": { "type": { "name": "func" } },
Expand Down
5 changes: 5 additions & 0 deletions docs/pages/x/api/date-pickers/date-time-picker.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@
"default": "() => <span data-mui-test=\"loading-progress\">...</span>"
},
"rifmFormatter": { "type": { "name": "func" } },
"shouldDisableClock": {
"type": { "name": "func" },
"deprecated": true,
"deprecationInfo": "Consider using <code>shouldDisableTime</code>."
},
"shouldDisableDate": { "type": { "name": "func" } },
"shouldDisableMonth": { "type": { "name": "func" } },
"shouldDisableTime": { "type": { "name": "func" } },
Expand Down
5 changes: 5 additions & 0 deletions docs/pages/x/api/date-pickers/desktop-date-time-picker.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@
"default": "() => <span data-mui-test=\"loading-progress\">...</span>"
},
"rifmFormatter": { "type": { "name": "func" } },
"shouldDisableClock": {
"type": { "name": "func" },
"deprecated": true,
"deprecationInfo": "Consider using <code>shouldDisableTime</code>."
},
"shouldDisableDate": { "type": { "name": "func" } },
"shouldDisableMonth": { "type": { "name": "func" } },
"shouldDisableTime": { "type": { "name": "func" } },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@
"description": "'all'<br>&#124;&nbsp;'day'<br>&#124;&nbsp;'hours'<br>&#124;&nbsp;'meridiem'<br>&#124;&nbsp;'minutes'<br>&#124;&nbsp;'month'<br>&#124;&nbsp;'seconds'<br>&#124;&nbsp;'year'<br>&#124;&nbsp;number<br>&#124;&nbsp;{ endIndex: number, startIndex: number }"
}
},
"shouldDisableClock": {
"type": { "name": "func" },
"deprecated": true,
"deprecationInfo": "Consider using <code>shouldDisableTime</code>."
},
"shouldDisableDate": { "type": { "name": "func" } },
"shouldDisableMonth": { "type": { "name": "func" } },
"shouldDisableTime": { "type": { "name": "func" } },
Expand Down
5 changes: 5 additions & 0 deletions docs/pages/x/api/date-pickers/desktop-next-time-picker.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@
"description": "'all'<br>&#124;&nbsp;'day'<br>&#124;&nbsp;'hours'<br>&#124;&nbsp;'meridiem'<br>&#124;&nbsp;'minutes'<br>&#124;&nbsp;'month'<br>&#124;&nbsp;'seconds'<br>&#124;&nbsp;'year'<br>&#124;&nbsp;number<br>&#124;&nbsp;{ endIndex: number, startIndex: number }"
}
},
"shouldDisableClock": {
"type": { "name": "func" },
"deprecated": true,
"deprecationInfo": "Consider using <code>shouldDisableTime</code>."
},
"shouldDisableTime": { "type": { "name": "func" } },
"showToolbar": {
"type": { "name": "bool" },
Expand Down
5 changes: 5 additions & 0 deletions docs/pages/x/api/date-pickers/desktop-time-picker.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
},
"readOnly": { "type": { "name": "bool" } },
"rifmFormatter": { "type": { "name": "func" } },
"shouldDisableClock": {
"type": { "name": "func" },
"deprecated": true,
"deprecationInfo": "Consider using <code>shouldDisableTime</code>."
},
"shouldDisableTime": { "type": { "name": "func" } },
"showToolbar": { "type": { "name": "bool" } },
"value": { "type": { "name": "any" } },
Expand Down
5 changes: 5 additions & 0 deletions docs/pages/x/api/date-pickers/mobile-date-time-picker.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@
"default": "() => <span data-mui-test=\"loading-progress\">...</span>"
},
"rifmFormatter": { "type": { "name": "func" } },
"shouldDisableClock": {
"type": { "name": "func" },
"deprecated": true,
"deprecationInfo": "Consider using <code>shouldDisableTime</code>."
},
"shouldDisableDate": { "type": { "name": "func" } },
"shouldDisableMonth": { "type": { "name": "func" } },
"shouldDisableTime": { "type": { "name": "func" } },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@
"description": "'all'<br>&#124;&nbsp;'day'<br>&#124;&nbsp;'hours'<br>&#124;&nbsp;'meridiem'<br>&#124;&nbsp;'minutes'<br>&#124;&nbsp;'month'<br>&#124;&nbsp;'seconds'<br>&#124;&nbsp;'year'<br>&#124;&nbsp;number<br>&#124;&nbsp;{ endIndex: number, startIndex: number }"
}
},
"shouldDisableClock": {
"type": { "name": "func" },
"deprecated": true,
"deprecationInfo": "Consider using <code>shouldDisableTime</code>."
},
"shouldDisableDate": { "type": { "name": "func" } },
"shouldDisableMonth": { "type": { "name": "func" } },
"shouldDisableTime": { "type": { "name": "func" } },
Expand Down
5 changes: 5 additions & 0 deletions docs/pages/x/api/date-pickers/mobile-next-time-picker.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@
"description": "'all'<br>&#124;&nbsp;'day'<br>&#124;&nbsp;'hours'<br>&#124;&nbsp;'meridiem'<br>&#124;&nbsp;'minutes'<br>&#124;&nbsp;'month'<br>&#124;&nbsp;'seconds'<br>&#124;&nbsp;'year'<br>&#124;&nbsp;number<br>&#124;&nbsp;{ endIndex: number, startIndex: number }"
}
},
"shouldDisableClock": {
"type": { "name": "func" },
"deprecated": true,
"deprecationInfo": "Consider using <code>shouldDisableTime</code>."
},
"shouldDisableTime": { "type": { "name": "func" } },
"showToolbar": {
"type": { "name": "bool" },
Expand Down
5 changes: 5 additions & 0 deletions docs/pages/x/api/date-pickers/mobile-time-picker.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
},
"readOnly": { "type": { "name": "bool" } },
"rifmFormatter": { "type": { "name": "func" } },
"shouldDisableClock": {
"type": { "name": "func" },
"deprecated": true,
"deprecationInfo": "Consider using <code>shouldDisableTime</code>."
},
"shouldDisableTime": { "type": { "name": "func" } },
"showToolbar": { "type": { "name": "bool" } },
"value": { "type": { "name": "any" } },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
"description": "'all'<br>&#124;&nbsp;'day'<br>&#124;&nbsp;'hours'<br>&#124;&nbsp;'meridiem'<br>&#124;&nbsp;'minutes'<br>&#124;&nbsp;'month'<br>&#124;&nbsp;'seconds'<br>&#124;&nbsp;'year'<br>&#124;&nbsp;number<br>&#124;&nbsp;{ endIndex: number, startIndex: number }"
}
},
"shouldDisableClock": {
"type": { "name": "func" },
"deprecated": true,
"deprecationInfo": "Consider using <code>shouldDisableTime</code>."
},
"shouldDisableDate": { "type": { "name": "func" } },
"shouldDisableTime": { "type": { "name": "func" } },
"spacing": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
"description": "'all'<br>&#124;&nbsp;'day'<br>&#124;&nbsp;'hours'<br>&#124;&nbsp;'meridiem'<br>&#124;&nbsp;'minutes'<br>&#124;&nbsp;'month'<br>&#124;&nbsp;'seconds'<br>&#124;&nbsp;'year'<br>&#124;&nbsp;number<br>&#124;&nbsp;{ endIndex: number, startIndex: number }"
}
},
"shouldDisableClock": {
"type": { "name": "func" },
"deprecated": true,
"deprecationInfo": "Consider using <code>shouldDisableTime</code>."
},
"shouldDisableTime": { "type": { "name": "func" } },
"spacing": {
"type": {
Expand Down
5 changes: 5 additions & 0 deletions docs/pages/x/api/date-pickers/next-date-time-picker.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@
"description": "'all'<br>&#124;&nbsp;'day'<br>&#124;&nbsp;'hours'<br>&#124;&nbsp;'meridiem'<br>&#124;&nbsp;'minutes'<br>&#124;&nbsp;'month'<br>&#124;&nbsp;'seconds'<br>&#124;&nbsp;'year'<br>&#124;&nbsp;number<br>&#124;&nbsp;{ endIndex: number, startIndex: number }"
}
},
"shouldDisableClock": {
"type": { "name": "func" },
"deprecated": true,
"deprecationInfo": "Consider using <code>shouldDisableTime</code>."
},
"shouldDisableDate": { "type": { "name": "func" } },
"shouldDisableMonth": { "type": { "name": "func" } },
"shouldDisableTime": { "type": { "name": "func" } },
Expand Down
5 changes: 5 additions & 0 deletions docs/pages/x/api/date-pickers/next-time-picker.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
"description": "'all'<br>&#124;&nbsp;'day'<br>&#124;&nbsp;'hours'<br>&#124;&nbsp;'meridiem'<br>&#124;&nbsp;'minutes'<br>&#124;&nbsp;'month'<br>&#124;&nbsp;'seconds'<br>&#124;&nbsp;'year'<br>&#124;&nbsp;number<br>&#124;&nbsp;{ endIndex: number, startIndex: number }"
}
},
"shouldDisableClock": {
"type": { "name": "func" },
"deprecated": true,
"deprecationInfo": "Consider using <code>shouldDisableTime</code>."
},
"shouldDisableTime": { "type": { "name": "func" } },
"showToolbar": {
"type": { "name": "bool" },
Expand Down
5 changes: 5 additions & 0 deletions docs/pages/x/api/date-pickers/static-date-time-picker.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@
"default": "() => <span data-mui-test=\"loading-progress\">...</span>"
},
"rifmFormatter": { "type": { "name": "func" } },
"shouldDisableClock": {
"type": { "name": "func" },
"deprecated": true,
"deprecationInfo": "Consider using <code>shouldDisableTime</code>."
},
"shouldDisableDate": { "type": { "name": "func" } },
"shouldDisableMonth": { "type": { "name": "func" } },
"shouldDisableTime": { "type": { "name": "func" } },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@
"type": { "name": "func" },
"default": "() => <span data-mui-test=\"loading-progress\">...</span>"
},
"shouldDisableClock": {
"type": { "name": "func" },
"deprecated": true,
"deprecationInfo": "Consider using <code>shouldDisableTime</code>."
},
"shouldDisableDate": { "type": { "name": "func" } },
"shouldDisableMonth": { "type": { "name": "func" } },
"shouldDisableTime": { "type": { "name": "func" } },
Expand Down
5 changes: 5 additions & 0 deletions docs/pages/x/api/date-pickers/static-next-time-picker.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
"orientation": {
"type": { "name": "enum", "description": "'landscape'<br>&#124;&nbsp;'portrait'" }
},
"shouldDisableClock": {
"type": { "name": "func" },
"deprecated": true,
"deprecationInfo": "Consider using <code>shouldDisableTime</code>."
},
"shouldDisableTime": { "type": { "name": "func" } },
"showToolbar": {
"type": { "name": "bool" },
Expand Down
5 changes: 5 additions & 0 deletions docs/pages/x/api/date-pickers/static-time-picker.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@
},
"readOnly": { "type": { "name": "bool" } },
"rifmFormatter": { "type": { "name": "func" } },
"shouldDisableClock": {
"type": { "name": "func" },
"deprecated": true,
"deprecationInfo": "Consider using <code>shouldDisableTime</code>."
},
"shouldDisableTime": { "type": { "name": "func" } },
"showToolbar": { "type": { "name": "bool" } },
"sx": {
Expand Down
5 changes: 5 additions & 0 deletions docs/pages/x/api/date-pickers/time-clock.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
"default": "'hours'"
},
"readOnly": { "type": { "name": "bool" } },
"shouldDisableClock": {
"type": { "name": "func" },
"deprecated": true,
"deprecationInfo": "Consider using <code>shouldDisableTime</code>."
},
"shouldDisableTime": { "type": { "name": "func" } },
"sx": {
"type": {
Expand Down
5 changes: 5 additions & 0 deletions docs/pages/x/api/date-pickers/time-field.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
"description": "'all'<br>&#124;&nbsp;'day'<br>&#124;&nbsp;'hours'<br>&#124;&nbsp;'meridiem'<br>&#124;&nbsp;'minutes'<br>&#124;&nbsp;'month'<br>&#124;&nbsp;'seconds'<br>&#124;&nbsp;'year'<br>&#124;&nbsp;number<br>&#124;&nbsp;{ endIndex: number, startIndex: number }"
}
},
"shouldDisableClock": {
"type": { "name": "func" },
"deprecated": true,
"deprecationInfo": "Consider using <code>shouldDisableTime</code>."
},
"shouldDisableTime": { "type": { "name": "func" } },
"size": { "type": { "name": "enum", "description": "'medium'<br>&#124;&nbsp;'small'" } },
"sx": {
Expand Down
5 changes: 5 additions & 0 deletions docs/pages/x/api/date-pickers/time-picker.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@
},
"readOnly": { "type": { "name": "bool" } },
"rifmFormatter": { "type": { "name": "func" } },
"shouldDisableClock": {
"type": { "name": "func" },
"deprecated": true,
"deprecationInfo": "Consider using <code>shouldDisableTime</code>."
},
"shouldDisableTime": { "type": { "name": "func" } },
"showToolbar": { "type": { "name": "bool" } },
"value": { "type": { "name": "any" } },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@
"readOnly": "It prevents the user from changing the value of the field (not from interacting with the field).",
"required": "If <code>true</code>, the label is displayed as required and the <code>input</code> element is required.",
"selectedSections": "The currently selected sections. This prop accept four formats: 1. If a number is provided, the section at this index will be selected. 2. If an object with a <code>startIndex</code> and <code>endIndex</code> properties are provided, the sections between those two indexes will be selected. 3. If a string of type <code>MuiDateSectionName</code> is provided, the first section with that name will be selected. 4. If <code>null</code> is provided, no section will be selected If not provided, the selected sections will be handled internally.",
"shouldDisableClock": "Disable specific clock time.<br><br><strong>Signature:</strong><br><code>function(clockValue: number, view: TimeView) =&gt; boolean</code><br><em>clockValue:</em> The value to check.<br><em>view:</em> The clock type of the timeValue.<br> <em>returns</em> (boolean): If <code>true</code> the time will be disabled.",
"shouldDisableDate": "Disable specific date.<br><br><strong>Signature:</strong><br><code>function(day: TDate) =&gt; boolean</code><br><em>day:</em> The date to test.<br> <em>returns</em> (boolean): If <code>true</code> the date will be disabled.",
"shouldDisableMonth": "Disable specific month.<br><br><strong>Signature:</strong><br><code>function(month: TDate) =&gt; boolean</code><br><em>month:</em> The month to test.<br> <em>returns</em> (boolean): If <code>true</code> the month will be disabled.",
"shouldDisableTime": "Disable specific time.<br><br><strong>Signature:</strong><br><code>function(timeValue: number, view: TimeView) =&gt; boolean</code><br><em>timeValue:</em> The value to check.<br><em>view:</em> The clock type of the timeValue.<br> <em>returns</em> (boolean): If <code>true</code> the time will be disabled.",
"shouldDisableTime": "Disable specific time.<br><br><strong>Signature:</strong><br><code>function(value: TDate, view: TimeView) =&gt; boolean</code><br><em>value:</em> The value to check.<br><em>view:</em> The clock type of the timeValue.<br> <em>returns</em> (boolean): If <code>true</code> the time will be disabled.",
"shouldDisableYear": "Disable specific year.<br><br><strong>Signature:</strong><br><code>function(year: TDate) =&gt; boolean</code><br><em>year:</em> The year to test.<br> <em>returns</em> (boolean): If <code>true</code> the year will be disabled.",
"size": "The size of the component.",
"sx": "The system prop that allows defining system overrides as well as additional CSS styles. See the <a href=\"/system/getting-started/the-sx-prop/\">`sx` page</a> for more details.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@
"readOnly": "It prevents the user from changing the value of the field (not from interacting with the field).",
"required": "If <code>true</code>, the label is displayed as required and the <code>input</code> element is required.",
"selectedSections": "The currently selected sections. This prop accept four formats: 1. If a number is provided, the section at this index will be selected. 2. If an object with a <code>startIndex</code> and <code>endIndex</code> properties are provided, the sections between those two indexes will be selected. 3. If a string of type <code>MuiDateSectionName</code> is provided, the first section with that name will be selected. 4. If <code>null</code> is provided, no section will be selected If not provided, the selected sections will be handled internally.",
"shouldDisableClock": "Disable specific clock time.<br><br><strong>Signature:</strong><br><code>function(clockValue: number, view: TimeView) =&gt; boolean</code><br><em>clockValue:</em> The value to check.<br><em>view:</em> The clock type of the timeValue.<br> <em>returns</em> (boolean): If <code>true</code> the time will be disabled.",
"shouldDisableDate": "Disable specific date.<br><br><strong>Signature:</strong><br><code>function(day: TDate) =&gt; boolean</code><br><em>day:</em> The date to test.<br> <em>returns</em> (boolean): If <code>true</code> the date will be disabled.",
"shouldDisableMonth": "Disable specific month.<br><br><strong>Signature:</strong><br><code>function(month: TDate) =&gt; boolean</code><br><em>month:</em> The month to test.<br> <em>returns</em> (boolean): If <code>true</code> the month will be disabled.",
"shouldDisableTime": "Disable specific time.<br><br><strong>Signature:</strong><br><code>function(timeValue: number, view: TimeView) =&gt; boolean</code><br><em>timeValue:</em> The value to check.<br><em>view:</em> The clock type of the timeValue.<br> <em>returns</em> (boolean): If <code>true</code> the time will be disabled.",
"shouldDisableTime": "Disable specific time.<br><br><strong>Signature:</strong><br><code>function(value: TDate, view: TimeView) =&gt; boolean</code><br><em>value:</em> The value to check.<br><em>view:</em> The clock type of the timeValue.<br> <em>returns</em> (boolean): If <code>true</code> the time will be disabled.",
"shouldDisableYear": "Disable specific year.<br><br><strong>Signature:</strong><br><code>function(year: TDate) =&gt; boolean</code><br><em>year:</em> The year to test.<br> <em>returns</em> (boolean): If <code>true</code> the year will be disabled.",
"size": "The size of the component.",
"sx": "The system prop that allows defining system overrides as well as additional CSS styles. See the <a href=\"/system/getting-started/the-sx-prop/\">`sx` page</a> for more details.",
Expand Down
Loading