From c4ffe3681471d45527f08bfc85c28b5370f4fb44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E8=B4=A7=E7=88=B1=E5=90=83=E7=99=BD=E8=90=9D?= =?UTF-8?q?=E5=8D=9C?= Date: Tue, 23 Apr 2024 17:57:28 +0800 Subject: [PATCH] fix: min/maxDate should also blocked by typing (#802) * test: test driven * fix: not exceed boundary --- docs/examples/debug.tsx | 18 +++++++++--- src/PickerInput/hooks/useDisabledBoundary.ts | 30 ------------------- src/PickerInput/hooks/useFilledProps.ts | 7 ++++- tests/new-range.spec.tsx | 31 ++++++++++++++++++++ 4 files changed, 51 insertions(+), 35 deletions(-) diff --git a/docs/examples/debug.tsx b/docs/examples/debug.tsx index be9618749..129b097a4 100644 --- a/docs/examples/debug.tsx +++ b/docs/examples/debug.tsx @@ -45,10 +45,20 @@ const dateFnsSharedLocale = { export default () => { return (
- {/* - */} - - + + console.error('>>>>>>>', val)} + /> + {/* */} + {/* */} + console.error('>>>>>>>', val)} + />
); }; diff --git a/src/PickerInput/hooks/useDisabledBoundary.ts b/src/PickerInput/hooks/useDisabledBoundary.ts index cee0a789f..7f00f37c0 100644 --- a/src/PickerInput/hooks/useDisabledBoundary.ts +++ b/src/PickerInput/hooks/useDisabledBoundary.ts @@ -43,35 +43,5 @@ export default function useDisabledBoundary( return false; }); - // const isInValidBoundary: IsInvalidBoundary = (date, type, from) => { - // const toBoundaryDate = (boundary?: LimitDate) => - // typeof boundary === 'function' - // ? boundary({ - // from, - // }) - // : boundary; - - // const mergedMinDate = toBoundaryDate(minDate); - // const mergedMaxDate = toBoundaryDate(maxDate); - - // if ( - // mergedMinDate && - // generateConfig.isAfter(mergedMinDate, date) && - // !isSame(generateConfig, locale, mergedMinDate, date, type) - // ) { - // return true; - // } - - // if ( - // mergedMaxDate && - // generateConfig.isAfter(date, mergedMaxDate) && - // !isSame(generateConfig, locale, mergedMaxDate, date, type) - // ) { - // return true; - // } - - // return false; - // }; - return mergedDisabledDate; } diff --git a/src/PickerInput/hooks/useFilledProps.ts b/src/PickerInput/hooks/useFilledProps.ts index e87e23bb4..5b562f61c 100644 --- a/src/PickerInput/hooks/useFilledProps.ts +++ b/src/PickerInput/hooks/useFilledProps.ts @@ -198,7 +198,12 @@ export default function useFilledProps< ); // ====================== Invalidate ====================== - const isInvalidateDate = useInvalidate(generateConfig, picker, disabledDate, mergedShowTime); + const isInvalidateDate = useInvalidate( + generateConfig, + picker, + disabledBoundaryDate, + mergedShowTime, + ); // ======================== Merged ======================== const mergedProps = React.useMemo( diff --git a/tests/new-range.spec.tsx b/tests/new-range.spec.tsx index e7fac79a0..761af8723 100644 --- a/tests/new-range.spec.tsx +++ b/tests/new-range.spec.tsx @@ -9,6 +9,7 @@ import type { RangePickerProps } from '../src'; import zh_CN from '../src/locale/zh_CN'; import { closePicker, + DayPicker, DayRangePicker, findCell, getDay, @@ -1212,6 +1213,36 @@ describe('NewPicker.Range', () => { openPicker(container); expect(document.querySelector('.rc-picker-header-view').textContent).toBe('1990年8月'); }); + + it('typing should not exceed boundary', () => { + const onChange = jest.fn(); + const { container } = render( + , + ); + const inputEle = container.querySelector('input'); + + // Out of range + fireEvent.change(inputEle, { + target: { + value: '2000-01-01', + }, + }); + fireEvent.keyDown(inputEle, { + key: 'Enter', + }); + expect(onChange).not.toHaveBeenCalled(); + + // In range + fireEvent.change(inputEle, { + target: { + value: '2024-03-03', + }, + }); + fireEvent.keyDown(inputEle, { + key: 'Enter', + }); + expect(onChange).toHaveBeenCalled(); + }); }); it('double click now button', () => {