Skip to content

Commit 7b187b8

Browse files
committed
clean up open
1 parent 78c9bc2 commit 7b187b8

File tree

3 files changed

+108
-59
lines changed

3 files changed

+108
-59
lines changed

examples/range.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export default () => {
4949
</h1>
5050

5151
<div style={{ display: 'flex', flexWrap: 'wrap' }}>
52-
{/* <div style={{ margin: '0 8px' }}>
52+
<div style={{ margin: '0 8px' }}>
5353
<h3>Basic</h3>
5454
<RangePicker<Moment>
5555
{...sharedProps}
@@ -65,7 +65,7 @@ export default () => {
6565
>
6666
Focus!
6767
</button>
68-
</div> */}
68+
</div>
6969
<div style={{ margin: '0 8px' }}>
7070
<h3>Basic</h3>
7171
<RangePicker<Moment> {...sharedProps} locale={zhCN} picker="year" />

src/Picker.tsx

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import useValueTexts from './hooks/useValueTexts';
3333
export interface PickerRefConfig {
3434
focus: () => void;
3535
blur: () => void;
36-
open: () => void;
3736
}
3837

3938
export interface PickerSharedProps<DateType> extends React.AriaAttributes {
@@ -301,9 +300,6 @@ function InnerPicker<DateType>(props: PickerProps<DateType>) {
301300
inputRef.current.blur();
302301
}
303302
},
304-
open: () => {
305-
triggerOpen(true);
306-
},
307303
};
308304
}
309305

@@ -421,12 +417,6 @@ class Picker<DateType> extends React.Component<PickerProps<DateType>> {
421417
}
422418
};
423419

