From d4accc0a41f7203ac2b9f5f17d0829fb6bbf5e7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E8=B4=A7=E6=9C=BA=E5=99=A8=E4=BA=BA?= Date: Mon, 15 Jan 2024 17:54:24 +0800 Subject: [PATCH 1/3] fix: disabled logic should work --- docs/examples/debug.tsx | 13 +++++++++++++ src/hooks/useTimeConfig.ts | 35 +++++++++++++++++++---------------- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/docs/examples/debug.tsx b/docs/examples/debug.tsx index f5d9fa6b8..c707d499b 100644 --- a/docs/examples/debug.tsx +++ b/docs/examples/debug.tsx @@ -150,9 +150,22 @@ export default () => { // Shared {...sharedLocale} disabledDate={(date) => date.isBefore(dayjs())} + // disabledTime={() => ({ + // disabledHours: () => [0, 1, 2, 3, 4, 5], + // disabledMinutes: () => [0, 1, 2, 3, 4, 5], + // disabledSeconds: () => [0, 1, 2, 3, 4, 5], + // })} open ref={singleRef} suffixIcon="🧶" + // showTime={{ + // disabledTime: () => ({ + // disabledHours: () => [0, 1, 2, 3, 4, 5], + // disabledMinutes: () => [0, 1, 2, 3, 4, 5], + // disabledSeconds: () => [0, 1, 2, 3, 4, 5], + // }), + // }} + showTime={{}} onChange={(...args) => { console.log('🔥 Change:', ...args); }} diff --git a/src/hooks/useTimeConfig.ts b/src/hooks/useTimeConfig.ts index 5f0d3f18b..18800d979 100644 --- a/src/hooks/useTimeConfig.ts +++ b/src/hooks/useTimeConfig.ts @@ -7,7 +7,7 @@ function checkShow(format: string, keywords: string[], show?: boolean) { } const showTimeKeys = [ - 'format', + // 'format', 'showNow', 'showHour', 'showMinute', @@ -31,18 +31,21 @@ const showTimeKeys = [ /** * Get SharedTimeProps from props. */ -function pickTimeProps(props: any): SharedTimeProps { +function pickTimeProps( + props: any, +): [timeProps: SharedTimeProps, propFormat: string] { const timeProps: any = pickProps(props, showTimeKeys); + const { format } = props; - if (timeProps.format) { - let format = timeProps.format; + let propFormat: string; + if (format) { if (Array.isArray(format)) { - format = format[0]; + propFormat = format[0]; } - timeProps.format = typeof format === 'object' ? format.format : format; + propFormat = typeof format === 'object' ? format.format : format; } - return timeProps; + return [timeProps, propFormat]; } function isStringFormat(format: any): format is string { @@ -68,11 +71,15 @@ export function getTimeProps( showTimeFormat: string, propFormat: string, ] { - const { showTime, picker } = componentProps; + const { showTime } = componentProps; - const pickedProps = pickTimeProps(componentProps); - const isShowTimeConfig = showTime && typeof showTime === 'object'; - const timeConfig = isShowTimeConfig ? showTime : pickedProps; + const [pickedProps, propFormat] = pickTimeProps(componentProps); + + const showTimeConfig = showTime && typeof showTime === 'object' ? showTime : {}; + const timeConfig = { + ...pickedProps, + ...showTimeConfig, + }; const { showMillisecond } = timeConfig; let { showHour, showMinute, showSecond } = timeConfig; @@ -83,11 +90,7 @@ export function getTimeProps( showSecond = true; } - const mergedFormat = isShowTimeConfig - ? showTime.format - : picker === 'time' - ? pickedProps.format - : null; + const mergedFormat = showTimeConfig.format || propFormat; return [ timeConfig, From 13a2544c0250e02cd3e4e9f25e97da1b759e06c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E8=B4=A7=E6=9C=BA=E5=99=A8=E4=BA=BA?= Date: Mon, 15 Jan 2024 18:04:50 +0800 Subject: [PATCH 2/3] test: add test case --- tests/picker.spec.tsx | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/tests/picker.spec.tsx b/tests/picker.spec.tsx index c36b0949a..91f25477e 100644 --- a/tests/picker.spec.tsx +++ b/tests/picker.spec.tsx @@ -605,7 +605,7 @@ describe('Picker.Basic', () => { it('time should display now', () => { const onCalendarChange = jest.fn(); const { container } = render(); - + openPicker(container); expect(document.querySelector('.rc-picker-header')).toBeFalsy(); @@ -1356,4 +1356,24 @@ describe('Picker.Basic', () => { expect(inputEle.size).toBe(12); expect(inputEle).toHaveValue('06:03:04 PM'); }); + + it('compatible with disabledTime on prop directly', () => { + render( + ({ + disabledHours: () => [0], + })} + hideDisabledOptions + showTime + open + />, + ); + + expect(document.querySelectorAll('.rc-picker-time-panel-column:first-child li')).toHaveLength( + 23, + ); + expect( + document.querySelector('.rc-picker-time-panel-column:first-child li').textContent, + ).toEqual('01'); + }); }); From d0b3de70703cf48f6ca3f31575bffbb78fca5b87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E8=B4=A7=E6=9C=BA=E5=99=A8=E4=BA=BA?= Date: Mon, 15 Jan 2024 19:24:59 +0800 Subject: [PATCH 3/3] fix: merge logic --- src/hooks/useTimeConfig.ts | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/hooks/useTimeConfig.ts b/src/hooks/useTimeConfig.ts index 18800d979..cd2d027a3 100644 --- a/src/hooks/useTimeConfig.ts +++ b/src/hooks/useTimeConfig.ts @@ -35,14 +35,20 @@ function pickTimeProps( props: any, ): [timeProps: SharedTimeProps, propFormat: string] { const timeProps: any = pickProps(props, showTimeKeys); - const { format } = props; + const { format, picker } = props; - let propFormat: string; + let propFormat: typeof format = null; if (format) { - if (Array.isArray(format)) { - propFormat = format[0]; + propFormat = format; + + if (Array.isArray(propFormat)) { + propFormat = propFormat[0]; } - propFormat = typeof format === 'object' ? format.format : format; + propFormat = typeof propFormat === 'object' ? propFormat.format : propFormat; + } + + if (picker === 'time') { + timeProps.format = propFormat; } return [timeProps, propFormat]; @@ -90,8 +96,6 @@ export function getTimeProps( showSecond = true; } - const mergedFormat = showTimeConfig.format || propFormat; - return [ timeConfig, { @@ -101,8 +105,8 @@ export function getTimeProps( showSecond, showMillisecond, }, - mergedFormat, - pickedProps.format, + timeConfig.format, + propFormat, ]; }