mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-24 15:46:10 +08:00
59ad48476b
* chore: add boime lint * fix lint * use files ignore * revert change * ignore clarity.js * fix some errors * fix some errors * fix some errors * fix some errors * add yml file * Update clarity.js Signed-off-by: afc163 <afc163@gmail.com> * add npm run lint:biome * add npm run lint:biome * fix test case * fix ts errors * fix ts errors * fix lint and add .lintstagedrc * shorten prop name * chore: update package.json * update biome.json * chore: remove stylelint * chore: useOptionalChain * fix lint * biome format * prettier all code * prettier all code * fix site test --------- Signed-off-by: afc163 <afc163@gmail.com>
183 lines
5.4 KiB
TypeScript
Executable File
183 lines
5.4 KiB
TypeScript
Executable File
import * as React from 'react';
|
|
import classNames from 'classnames';
|
|
import type { BaseSelectRef } from 'rc-select';
|
|
import toArray from 'rc-util/lib/Children/toArray';
|
|
import omit from 'rc-util/lib/omit';
|
|
|
|
import { useZIndex } from '../_util/hooks/useZIndex';
|
|
import genPurePanel from '../_util/PurePanel';
|
|
import type { InputStatus } from '../_util/statusUtils';
|
|
import { devUseWarning } from '../_util/warning';
|
|
import type { ConfigConsumerProps } from '../config-provider';
|
|
import { ConfigContext } from '../config-provider';
|
|
import type {
|
|
BaseOptionType,
|
|
DefaultOptionType,
|
|
InternalSelectProps,
|
|
RefSelectProps,
|
|
SelectProps,
|
|
} from '../select';
|
|
import Select from '../select';
|
|
|
|
const { Option } = Select;
|
|
|
|
export interface DataSourceItemObject {
|
|
value: string;
|
|
text: string;
|
|
}
|
|
export type DataSourceItemType = DataSourceItemObject | React.ReactNode;
|
|
|
|
export interface AutoCompleteProps<
|
|
ValueType = any,
|
|
OptionType extends BaseOptionType | DefaultOptionType = DefaultOptionType,
|
|
> extends Omit<
|
|
InternalSelectProps<ValueType, OptionType>,
|
|
'loading' | 'mode' | 'optionLabelProp' | 'labelInValue'
|
|
> {
|
|
/** @deprecated Please use `options` instead */
|
|
dataSource?: DataSourceItemType[];
|
|
status?: InputStatus;
|
|
popupClassName?: string;
|
|
/** @deprecated Please use `popupClassName` instead */
|
|
dropdownClassName?: string;
|
|
/** @deprecated Please use `popupMatchSelectWidth` instead */
|
|
dropdownMatchSelectWidth?: boolean | number;
|
|
popupMatchSelectWidth?: boolean | number;
|
|
}
|
|
|
|
function isSelectOptionOrSelectOptGroup(child: any): boolean {
|
|
return child?.type && (child.type.isSelectOption || child.type.isSelectOptGroup);
|
|
}
|
|
|
|
const AutoComplete: React.ForwardRefRenderFunction<RefSelectProps, AutoCompleteProps> = (
|
|
props,
|
|
ref,
|
|
) => {
|
|
const {
|
|
prefixCls: customizePrefixCls,
|
|
className,
|
|
popupClassName,
|
|
dropdownClassName,
|
|
children,
|
|
dataSource,
|
|
} = props;
|
|
const childNodes: React.ReactElement[] = toArray(children);
|
|
|
|
// ============================= Input =============================
|
|
let customizeInput: React.ReactElement | undefined;
|
|
|
|
if (
|
|
childNodes.length === 1 &&
|
|
React.isValidElement(childNodes[0]) &&
|
|
!isSelectOptionOrSelectOptGroup(childNodes[0])
|
|
) {
|
|
[customizeInput] = childNodes;
|
|
}
|
|
|
|
const getInputElement = customizeInput ? (): React.ReactElement => customizeInput! : undefined;
|
|
|
|
// ============================ Options ============================
|
|
let optionChildren: React.ReactNode;
|
|
|
|
// [Legacy] convert `children` or `dataSource` into option children
|
|
if (childNodes.length && isSelectOptionOrSelectOptGroup(childNodes[0])) {
|
|
optionChildren = children;
|
|
} else {
|
|
optionChildren = dataSource
|
|
? dataSource.map((item) => {
|
|
if (React.isValidElement(item)) {
|
|
return item;
|
|
}
|
|
switch (typeof item) {
|
|
case 'string':
|
|
return (
|
|
<Option key={item} value={item}>
|
|
{item}
|
|
</Option>
|
|
);
|
|
case 'object': {
|
|
const { value: optionValue } = item as DataSourceItemObject;
|
|
return (
|
|
<Option key={optionValue} value={optionValue}>
|
|
{(item as DataSourceItemObject).text}
|
|
</Option>
|
|
);
|
|
}
|
|
default:
|
|
return undefined;
|
|
}
|
|
})
|
|
: [];
|
|
}
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
const warning = devUseWarning('AutoComplete');
|
|
|
|
warning.deprecated(!('dataSource' in props), 'dataSource', 'options');
|
|
|
|
warning(
|
|
!customizeInput || !('size' in props),
|
|
'usage',
|
|
'You need to control style self instead of setting `size` when using customize input.',
|
|
);
|
|
|
|
warning.deprecated(!dropdownClassName, 'dropdownClassName', 'popupClassName');
|
|
}
|
|
|
|
const { getPrefixCls } = React.useContext<ConfigConsumerProps>(ConfigContext);
|
|
|
|
const prefixCls = getPrefixCls('select', customizePrefixCls);
|
|
|
|
// ============================ zIndex ============================
|
|
const [zIndex] = useZIndex('SelectLike', props.dropdownStyle?.zIndex as number);
|
|
|
|
return (
|
|
<Select
|
|
ref={ref}
|
|
suffixIcon={null}
|
|
{...omit(props, ['dataSource', 'dropdownClassName'])}
|
|
prefixCls={prefixCls}
|
|
popupClassName={popupClassName || dropdownClassName}
|
|
dropdownStyle={{
|
|
...props.dropdownStyle,
|
|
zIndex,
|
|
}}
|
|
className={classNames(`${prefixCls}-auto-complete`, className)}
|
|
mode={Select.SECRET_COMBOBOX_MODE_DO_NOT_USE as SelectProps['mode']}
|
|
{...{
|
|
// Internal api
|
|
getInputElement,
|
|
}}
|
|
>
|
|
{optionChildren}
|
|
</Select>
|
|
);
|
|
};
|
|
|
|
const RefAutoComplete = React.forwardRef<RefSelectProps, AutoCompleteProps>(
|
|
AutoComplete,
|
|
) as unknown as (<
|
|
ValueType = any,
|
|
OptionType extends BaseOptionType | DefaultOptionType = DefaultOptionType,
|
|
>(
|
|
props: React.PropsWithChildren<AutoCompleteProps<ValueType, OptionType>> &
|
|
React.RefAttributes<BaseSelectRef>,
|
|
) => React.ReactElement) & {
|
|
displayName?: string;
|
|
Option: typeof Option;
|
|
_InternalPanelDoNotUseOrYouWillBeFired: typeof PurePanel;
|
|
};
|
|
|
|
// We don't care debug panel
|
|
/* istanbul ignore next */
|
|
const PurePanel = genPurePanel(RefAutoComplete);
|
|
|
|
RefAutoComplete.Option = Option;
|
|
RefAutoComplete._InternalPanelDoNotUseOrYouWillBeFired = PurePanel;
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
RefAutoComplete.displayName = 'AutoComplete';
|
|
}
|
|
|
|
export default RefAutoComplete;
|