Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(select): rewrite with hook #27318

Merged
merged 7 commits into from
Oct 24, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 6 additions & 9 deletions components/auto-complete/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import * as React from 'react';
import toArray from 'rc-util/lib/Children/toArray';
import { SelectProps as RcSelectProps } from 'rc-select';
import classNames from 'classnames';
import omit from 'omit.js';
import Select, { InternalSelectProps, OptionType } from '../select';
Expand All @@ -17,8 +16,6 @@ import { isValidElement } from '../_util/reactNode';

const { Option } = Select;

const InternalSelect = Select as React.ComponentClass<RcSelectProps>;

export interface DataSourceItemObject {
value: string;
text: string;
Expand All @@ -37,13 +34,13 @@ function isSelectOptionOrSelectOptGroup(child: any): Boolean {
return child && child.type && (child.type.isSelectOption || child.type.isSelectOptGroup);
}

const AutoComplete: React.ForwardRefRenderFunction<Select, AutoCompleteProps> = (props, ref) => {
const AutoComplete: React.ForwardRefRenderFunction<unknown, AutoCompleteProps> = (props, ref) => {
hengkx marked this conversation as resolved.
Show resolved Hide resolved
const { prefixCls: customizePrefixCls, className, children, dataSource } = props;
const childNodes: React.ReactElement[] = toArray(children);

const selectRef = React.useRef<Select>();
const selectRef = React.useRef();

React.useImperativeHandle<Select, Select>(ref, () => selectRef.current!);
React.useImperativeHandle(ref, () => selectRef.current!);

// ============================= Input =============================
let customizeInput: React.ReactElement | undefined;
Expand Down Expand Up @@ -113,7 +110,7 @@ const AutoComplete: React.ForwardRefRenderFunction<Select, AutoCompleteProps> =
const prefixCls = getPrefixCls('select', customizePrefixCls);

return (
<InternalSelect
<Select
ref={selectRef as any}
{...omit(props, ['dataSource'])}
prefixCls={prefixCls}
Expand All @@ -122,14 +119,14 @@ const AutoComplete: React.ForwardRefRenderFunction<Select, AutoCompleteProps> =
getInputElement={getInputElement}
>
{optionChildren}
</InternalSelect>
</Select>
);
}}
</ConfigConsumer>
);
};

const RefAutoComplete = React.forwardRef<Select, AutoCompleteProps>(AutoComplete);
const RefAutoComplete = React.forwardRef<unknown, AutoCompleteProps>(AutoComplete);

type RefAutoCompleteWithOption = typeof RefAutoComplete & {
Option: OptionType;
Expand Down