Skip to content

Commit 18ed8c8

Browse files
authored
fix: Fix disabledTime not working on prop directly (#740)
* fix: disabled logic should work * test: add test case * fix: merge logic
1 parent 4b28f3b commit 18ed8c8

File tree

3 files changed

+61
-21
lines changed

3 files changed

+61
-21
lines changed

docs/examples/debug.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,22 @@ export default () => {
150150
// Shared
151151
{...sharedLocale}
152152
disabledDate={(date) => date.isBefore(dayjs())}
153+
// disabledTime={() => ({
154+
// disabledHours: () => [0, 1, 2, 3, 4, 5],
155+
// disabledMinutes: () => [0, 1, 2, 3, 4, 5],
156+
// disabledSeconds: () => [0, 1, 2, 3, 4, 5],
157+
// })}
153158
open
154159
ref={singleRef}
155160
suffixIcon="🧶"
161+
// showTime={{
162+
// disabledTime: () => ({
163+
// disabledHours: () => [0, 1, 2, 3, 4, 5],
164+
// disabledMinutes: () => [0, 1, 2, 3, 4, 5],
165+
// disabledSeconds: () => [0, 1, 2, 3, 4, 5],
166+
// }),
167+
// }}
168+
showTime={{}}
156169
onChange={(...args) => {
157170
console.log('🔥 Change:', ...args);
158171
}}

src/hooks/useTimeConfig.ts

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function checkShow(format: string, keywords: string[], show?: boolean) {
77
}
88

99
const showTimeKeys = [
10-
'format',
10+
// 'format',
1111
'showNow',
1212
'showHour',
1313
'showMinute',
@@ -31,18 +31,27 @@ const showTimeKeys = [
3131
/**
3232
* Get SharedTimeProps from props.
3333
*/
34-
function pickTimeProps<DateType extends object = any>(props: any): SharedTimeProps<DateType> {
34+
function pickTimeProps<DateType extends object = any>(
35+
props: any,
36+
): [timeProps: SharedTimeProps<DateType>, propFormat: string] {
3537
const timeProps: any = pickProps(props, showTimeKeys);
38+
const { format, picker } = props;
3639

37-
if (timeProps.format) {
38-
let format = timeProps.format;
39-
if (Array.isArray(format)) {
40-
format = format[0];
40+
let propFormat: typeof format = null;
41+
if (format) {
42+
propFormat = format;
43+
44+
if (Array.isArray(propFormat)) {
45+
propFormat = propFormat[0];
4146
}
42-
timeProps.format = typeof format === 'object' ? format.format : format;
47+
propFormat = typeof propFormat === 'object' ? propFormat.format : propFormat;
48+
}
49+
50+
if (picker === 'time') {
51+
timeProps.format = propFormat;
4352
}
4453

45-
return timeProps;
54+
return [timeProps, propFormat];
4655
}
4756

4857
function isStringFormat(format: any): format is string {
@@ -68,11 +77,15 @@ export function getTimeProps<DateType extends object>(
6877
showTimeFormat: string,
6978
propFormat: string,
7079
] {
71-
const { showTime, picker } = componentProps;
80+
const { showTime } = componentProps;
7281

73-
const pickedProps = pickTimeProps(componentProps);
74-
const isShowTimeConfig = showTime && typeof showTime === 'object';
75-
const timeConfig = isShowTimeConfig ? showTime : pickedProps;
82+
const [pickedProps, propFormat] = pickTimeProps(componentProps);
83+
84+
const showTimeConfig = showTime && typeof showTime === 'object' ? showTime : {};
85+
const timeConfig = {
86+
...pickedProps,
87+
...showTimeConfig,
88+
};
7689

7790
const { showMillisecond } = timeConfig;
7891
let { showHour, showMinute, showSecond } = timeConfig;
@@ -83,12 +96,6 @@ export function getTimeProps<DateType extends object>(
8396
showSecond = true;
8497
}
8598

86-
const mergedFormat = isShowTimeConfig
87-
? showTime.format
88-
: picker === 'time'
89-
? pickedProps.format
90-
: null;
91-
9299
return [
93100
timeConfig,
94101
{
@@ -98,8 +105,8 @@ export function getTimeProps<DateType extends object>(
98105
showSecond,
99106
showMillisecond,
100107
},
101-
mergedFormat,
102-
pickedProps.format,
108+
timeConfig.format,
109+
propFormat,
103110
];
104111
}
105112

tests/picker.spec.tsx

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ describe('Picker.Basic', () => {
605605
it('time should display now', () => {
606606
const onCalendarChange = jest.fn();
607607
const { container } = render(<DayPicker onCalendarChange={onCalendarChange} picker="time" />);
608-
608+
609609
openPicker(container);
610610
expect(document.querySelector('.rc-picker-header')).toBeFalsy();
611611

@@ -1356,4 +1356,24 @@ describe('Picker.Basic', () => {
13561356
expect(inputEle.size).toBe(12);
13571357
expect(inputEle).toHaveValue('06:03:04 PM');
13581358
});
1359+
1360+
it('compatible with disabledTime on prop directly', () => {
1361+
render(
1362+
<DayPicker
1363+
disabledTime={() => ({
1364+
disabledHours: () => [0],
1365+
})}
1366+
hideDisabledOptions
1367+
showTime
1368+
open
1369+
/>,
1370+
);
1371+
1372+
expect(document.querySelectorAll('.rc-picker-time-panel-column:first-child li')).toHaveLength(
1373+
23,
1374+
);
1375+
expect(
1376+
document.querySelector('.rc-picker-time-panel-column:first-child li').textContent,
1377+
).toEqual('01');
1378+
});
13591379
});

0 commit comments

Comments
 (0)