Skip to content

Commit

Permalink
Merge 6d646a7 into 09b3ab1
Browse files Browse the repository at this point in the history
  • Loading branch information
ZWkang authored Oct 26, 2023
2 parents 09b3ab1 + 6d646a7 commit 86180c4
Showing 1 changed file with 46 additions and 13 deletions.
59 changes: 46 additions & 13 deletions src/Picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@
import type { AlignType } from '@rc-component/trigger/lib/interface';
import classNames from 'classnames';
import useMergedState from 'rc-util/lib/hooks/useMergedState';
import warning from 'rc-util/lib/warning';
import pickAttrs from 'rc-util/lib/pickAttrs';
import warning from 'rc-util/lib/warning';
import * as React from 'react';
import { GenerateConfig } from './generate';
import useHoverValue from './hooks/useHoverValue';
import usePickerInput from './hooks/usePickerInput';
import usePresets from './hooks/usePresets';
import useTextValueMapping from './hooks/useTextValueMapping';
import useValueTexts from './hooks/useValueTexts';
import type { CustomFormat, PickerMode, PresetDate } from './interface';
import type { CustomFormat, DisabledTime, PickerMode, PresetDate } from './interface';
import type { ContextOperationRefProps } from './PanelContext';
import PanelContext from './PanelContext';
import type {
Expand All @@ -34,10 +35,10 @@ import PickerPanel from './PickerPanel';
import PickerTrigger from './PickerTrigger';
import PresetPanel from './PresetPanel';
import { formatValue, isEqual, parseValue } from './utils/dateUtil';
import { getClearIcon } from './utils/getClearIcon';
import { toArray } from './utils/miscUtil';
import { elementsContains, getDefaultFormat, getInputSize } from './utils/uiUtil';
import { legacyPropsWarning } from './utils/warnUtil';
import { getClearIcon } from './utils/getClearIcon';

export type PickerRefConfig = {
focus: () => void;
Expand Down Expand Up @@ -67,8 +68,8 @@ export type PickerSharedProps<DateType> = {

// Render
suffixIcon?: React.ReactNode;
/**
* Clear all icon
/**
* Clear all icon
* @deprecated Please use `allowClear` instead
**/
clearIcon?: React.ReactNode;
Expand Down Expand Up @@ -145,6 +146,32 @@ type MergedPickerProps<DateType> = {
picker?: PickerMode;
} & OmitType<DateType>;

function testValueInSet(num: number, range: number[]) {

Check warning on line 149 in src/Picker.tsx

View check run for this annotation

Codecov / codecov/patch

src/Picker.tsx#L149

Added line #L149 was not covered by tests
if (typeof num === 'undefined' || typeof range === 'undefined') return;
const set = new Set(range);
return set.has(num);

Check warning on line 152 in src/Picker.tsx

View check run for this annotation

Codecov / codecov/patch

src/Picker.tsx#L151-L152

Added lines #L151 - L152 were not covered by tests
}

function validateTime<DateType>(
picker: PickerMode,
disabledTime: DisabledTime<DateType>,
date: DateType,
generateConfig: GenerateConfig<DateType>,
) {
if (!disabledTime || picker !== 'date') return false;
const disabledTimes = disabledTime(date);

Check warning on line 162 in src/Picker.tsx

View check run for this annotation

Codecov / codecov/patch

src/Picker.tsx#L162

Added line #L162 was not covered by tests
if (!disabledTimes) return false;
const { disabledHours, disabledMinutes, disabledSeconds } = disabledTimes;
const hour = generateConfig.getHour(date);
const minter = generateConfig.getMinute(date);
const second = generateConfig.getSecond(date);

Check warning on line 167 in src/Picker.tsx

View check run for this annotation

Codecov / codecov/patch

src/Picker.tsx#L164-L167

Added lines #L164 - L167 were not covered by tests

const validateHour = testValueInSet(hour, disabledHours?.());
const validateMinute = testValueInSet(minter, disabledMinutes?.(hour));
const validateSecond = testValueInSet(second, disabledSeconds?.(hour, minter));

Check warning on line 171 in src/Picker.tsx

View check run for this annotation

Codecov / codecov/patch

src/Picker.tsx#L169-L171

Added lines #L169 - L171 were not covered by tests
return validateHour || validateMinute || validateSecond;
}

function InnerPicker<DateType>(props: PickerProps<DateType>) {
const {
prefixCls = 'rc-picker',
Expand Down Expand Up @@ -196,6 +223,7 @@ function InnerPicker<DateType>(props: PickerProps<DateType>) {
autoComplete = 'off',
inputRender,
changeOnBlur,
disabledTime,
} = props as MergedPickerProps<DateType>;

const inputRef = React.useRef<HTMLInputElement>(null);
Expand Down Expand Up @@ -253,6 +281,8 @@ function InnerPicker<DateType>(props: PickerProps<DateType>) {
locale,
});

const timeProps = typeof showTime === 'object' ? showTime : {};

const [text, triggerTextChange, resetText] = useTextValueMapping({
valueTexts,
onTextChange: (newText) => {
Expand All @@ -261,7 +291,11 @@ function InnerPicker<DateType>(props: PickerProps<DateType>) {
formatList,
generateConfig,
});
if (inputDate && (!disabledDate || !disabledDate(inputDate))) {
if (
inputDate &&
(!disabledDate || !disabledDate(inputDate)) &&
!validateTime(picker, disabledTime, inputDate || timeProps?.defaultValue, generateConfig)
) {
setSelectedValue(inputDate);
}
},
Expand Down Expand Up @@ -340,7 +374,8 @@ function InnerPicker<DateType>(props: PickerProps<DateType>) {
// When user typing disabledDate with keyboard and enter, this value will be empty
!selectedValue ||
// Normal disabled check
(disabledDate && disabledDate(selectedValue))
(disabledDate && disabledDate(selectedValue)) ||
validateTime(picker, disabledTime, selectedValue || timeProps?.defaultValue, generateConfig)
) {
return false;
}
Expand Down Expand Up @@ -490,11 +525,7 @@ function InnerPicker<DateType>(props: PickerProps<DateType>) {
);
}

const mergedClearIcon: React.ReactNode = getClearIcon(
prefixCls,
allowClear,
clearIcon,
);
const mergedClearIcon: React.ReactNode = getClearIcon(prefixCls, allowClear, clearIcon);

const clearNode: React.ReactNode = (
<span
Expand All @@ -517,7 +548,9 @@ function InnerPicker<DateType>(props: PickerProps<DateType>) {

const mergedAllowClear = !!allowClear && mergedValue && !disabled;

const mergedInputProps: React.InputHTMLAttributes<HTMLInputElement> & { ref: React.MutableRefObject<HTMLInputElement> } = {
const mergedInputProps: React.InputHTMLAttributes<HTMLInputElement> & {
ref: React.MutableRefObject<HTMLInputElement>;
} = {
id,
tabIndex,
disabled,
Expand Down

0 comments on commit 86180c4

Please sign in to comment.