Skip to content

Commit 42f05c8

Browse files
authored
feat: support changeOnBlur (#629)
* add changeOnBlur * support Picker changeOnBlur * support RangePicker changeOnBlur * chore: test of it * chore: tmp refactor * chore: adjust open logic * test: update test case * fix: range enter switch * chore: on confirm to switch * fix: close directly if no need next one * fix: open record * refactor: with firstTimeOpen * fix: dbl click should not close * test: fix test case * test: fix test case * test: fix test case * docs: update back * chore: clean up * chore: clean up
1 parent 3788753 commit 42f05c8

File tree

11 files changed

+388
-161
lines changed

11 files changed

+388
-161
lines changed

docs/examples/basic.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export default () => {
4747
<div style={{ display: 'flex', flexWrap: 'wrap' }}>
4848
<div style={{ margin: '0 8px' }}>
4949
<h3>Basic</h3>
50-
<Picker<Moment> {...sharedProps} locale={zhCN} />
50+
<Picker<Moment> {...sharedProps} locale={zhCN} suffixIcon="SUFFIX" />
5151
<Picker<Moment> {...sharedProps} locale={enUS} />
5252
</div>
5353
<div style={{ margin: '0 8px' }}>
@@ -79,6 +79,7 @@ export default () => {
7979
}
8080
return {};
8181
}}
82+
changeOnBlur
8283
/>
8384
</div>
8485
<div style={{ margin: '0 8px' }}>

docs/examples/range.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import React from 'react';
21
import type { Moment } from 'moment';
32
import moment from 'moment';
4-
import RangePicker from '../../src/RangePicker';
3+
import React from 'react';
4+
import '../../assets/index.less';
55
import momentGenerateConfig from '../../src/generate/moment';
66
import zhCN from '../../src/locale/zh_CN';
7-
import '../../assets/index.less';
7+
import RangePicker from '../../src/RangePicker';
88
import './common.less';
99

1010
const defaultStartValue = moment('2019-09-03 05:02:03');
@@ -70,13 +70,18 @@ export default () => {
7070
ref={rangePickerRef}
7171
showTime
7272
style={{ width: 580 }}
73-
cellRender={(current, info) => <div title={info.type} style={{background: 'green'}}>{typeof current === "number" ? current : current.get("date")}</div>}
73+
cellRender={(current, info) => (
74+
<div title={info.type} style={{ background: 'green' }}>
75+
{typeof current === 'number' ? current : current.get('date')}
76+
</div>
77+
)}
7478
ranges={{
7579
ranges: [moment(), moment().add(10, 'day')],
7680
}}
7781
onOk={(dates) => {
7882
console.log('OK!!!', dates);
7983
}}
84+
changeOnBlur
8085
/>
8186
<RangePicker<Moment>
8287
{...sharedProps}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"@babel/runtime": "^7.10.1",
4141
"@rc-component/trigger": "^1.5.0",
4242
"classnames": "^2.2.1",
43-
"rc-util": "^5.27.0"
43+
"rc-util": "^5.30.0"
4444
},
4545
"engines": {
4646
"node": ">=8.x"

src/Picker.tsx

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
* Tips: Should add faq about `datetime` mode with `defaultValue`
1212
*/
1313

14-
import classNames from 'classnames';
1514
import type { AlignType } from '@rc-component/trigger/lib/interface';
15+
import classNames from 'classnames';
1616
import useMergedState from 'rc-util/lib/hooks/useMergedState';
1717
import warning from 'rc-util/lib/warning';
1818
import * as React from 'react';
@@ -87,6 +87,12 @@ export type PickerSharedProps<DateType> = {
8787
onContextMenu?: React.MouseEventHandler<HTMLDivElement>;
8888
onKeyDown?: (event: React.KeyboardEvent<HTMLInputElement>, preventDefault: () => void) => void;
8989

90+
/**
91+
* Trigger `onChange` event when blur.
92+
* If you don't want to user click `confirm` to trigger change, can use this.
93+
*/
94+
changeOnBlur?: boolean;
95+
9096
// Internal
9197
/** @private Internal usage, do not use in production mode!!! */
9298
pickerRef?: React.MutableRefObject<PickerRefConfig>;
@@ -182,6 +188,7 @@ function InnerPicker<DateType>(props: PickerProps<DateType>) {
182188
direction,
183189
autoComplete = 'off',
184190
inputRender,
191+
changeOnBlur,
185192
} = props as MergedPickerProps<DateType>;
186193

187194
const inputRef = React.useRef<HTMLInputElement>(null);
@@ -301,6 +308,14 @@ function InnerPicker<DateType>(props: PickerProps<DateType>) {
301308
};
302309

303310
// ============================= Input =============================
311+
const onInternalBlur: React.FocusEventHandler<HTMLInputElement> = (e) => {
312+
if (changeOnBlur) {
313+
triggerChange(selectedValue);
314+
}
315+
316+
onBlur?.(e);
317+
};
318+
304319
const [inputProps, { focused, typing }] = usePickerInput({
305320
blurToCancel: needConfirmButton,
306321
open: mergedOpen,
@@ -336,7 +351,8 @@ function InnerPicker<DateType>(props: PickerProps<DateType>) {
336351
onKeyDown?.(e, preventDefault);
337352
},
338353
onFocus,
339-
onBlur,
354+
onBlur: onInternalBlur,
355+
changeOnBlur,
340356
});
341357

342358
// ============================= Sync ==============================
@@ -450,7 +466,17 @@ function InnerPicker<DateType>(props: PickerProps<DateType>) {
450466

451467
let suffixNode: React.ReactNode;
452468
if (suffixIcon) {
453-
suffixNode = <span className={`${prefixCls}-suffix`}>{suffixIcon}</span>;
469+
suffixNode = (
470+
<span
471+
className={`${prefixCls}-suffix`}
472+
onMouseDown={(e) => {
473+
// Not lost focus
474+
e.preventDefault();
475+
}}
476+
>
477+
{suffixIcon}
478+
</span>
479+
);
454480
}
455481

456482
let clearNode: React.ReactNode;

0 commit comments

Comments
 (0)