From 47ab3e62f40988f29cc9e27740be6b3009a4338a 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: Tue, 5 Dec 2023 11:20:20 +0800 Subject: [PATCH] refactor: use changeOnBlur --- docs/examples/debug.tsx | 36 ++++++++----------- src/NewPicker/PickerInput/Selector/Input.tsx | 6 ++-- .../PickerInput/Selector/RangeSelector.tsx | 8 ++--- .../PickerInput/hooks/useRangeValue.ts | 4 --- src/NewPicker/index.tsx | 2 +- src/NewPicker/interface.tsx | 8 +++-- tests/new-range.spec.tsx | 7 ++-- 7 files changed, 31 insertions(+), 40 deletions(-) diff --git a/docs/examples/debug.tsx b/docs/examples/debug.tsx index 85a6d8e95..98822512b 100644 --- a/docs/examples/debug.tsx +++ b/docs/examples/debug.tsx @@ -2,7 +2,6 @@ import * as React from 'react'; import '../../assets/index.less'; import type { Locale, PickerRef } from '../../src/NewPicker/interface'; import RangePicker from '../../src/NewPicker/PickerInput/RangePicker'; -import PickerPanel, { type PickerPanelProps } from '../../src/NewPicker/PickerPanel'; import dayjs, { type Dayjs } from 'dayjs'; import 'dayjs/locale/ar'; @@ -11,7 +10,6 @@ import buddhistEra from 'dayjs/plugin/buddhistEra'; import LocalizedFormat from 'dayjs/plugin/localizedFormat'; import dayjsGenerateConfig from '../../src/generate/dayjs'; import zhCN from '../../src/locale/zh_CN'; -import TimePanel from '../../src/NewPicker/PickerPanel/TimePanel'; dayjs.locale('zh-cn'); // dayjs.locale('ar'); @@ -36,32 +34,22 @@ const sharedLocale = { generateConfig: dayjsGenerateConfig, }; -function CellPicker(props: Partial) { - return ( -
-
{props.picker || 'date'}
- -
- ); -} - -const MyTime = (props: any) => { - return ( -
- 2333 - -
- ); -}; +const MyInput = React.forwardRef>( + (props, ref) => { + console.log('>>>', props); + return ; + }, +); +MyInput.displayName = 'MyInput'; export default () => { const singleRef = React.useRef(null); const [value, setValue] = React.useState(null); const [rangeValue, setRangeValue] = React.useState<[Dayjs?, Dayjs?]>( - [dayjs('2000-12-15'), dayjs('2020-12-22')], + // [dayjs('2000-12-15'), dayjs('2020-12-22')], // null, - // undefined, + undefined, ); return ( @@ -73,6 +61,11 @@ export default () => { { @@ -104,7 +97,6 @@ export default () => { start: 'inputStart', end: 'inputEnd', }} - renderExtraFooter={() => } />
diff --git a/src/NewPicker/PickerInput/Selector/Input.tsx b/src/NewPicker/PickerInput/Selector/Input.tsx index 55ffa100d..8cdd42fd4 100644 --- a/src/NewPicker/PickerInput/Selector/Input.tsx +++ b/src/NewPicker/PickerInput/Selector/Input.tsx @@ -44,7 +44,7 @@ export interface InputProps extends Omit void; - preserveInvalidOnBlur?: boolean; + changeOnBlur?: boolean; invalid?: boolean; } @@ -60,7 +60,7 @@ const Input = React.forwardRef((props, ref) => { onHelp, onSubmit, onKeyDown, - preserveInvalidOnBlur, + changeOnBlur = true, invalid, // Pass to input ...restProps @@ -196,7 +196,7 @@ const Input = React.forwardRef((props, ref) => { // ======================== Active ======================== // Check if blur need reset input value useLockEffect(active, () => { - if (!active && !preserveInvalidOnBlur) { + if (!active && changeOnBlur) { setInputValue(value); } }); diff --git a/src/NewPicker/PickerInput/Selector/RangeSelector.tsx b/src/NewPicker/PickerInput/Selector/RangeSelector.tsx index 673fb8544..37f82c732 100644 --- a/src/NewPicker/PickerInput/Selector/RangeSelector.tsx +++ b/src/NewPicker/PickerInput/Selector/RangeSelector.tsx @@ -82,7 +82,7 @@ function RangeSelector( // Valid format, maskFormat, - preserveInvalidOnBlur, + changeOnBlur, onInvalid, // Disabled @@ -196,7 +196,7 @@ function RangeSelector( // ============== Shared ============== format: maskFormat, validateFormat: (text) => !!validateFormat(text), - preserveInvalidOnBlur, + changeOnBlur, readOnly: inputReadOnly, @@ -334,9 +334,9 @@ function RangeSelector( onMouseDown?.(e); }} > - +
{separator}
- +
{showClear && } diff --git a/src/NewPicker/PickerInput/hooks/useRangeValue.ts b/src/NewPicker/PickerInput/hooks/useRangeValue.ts index fcd0b73b7..518ef1ed6 100644 --- a/src/NewPicker/PickerInput/hooks/useRangeValue.ts +++ b/src/NewPicker/PickerInput/hooks/useRangeValue.ts @@ -119,7 +119,6 @@ export default function useRangeValue( | 'order' | 'onCalendarChange' | 'onChange' - | 'preserveInvalidOnBlur' | 'picker' >, mergedValue: RangeValueType, @@ -146,9 +145,6 @@ export default function useRangeValue( onChange, - // Focus - // preserveInvalidOnBlur, - // Checker allowEmpty, order, diff --git a/src/NewPicker/index.tsx b/src/NewPicker/index.tsx index 853260408..262747160 100644 --- a/src/NewPicker/index.tsx +++ b/src/NewPicker/index.tsx @@ -3,7 +3,7 @@ * - Common * - [Break] Support special year format, all the year will follow the locale config. * - Blur all of field will trigger `onChange` if validate - * - Support `preserveInvalidOnBlur` to not to clean input if invalid + * - Support `changeOnBlur` to not to clean input if invalid * - `pickerValue` is now full controlled * - `defaultPickerValue` will take effect on every field active with popup opening. * - [Break] clear button return the event with `onClick` diff --git a/src/NewPicker/interface.tsx b/src/NewPicker/interface.tsx index a0b0ae456..36ec8360a 100644 --- a/src/NewPicker/interface.tsx +++ b/src/NewPicker/interface.tsx @@ -305,7 +305,7 @@ export interface SharedPickerProps extends Shared * When user input invalidate date, keep it in the input field. * This is only used for strong a11y requirement which do not want modify after blur. */ - preserveInvalidOnBlur?: boolean; + changeOnBlur?: boolean; // Icons suffixIcon?: React.ReactNode; @@ -425,7 +425,11 @@ export interface SelectorProps extends SharedHTMLAttrs { onChange: (date: DateType, index?: number) => void; onInputChange: VoidFunction; /** When user input invalidate date, keep it in the input field */ - preserveInvalidOnBlur?: boolean; + /** + * By default value in input field will be reset with previous valid value when blur. + * Set to `false` will keep invalid text in input field when blur. + */ + changeOnBlur?: boolean; // Open /** Open index */ diff --git a/tests/new-range.spec.tsx b/tests/new-range.spec.tsx index 2159c4ad3..8ce198e92 100644 --- a/tests/new-range.spec.tsx +++ b/tests/new-range.spec.tsx @@ -182,7 +182,6 @@ describe('NewPicker.Range', () => { selectCell(5); await waitFakeTimer(); - expect(document.querySelector('.rc-picker-header-view').textContent).toBe('2000年1月'); }); }); @@ -205,7 +204,7 @@ describe('NewPicker.Range', () => { expect(isOpen()).toBeTruthy(); }); - it('preserveInvalidOnBlur=false', () => { + it('changeOnBlur=true', () => { const { container } = render(); const firstInput = container.querySelector('input'); @@ -223,8 +222,8 @@ describe('NewPicker.Range', () => { expect(firstInput).toHaveValue(''); }); - it('preserveInvalidOnBlur=true', () => { - const { container } = render(); + it('changeOnBlur=false', () => { + const { container } = render(); const firstInput = container.querySelector('input'); openPicker(container);