Skip to content

Commit a27ec3f

Browse files
committed
chore: Move class out to support ts define export
1 parent 9b91e47 commit a27ec3f

File tree

2 files changed

+30
-46
lines changed

2 files changed

+30
-46
lines changed

src/Select.tsx

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
* - `combobox` mode not support `optionLabelProp`
3030
*/
3131

32+
import React from 'react';
3233
import { OptionsType as SelectOptionsType } from './interface';
3334
import SelectOptionList from './OptionList';
3435
import Option from './Option';
@@ -42,24 +43,15 @@ import {
4243
flattenOptions,
4344
fillOptionsWithMissingValue,
4445
} from './utils/valueUtil';
45-
import generateSelector, { SelectProps } from './generate';
46+
import generateSelector, { SelectProps, RefSelectProps } from './generate';
4647
import { DefaultValueType } from './interface/generator';
4748
import warningProps from './utils/warningPropsUtil';
4849

49-
interface SelectStaticProps {
50-
Option: typeof Option;
51-
OptGroup: typeof OptGroup;
52-
}
53-
54-
const Select = generateSelector<SelectOptionsType, SelectStaticProps>({
50+
const RefSelect = generateSelector<SelectOptionsType>({
5551
prefixCls: 'rc-select',
5652
components: {
5753
optionList: SelectOptionList,
5854
},
59-
staticProps: {
60-
Option,
61-
OptGroup,
62-
},
6355
convertChildrenToData: convertSelectChildrenToData,
6456
flattenOptions,
6557
getLabeledValue: getSelectLabeledValue,
@@ -75,6 +67,28 @@ export type SelectProps<ValueType extends DefaultValueType = DefaultValueType> =
7567
ValueType
7668
>;
7769

78-
(Select as any).displayName = 'Select';
70+
/**
71+
* Typescript not support generic with function component,
72+
* we have to wrap an class component to handle this.
73+
*/
74+
class Select<VT> extends React.Component<SelectProps<SelectOptionsType, VT>> {
75+
static Option: typeof Option;
76+
77+
static OptGroup: typeof OptGroup;
78+
79+
selectRef = React.createRef<RefSelectProps>();
80+
81+
focus = () => {
82+
this.selectRef.current.focus();
83+
};
84+
85+
blur = () => {
86+
this.selectRef.current.blur();
87+
};
88+
89+
render() {
90+
return <RefSelect ref={this.selectRef} {...this.props} />;
91+
}
92+
}
7993

8094
export default Select;

src/generate.tsx

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export interface SelectProps<OptionsType extends object[], ValueType> extends Re
128128
choiceTransitionName?: string;
129129
}
130130

131-
export interface GenerateConfig<OptionsType extends object[], StaticProps> {
131+
export interface GenerateConfig<OptionsType extends object[]> {
132132
prefixCls: string;
133133
components: {
134134
optionList: React.ForwardRefExoticComponent<
@@ -138,7 +138,6 @@ export interface GenerateConfig<OptionsType extends object[], StaticProps> {
138138
React.RefAttributes<RefOptionListProps>
139139
>;
140140
};
141-
staticProps?: StaticProps;
142141
/** Convert jsx tree into `OptionsType` */
143142
convertChildrenToData: (children: React.ReactNode) => OptionsType;
144143
/** Flatten nest options into raw option list */
@@ -171,13 +170,11 @@ export default function generateSelector<
171170
label?: React.ReactNode;
172171
key?: Key;
173172
disabled?: boolean;
174-
}[],
175-
StaticProps
176-
>(config: GenerateConfig<OptionsType, StaticProps>) {
173+
}[]
174+
>(config: GenerateConfig<OptionsType>) {
177175
const {
178176
prefixCls: defaultPrefixCls,
179177
components: { optionList: OptionList },
180-
staticProps,
181178
convertChildrenToData,
182179
flattenOptions,
183180
getLabeledValue,
@@ -852,32 +849,5 @@ export default function generateSelector<
852849
type RefSelectFuncType = typeof RefSelectFunc;
853850
const RefSelect = ((React.forwardRef as unknown) as RefSelectFuncType)(Select);
854851

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-
875-
// Inject static props
876-
if (staticProps) {
877-
Object.keys(staticProps).forEach(prop => {
878-
ClassSelect[prop] = staticProps[prop];
879-
});
880-
}
881-
882-
return ClassSelect as (typeof ClassSelect & StaticProps);
852+
return RefSelect;
883853
}

0 commit comments

Comments
 (0)