Skip to content

Commit

Permalink
refactor: use changeOnBlur
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed Dec 5, 2023
1 parent 9d7fd22 commit 47ab3e6
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 40 deletions.
36 changes: 14 additions & 22 deletions docs/examples/debug.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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');
Expand All @@ -36,32 +34,22 @@ const sharedLocale = {
generateConfig: dayjsGenerateConfig,
};

function CellPicker(props: Partial<PickerPanelProps>) {
return (
<div>
<h5 style={{ margin: 0 }}>{props.picker || 'date'}</h5>
<PickerPanel {...sharedLocale} {...props} />
</div>
);
}

const MyTime = (props: any) => {
return (
<div>
2333
<TimePanel {...props} />
</div>
);
};
const MyInput = React.forwardRef<HTMLInputElement, React.InputHTMLAttributes<HTMLInputElement>>(
(props, ref) => {
console.log('>>>', props);
return <input {...props} ref={ref} />;
},
);
MyInput.displayName = 'MyInput';

export default () => {
const singleRef = React.useRef<PickerRef>(null);

const [value, setValue] = React.useState<Dayjs>(null);

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note documentation

Unused variable value.

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note documentation

Unused variable setValue.
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 (
Expand All @@ -73,6 +61,11 @@ export default () => {
<RangePicker
{...sharedLocale}
value={rangeValue}
components={{
input: MyInput,
}}
// showTime
showNow
placeholder={['Start', 'End']}
suffixIcon="🧶"
onFocus={(_, info) => {
Expand Down Expand Up @@ -104,7 +97,6 @@ export default () => {
start: 'inputStart',
end: 'inputEnd',
}}
renderExtraFooter={() => <input />}
/>
<br />

Expand Down
6 changes: 3 additions & 3 deletions src/NewPicker/PickerInput/Selector/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export interface InputProps extends Omit<React.InputHTMLAttributes<HTMLInputElem
* Like open the popup for interactive.
*/
onHelp: () => void;
preserveInvalidOnBlur?: boolean;
changeOnBlur?: boolean;
invalid?: boolean;
}

Expand All @@ -60,7 +60,7 @@ const Input = React.forwardRef<InputRef, InputProps>((props, ref) => {
onHelp,
onSubmit,
onKeyDown,
preserveInvalidOnBlur,
changeOnBlur = true,
invalid,
// Pass to input
...restProps
Expand Down Expand Up @@ -196,7 +196,7 @@ const Input = React.forwardRef<InputRef, InputProps>((props, ref) => {
// ======================== Active ========================
// Check if blur need reset input value
useLockEffect(active, () => {
if (!active && !preserveInvalidOnBlur) {
if (!active && changeOnBlur) {
setInputValue(value);
}
});
Expand Down
8 changes: 4 additions & 4 deletions src/NewPicker/PickerInput/Selector/RangeSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function RangeSelector<DateType = any>(
// Valid
format,
maskFormat,
preserveInvalidOnBlur,
changeOnBlur,
onInvalid,

// Disabled
Expand Down Expand Up @@ -196,7 +196,7 @@ function RangeSelector<DateType = any>(
// ============== Shared ==============
format: maskFormat,
validateFormat: (text) => !!validateFormat(text),
preserveInvalidOnBlur,
changeOnBlur,

readOnly: inputReadOnly,

Expand Down Expand Up @@ -334,9 +334,9 @@ function RangeSelector<DateType = any>(
onMouseDown?.(e);
}}
>
<Input ref={inputStartRef} {...getInputProps(0)} />
<Input ref={inputStartRef} {...getInputProps(0)} date-range="start" />
<div className={`${prefixCls}-range-separator`}>{separator}</div>
<Input ref={inputEndRef} {...getInputProps(1)} />
<Input ref={inputEndRef} {...getInputProps(1)} date-range="end" />
<div className={`${prefixCls}-active-bar`} style={activeBarStyle} />
<Icon type="suffix" icon={suffixIcon} />
{showClear && <ClearIcon type="clear" icon={clearIcon} onClear={onClear} />}
Expand Down
4 changes: 0 additions & 4 deletions src/NewPicker/PickerInput/hooks/useRangeValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ export default function useRangeValue<DateType extends object = any>(
| 'order'
| 'onCalendarChange'
| 'onChange'
| 'preserveInvalidOnBlur'
| 'picker'
>,
mergedValue: RangeValueType<DateType>,
Expand All @@ -146,9 +145,6 @@ export default function useRangeValue<DateType extends object = any>(

onChange,

// Focus
// preserveInvalidOnBlur,

// Checker
allowEmpty,
order,
Expand Down
2 changes: 1 addition & 1 deletion src/NewPicker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
8 changes: 6 additions & 2 deletions src/NewPicker/interface.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ export interface SharedPickerProps<DateType extends object = any> 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;
Expand Down Expand Up @@ -425,7 +425,11 @@ export interface SelectorProps<DateType = any> 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 */
Expand Down
7 changes: 3 additions & 4 deletions tests/new-range.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ describe('NewPicker.Range', () => {
selectCell(5);
await waitFakeTimer();


expect(document.querySelector('.rc-picker-header-view').textContent).toBe('2000年1月');
});
});
Expand All @@ -205,7 +204,7 @@ describe('NewPicker.Range', () => {
expect(isOpen()).toBeTruthy();
});

it('preserveInvalidOnBlur=false', () => {
it('changeOnBlur=true', () => {
const { container } = render(<DayRangePicker />);
const firstInput = container.querySelector<HTMLInputElement>('input');

Expand All @@ -223,8 +222,8 @@ describe('NewPicker.Range', () => {
expect(firstInput).toHaveValue('');
});

it('preserveInvalidOnBlur=true', () => {
const { container } = render(<DayRangePicker preserveInvalidOnBlur />);
it('changeOnBlur=false', () => {
const { container } = render(<DayRangePicker changeOnBlur={false} />);
const firstInput = container.querySelector<HTMLInputElement>('input');

openPicker(container);
Expand Down

0 comments on commit 47ab3e6

Please sign in to comment.