Skip to content

Commit

Permalink
fix(Picker): add ref to PickerComponentProps (#2261)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvelSQ committed Dec 30, 2021
1 parent a5a05fb commit 6e3e6c5
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
10 changes: 7 additions & 3 deletions src/CheckPicker/CheckPicker.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useRef, useState, useCallback } from 'react';
import React, { useRef, useState, useCallback, Ref } from 'react';
import PropTypes from 'prop-types';
import clone from 'lodash/clone';
import isUndefined from 'lodash/isUndefined';
Expand Down Expand Up @@ -58,12 +58,16 @@ export interface CheckPickerProps<T>
const emptyArray = [];

export interface CheckPickerComponent {
<T>(props: CheckPickerProps<T>): JSX.Element | null;
<T>(
props: CheckPickerProps<T> & {
ref?: Ref<PickerInstance>;
}
): JSX.Element | null;
displayName?: string;
propTypes?: React.WeakValidationMap<CheckPickerProps<any>>;
}

const CheckPicker: CheckPickerComponent = React.forwardRef(
const CheckPicker = React.forwardRef(
<T extends number | string>(props: CheckPickerProps<T>, ref: React.Ref<PickerInstance>) => {
const {
as: Component = 'div',
Expand Down
5 changes: 5 additions & 0 deletions src/CheckPicker/test/CheckPicker.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import { expectType } from 'ts-expect';
import { PickerInstance } from '../../Picker';
import CheckPicker from '../CheckPicker';

// Infer value and onChange types from data
Expand Down Expand Up @@ -30,3 +31,7 @@ const stringValuedData = [{ label: 'One', value: 'One' }];
expectType<string[]>(newValue);
}}
/>;

const pickerRef = React.createRef<PickerInstance>();

<CheckPicker ref={pickerRef} data={[]} />;
8 changes: 6 additions & 2 deletions src/SelectPicker/SelectPicker.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useRef, useState, useCallback, useEffect } from 'react';
import React, { useRef, useState, useCallback, useEffect, Ref } from 'react';
import PropTypes from 'prop-types';
import pick from 'lodash/pick';
import isUndefined from 'lodash/isUndefined';
Expand Down Expand Up @@ -105,7 +105,11 @@ export interface SelectPickerProps<T>
const emptyArray = [];

export interface SelectPickerComponent {
<T>(props: SelectPickerProps<T>): JSX.Element | null;
<T>(
props: SelectPickerProps<T> & {
ref?: Ref<PickerInstance>;
}
): JSX.Element | null;
displayName?: string;
propTypes?: React.WeakValidationMap<SelectPickerProps<any>>;
}
Expand Down
5 changes: 5 additions & 0 deletions src/SelectPicker/test/SelectPicker.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import { expectType } from 'ts-expect';
import { PickerInstance } from '../../Picker';
import SelectPicker from '../SelectPicker';

// Infer value and onChange types from data
Expand All @@ -26,3 +27,7 @@ const stringValuedData = [{ label: 'One', value: 'One' }];
expectType<string>(newValue);
}}
/>;

const pickerRef = React.createRef<PickerInstance>();

<SelectPicker ref={pickerRef} data={[]} />;

0 comments on commit 6e3e6c5

Please sign in to comment.