TimePicker禁用时间参考 Antd TimePicker
https://ant.design/components/time-picker-cn/#components-time-picker-demo-disabled

  • disabledHours,没有参数,function()
  • disabledMinutes,参数是当前选中的时间,function(selectedHour)
  • disabledSeconds,参数是当前选中的时间和分钟,function(selectedHour, selectedMinute)
  • hideDisabledOptions,禁用的时间不显示
  • allowClear
    • 清除时间后,onChange(momentime, timeString)
    • momentime为 null,timeStirng为空字符串

disabledHours 传入的数组,就是禁用的时间

  1. type DisabledTime = (now: Moment) => {
  2. disabledHours?: () => number[];
  3. disabledMinutes?: (selectedHour: number) => number[];
  4. disabledSeconds?: (selectedHour: number, selectedMinute: number) => number[];
  5. };
  6. <TimePicker
  7. onChange={value => {
  8. setEndTime(value);
  9. triggerChange({ end: value });
  10. }}
  11. value={value.end || moment('23:59:59', 'HH:mm:ss')}
  12. allowClear={false}
  13. disabled={disabled}
  14. disabledHours={disEndHouse}
  15. disabledMinutes={disEndMinute}
  16. disabledSeconds={disEndSeconds}
  17. // showTime={{ format: 'HH:mm:ss', minuteStep: 15 }}
  18. />

之前的时间禁用

image.png

  1. import React from 'react'
  2. import { DatePicker } from 'antd'
  3. import { object, func } from 'prop-types'
  4. import moment from 'moment'
  5. const { TimePicker } = DatePicker
  6. TimeSelect.propTypes = {
  7. value: object,
  8. onChange: func.isRequired,
  9. }
  10. function TimeSelect({ value, onChange }) {
  11. const [state, setState] = useState(value || moment())
  12. function timeChange(mtime, timeString) {
  13. // clear momentime: null, timeString: ''
  14. setState(mtime)
  15. onChange(mtime)
  16. }
  17. // 之前的小时禁用,没有参数
  18. function disabledHours() {
  19. // ["13", "59"]
  20. return Array.from({ length: state.hours() }, (_, i) => i)
  21. }
  22. // 之前的分钟禁用,参数是选中的小时
  23. function disabledMinutes(selectedHour) {
  24. if (state.hours() === selectedHour) {
  25. return Array.from({ length: state.minutes() }, (_, i) => i)
  26. }
  27. return []
  28. }
  29. // 之前的秒禁用,参数是当前选中的小时,分钟
  30. function disabledSeconds(selectedHour, selectedMinute) {
  31. const flag = state.hours() === selectedHour && state.minutes() === selectedMinute
  32. if (flag) {
  33. return Array.from({ length: state.seconds() }, (_, i) => i)
  34. }
  35. return []
  36. }
  37. return (
  38. <TimePicker
  39. value={state}
  40. onChange={timeChange}
  41. disabled={disabled}
  42. disabledHours={disabledHours}
  43. disabledMinutes={disabledMinutes}
  44. disabledSeconds={disabledSeconds}
  45. // 之前 & 之后的时间都不显示
  46. hideDisabledOptions
  47. format="HH:mm:ss"
  48. allowClear={false}
  49. />
  50. )
  51. }
  52. export default TimeSelect

之后的时间禁用

image.png

  1. const [state, setState] = useState(value || moment())
  2. // 之后的小时禁用
  3. function disabledHours() {
  4. // ["11", "19"] +1 当前时间可以选择
  5. // [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]
  6. const hours = state.hours() + 1;
  7. return Array.from({length: 24 - hours}, (_, i) => hours+i);
  8. }
  9. // 之后的分钟禁用,参数是选中的小时
  10. function disabledMinutes(selectedHour) {
  11. if(state.hours() === selectedHour) {
  12. const minutes = state.minutes() + 1;
  13. return Array.from({length: 60-minutes}, (_, i) => minutes+i);
  14. }
  15. return [];
  16. }
  17. // 之后的秒禁用,参数是当前选中的小时,分钟
  18. function disabledSeconds(selectedHour, selectedMinute) {
  19. const flag = state.hours() === selectedHour && state.minutes() === selectedMinute;
  20. if(flag) {
  21. const seconds = state.seconds() + 1;
  22. return Array.from({length: 60-seconds}, (_, i) => seconds+i);
  23. }
  24. return [];
  25. }