Skip to content

Commit 9b91e47

Browse files
committed
chore: Wrap an class to support typescript generic
1 parent 63ae7e6 commit 9b91e47

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/Select.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,6 @@ export type SelectProps<ValueType extends DefaultValueType = DefaultValueType> =
7575
ValueType
7676
>;
7777

78-
Select.displayName = 'Select';
78+
(Select as any).displayName = 'Select';
7979

8080
export default Select;

src/generate.tsx

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -852,12 +852,32 @@ export default function generateSelector<
852852
type RefSelectFuncType = typeof RefSelectFunc;
853853
const RefSelect = ((React.forwardRef as unknown) as RefSelectFuncType)(Select);
854854

855+
/**
856+
* Typescript not support generic with function component,
857+
* we have to wrap an class component to handle this.
858+
*/
859+
class ClassSelect<VT> extends React.Component<SelectProps<OptionsType, VT>> {
860+
selectRef = React.createRef<RefSelectProps>();
861+
862+
focus = () => {
863+
this.selectRef.current.focus();
864+
};
865+
866+
blur = () => {
867+
this.selectRef.current.blur();
868+
};
869+
870+
render() {
871+
return <RefSelect ref={this.selectRef} {...this.props} />;
872+
}
873+
}
874+
855875
// Inject static props
856876
if (staticProps) {
857877
Object.keys(staticProps).forEach(prop => {
858-
RefSelect[prop] = staticProps[prop];
878+
ClassSelect[prop] = staticProps[prop];
859879
});
860880
}
861881

862-
return RefSelect as (typeof RefSelect & StaticProps);
882+
return ClassSelect as (typeof ClassSelect & StaticProps);
863883
}

0 commit comments

Comments
 (0)