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 all 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
21 changes: 9 additions & 12 deletions components/auto-complete/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@

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 RcSelect from 'rc-select';
import Select, { InternalSelectProps, OptionType } from '../select';
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
import devWarning from '../_util/devWarning';
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,14 +35,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<RcSelect<any>, AutoCompleteProps> = (
props,
ref,
) => {
const { prefixCls: customizePrefixCls, className, children, dataSource } = props;
const childNodes: React.ReactElement[] = toArray(children);

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

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

// ============================= Input =============================
let customizeInput: React.ReactElement | undefined;

Expand Down Expand Up @@ -113,23 +110,23 @@ const AutoComplete: React.ForwardRefRenderFunction<Select, AutoCompleteProps> =
const prefixCls = getPrefixCls('select', customizePrefixCls);

return (
<InternalSelect
ref={selectRef as any}
<Select
ref={ref}
{...omit(props, ['dataSource'])}
prefixCls={prefixCls}
className={classNames(`${prefixCls}-auto-complete`, className)}
mode={Select.SECRET_COMBOBOX_MODE_DO_NOT_USE as any}
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