fix(date-picker): DatePicker ref typing (#31993)

This commit is contained in:
acfasj 2021-09-03 13:24:48 +08:00 committed by GitHub
parent a7a1a806b8
commit 999660c574
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 12 deletions

View File

@ -0,0 +1,27 @@
import * as React from 'react';
import DatePicker from '..';
describe('DatePicker.typescript', () => {
it('DatePicker ref methods', () => {
const datePicker = (
<DatePicker
ref={picker => {
picker?.focus();
picker?.blur();
}}
/>
);
expect(datePicker).toBeTruthy();
});
it('RangePicker ref methods', () => {
const rangePicker = (
<DatePicker.RangePicker
ref={picker => {
picker?.focus();
picker?.blur();
}}
/>
);
expect(rangePicker).toBeTruthy();
});
});

View File

@ -12,10 +12,11 @@ import SizeContext from '../../config-provider/SizeContext';
import LocaleReceiver from '../../locale-provider/LocaleReceiver';
import { getRangePlaceholder } from '../util';
import { RangePickerProps, PickerLocale, getTimeProps, Components } from '.';
import { PickerComponentClass } from './interface';
export default function generateRangePicker<DateType>(
generateConfig: GenerateConfig<DateType>,
): React.ComponentClass<RangePickerProps<DateType>> {
): PickerComponentClass<RangePickerProps<DateType>> {
class RangePicker extends React.Component<RangePickerProps<DateType>> {
static contextType = ConfigContext;

View File

@ -20,6 +20,7 @@ import {
getTimeProps,
Components,
} from '.';
import { PickerComponentClass } from './interface';
export default function generatePicker<DateType>(generateConfig: GenerateConfig<DateType>) {
type DatePickerProps = PickerProps<DateType>;
@ -147,7 +148,7 @@ export default function generatePicker<DateType>(generateConfig: GenerateConfig<
Picker.displayName = displayName;
}
return Picker as React.ComponentClass<InnerPickerProps>;
return Picker as PickerComponentClass<InnerPickerProps>;
}
const DatePicker = getPicker<DatePickerProps>();

View File

@ -1,4 +1,3 @@
import * as React from 'react';
import { GenerateConfig } from 'rc-picker/lib/generate/index';
import {
PickerBaseProps as RCPickerBaseProps,
@ -128,14 +127,8 @@ export type RangePickerProps<DateType> =
function generatePicker<DateType>(generateConfig: GenerateConfig<DateType>) {
// =========================== Picker ===========================
const {
DatePicker,
WeekPicker,
MonthPicker,
YearPicker,
TimePicker,
QuarterPicker,
} = generateSinglePicker(generateConfig);
const { DatePicker, WeekPicker, MonthPicker, YearPicker, TimePicker, QuarterPicker } =
generateSinglePicker(generateConfig);
// ======================== Range Picker ========================
const RangePicker = generateRangePicker(generateConfig);
@ -145,7 +138,7 @@ function generatePicker<DateType>(generateConfig: GenerateConfig<DateType>) {
WeekPicker: typeof WeekPicker;
MonthPicker: typeof MonthPicker;
YearPicker: typeof YearPicker;
RangePicker: React.ComponentClass<RangePickerProps<DateType>>;
RangePicker: typeof RangePicker;
TimePicker: typeof TimePicker;
QuarterPicker: typeof QuarterPicker;
};

View File

@ -0,0 +1,11 @@
import { ComponentClass } from 'react';
export interface CommonPickerMethods {
focus: () => void;
blur: () => void;
}
export interface PickerComponentClass<P = {}, S = unknown> extends ComponentClass<P, S> {
new (...args: ConstructorParameters<ComponentClass<P, S>>): InstanceType<ComponentClass<P, S>> &
CommonPickerMethods;
}