types: export DatePickRef and RangePickerRef (close #33417) (#33901)

This commit is contained in:
Amour1688 2022-01-31 12:04:47 +08:00 committed by GitHub
parent ebc0c1fbc5
commit 1176454437
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 0 deletions

View File

@ -1,5 +1,7 @@
import * as React from 'react';
import { Moment } from 'moment';
import DatePicker from '..';
import { DatePickRef, RangePickerRef } from '../generatePicker/interface';
describe('DatePicker.typescript', () => {
it('DatePicker ref methods', () => {
@ -13,6 +15,23 @@ describe('DatePicker.typescript', () => {
);
expect(datePicker).toBeTruthy();
});
// https://github.com/ant-design/ant-design/issues/33417
it('DatePicker ref methods with forwardRef', () => {
const MyDatePicker = React.forwardRef((props, ref: DatePickRef<Moment>) => (
<DatePicker {...props} ref={ref} />
));
const datePicker = (
<MyDatePicker
ref={picker => {
picker?.focus();
picker?.blur();
}}
/>
);
expect(datePicker).toBeTruthy();
});
it('RangePicker ref methods', () => {
const rangePicker = (
<DatePicker.RangePicker
@ -24,4 +43,19 @@ describe('DatePicker.typescript', () => {
);
expect(rangePicker).toBeTruthy();
});
it('RangePicker ref methods with forwardRef', () => {
const MyRangePicker = React.forwardRef((props, ref: RangePickerRef<Moment>) => (
<DatePicker.RangePicker {...props} ref={ref} />
));
const datePicker = (
<MyRangePicker
ref={picker => {
picker?.focus();
picker?.blur();
}}
/>
);
expect(datePicker).toBeTruthy();
});
});

View File

@ -0,0 +1,18 @@
import type { ComponentClass, ForwardedRef, Component } from 'react';
import { PickerProps, RangePickerProps } from '.';
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;
}
export type PickerRef<P> = ForwardedRef<Component<P> & CommonPickerMethods>;
export type DatePickRef<DateType> = PickerRef<PickerProps<DateType>>;
export type RangePickerRef<DateType> = PickerRef<RangePickerProps<DateType>>;