424-
open = () => {
425-
if (this.pickerRef.current) {
426-
this.pickerRef.current.open();
427-
}
428-
};
429-
430420
render() {
431421
return (
432422
<InnerPicker<DateType>

src/RangePicker.tsx

Lines changed: 106 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
import * as React from 'react';
22
import classNames from 'classnames';
33
import { DisabledTimes, PanelMode, PickerMode } from './interface';
4-
import { PickerBaseProps, PickerDateProps, PickerTimeProps } from './Picker';
4+
import {
5+
PickerBaseProps,
6+
PickerDateProps,
7+
PickerTimeProps,
8+
PickerRefConfig,
9+
} from './Picker';
510
import { SharedTimeProps } from './panels/TimePanel';
611
import useMergedState from './hooks/useMergeState';
712
import PickerTrigger from './PickerTrigger';
813
import PickerPanel from './PickerPanel';
914
import usePickerInput from './hooks/usePickerInput';
1015
import getDataOrAriaProps, { toArray } from './utils/miscUtil';
1116
import { getDefaultFormat, getInputSize } from './utils/uiUtil';
12-
import { ContextOperationRefProps } from './PanelContext';
17+
import PanelContext, { ContextOperationRefProps } from './PanelContext';
1318
import { isEqual } from './utils/dateUtil';
1419
import useValueTexts from './hooks/useValueTexts';
1520
import useTextValueMapping from './hooks/useTextValueMapping';
@@ -142,7 +147,7 @@ interface MergedRangePickerProps<DateType>
142147
picker?: PickerMode;
143148
}
144149

145-
function RangePicker<DateType>(props: RangePickerProps<DateType>) {
150+
function InnerRangePicker<DateType>(props: RangePickerProps<DateType>) {
146151
const {
147152
prefixCls = 'rc-picker',
148153
style,
@@ -170,6 +175,7 @@ function RangePicker<DateType>(props: RangePickerProps<DateType>) {
170175
allowClear,
171176
suffixIcon,
172177
clearIcon,
178+
pickerRef,
173179
inputReadOnly,
174180
onChange,
175181
onOpenChange,
@@ -246,6 +252,7 @@ function RangePicker<DateType>(props: RangePickerProps<DateType>) {
246252
(!isEqual(generateConfig, getIndexValue(mergedValue, 0), startValue) ||
247253
!isEqual(generateConfig, getIndexValue(mergedValue, 1), endValue))
248254
) {
255+
console.warn('trigger change!!!!');
249256
onChange(values, [
250257
startValue
251258
? generateConfig.locale.format(
@@ -393,7 +400,23 @@ function RangePicker<DateType>(props: RangePickerProps<DateType>) {
393400
}, [mergedValue]);
394401

395402
// ============================ Private ============================
396-
// TODO: pickerRef
403+
if (pickerRef) {
404+
pickerRef.current = {
405+
focus: () => {
406+
if (startInputRef.current) {
407+
startInputRef.current.focus();
408+
}
409+
},
410+
blur: () => {
411+
if (startInputRef.current) {
412+
startInputRef.current.blur();
413+
}
414+
if (endInputRef.current) {
415+
endInputRef.current.blur();
416+
}
417+
},
418+
};
419+
}
397420

398421
// ============================= Panel =============================
399422
const panelProps = {
@@ -449,53 +472,89 @@ function RangePicker<DateType>(props: RangePickerProps<DateType>) {
449472
};
450473

451474
return (
452-
<PickerTrigger
453-
visible={mergedOpen}
454-
popupElement={panel}
455-
popupStyle={popupStyle}
456-
prefixCls={prefixCls}
457-
dropdownClassName={dropdownClassName}
458-
dropdownAlign={dropdownAlign}
459-
getPopupContainer={getPopupContainer}
460-
transitionName={transitionName}
475+
<PanelContext.Provider
476+
value={{
477+
operationRef,
478+
hideHeader: picker === 'time',
479+
panelRef: panelDivRef,
480+
}}
461481
>
462-
<div
463-
className={classNames(`${prefixCls}-range`, className, {
464-
[`${prefixCls}-range-disabled`]: disabled,
465-
[`${prefixCls}-range-focused`]: startFocused || endFocused,
466-
})}
467-
style={style}
468-
{...getDataOrAriaProps(props)}
482+
<PickerTrigger
483+
visible={mergedOpen}
484+
popupElement={panel}
485+
popupStyle={popupStyle}
486+
prefixCls={prefixCls}
487+
dropdownClassName={dropdownClassName}
488+
dropdownAlign={dropdownAlign}
489+
getPopupContainer={getPopupContainer}
490+
transitionName={transitionName}
469491
>
470-
<div className={`${prefixCls}-input`} ref={startInputDivRef}>
471-
<input
472-
readOnly={inputReadOnly || !startTyping}
473-
value={startText}
474-
onChange={triggerStartTextChange}
475-
autoFocus={autoFocus}
476-
placeholder={getIndexValue(placeholder, 0) || ''}
477-
ref={startInputRef}
478-
{...startInputProps}
479-
{...inputSharedProps}
480-
/>
492+
<div
493+
className={classNames(`${prefixCls}-range`, className, {
494+
[`${prefixCls}-range-disabled`]: disabled,
495+
[`${prefixCls}-range-focused`]: startFocused || endFocused,
496+
})}
497+
style={style}
498+
{...getDataOrAriaProps(props)}
499+
>
500+
<div className={`${prefixCls}-input`} ref={startInputDivRef}>
501+
<input
502+
readOnly={inputReadOnly || !startTyping}
503+
value={startText}
504+
onChange={triggerStartTextChange}
505+
autoFocus={autoFocus}
506+
placeholder={getIndexValue(placeholder, 0) || ''}
507+
ref={startInputRef}
508+
{...startInputProps}
509+
{...inputSharedProps}
510+
/>
511+
</div>
512+
{separator}
513+
<div className={`${prefixCls}-input`} ref={startInputDivRef}>
514+
<input
515+
readOnly={inputReadOnly || !startTyping}
516+
value={endText}
517+
onChange={triggerEndTextChange}
518+
placeholder={getIndexValue(placeholder, 1) || ''}
519+
ref={endInputRef}
520+
{...endInputProps}
521+
{...inputSharedProps}
522+
/>
523+
</div>
524+
{suffixNode}
525+
{clearNode}
481526
</div>
482-
{separator}
483-
<div className={`${prefixCls}-input`} ref={startInputDivRef}>
484-
<input
485-
readOnly={inputReadOnly || !startTyping}
486-
value={endText}
487-
onChange={triggerEndTextChange}
488-
placeholder={getIndexValue(placeholder, 1) || ''}
489-
ref={endInputRef}
490-
{...endInputProps}
491-
{...inputSharedProps}
492-
/>
493-
</div>
494-
{suffixNode}
495-
{clearNode}
496-
</div>
497-
</PickerTrigger>
527+
</PickerTrigger>
528+
</PanelContext.Provider>
498529
);
499530
}
500531

532+
// Wrap with class component to enable pass generic with instance method
533+
class RangePicker<DateType> extends React.Component<
534+
RangePickerProps<DateType>
535+
> {
536+
pickerRef = React.createRef<PickerRefConfig>();
537+
538+
focus = () => {
539+
if (this.pickerRef.current) {
540+
this.pickerRef.current.focus();
541+
}
542+
};
543+
544+
blur = () => {
545+
if (this.pickerRef.current) {
546+
this.pickerRef.current.blur();
547+
}
548+
};
549+
550+
render() {
551+
return (
552+
<InnerRangePicker<DateType>
553+
{...this.props}
554+
pickerRef={this.pickerRef as React.MutableRefObject<PickerRefConfig>}
555+
/>
556+
);
557+
}
558+
}
559+
501560
export default RangePicker;

0 commit comments

Comments
 (0)