ant-design/components/tree-select/index.tsx
Yuki Zhang c3e51506cc
feat: add Space.Compact (#37652)
* feat: add Space.Compact

* feat: update input style

* feat: add CompactItem for context memoize

* feat: add demo

* feat: update button style

* feat: update input style

* feat: 提取 getCompactClassNames 公用方法

* feat: update button style

* feat: update picker

* feat: add block prop for Space.Compact

* feat: use Space.Compact for Input#addonBefore/After

* feat: update addon

* refactor: compact

* feat: update

* feat: update

* feat: migrate styles to compact for new Input/Input.Search

* feat: organize input demo

* feat: add more button compact demo

* feat: resolve select border collapse

* feat: fix input addon select style

* feat: add input addon demo in Space

* feat: add  useCompactItemContext hook

* feat: update compact-item style

* feat: rollback input#addon implements

* feat: rollback input#addon and input.search style

* feat: select border collapse

* feat: add Space.Compact demo

* feat: Space.Compact vertical for Button

* refactor: useCompactItemContext

* feat: update Button vertical demo

* feat: rtl for compact mixin

* feat: rtl for compact button

* feat: input rtl

* feat: add docs for Space.Compact

* test: add some tests for Space.Compact

* test: add tests

* feat: select style

* feat: handle select focus style

* feat: add useCompactItemContext for Picker and Cascader

* style: add compact-item style for cascader

* feat: support nested Space.Compact

* style: Input.Search in Space.Compact

* chore: clean

* feat: add useCompactItemContext for TreeSelect

* fix: lint issues

* fix: style lint

* docs: update demos for Space.Compact

* docs: update demo

* fix: add deps-lint-skip for space

* fix: add deps-lint-skip for space

* fix: handle edge case for useCompactItemContext

* test: add search test case

* chore: + bundlesize

* style: merge button compact style into one file

* style: improve style for nested compact

* fix: stylelint

* docs: update Space.Compact docs

* test: update demo snapshot

* test: update input debug test snapshot

* docs: update doccs for Space.Compact

* test: improve test cases for Compact

* docs: clean demos for Compact

* refactor: improve Space.Compact

* fix: add useCompactItemContext for Input.Search

* style: improve compact border-radius implementation

* refactor: use context to make nested compact works

* fix: input-number focused style

* refactor: remove useless style

* refactor: improve style for vertical compact

* test: update snapshot

* test: update snapshot for input

* refactor: improve compact-item-border-radius

* fix: search group in RTL

* style: improve style for button compact

* style: use after

* fix: stylelint

* chore: update snapshot

* style: improve button compact

* refactor: improve btn-icon-only style in compact
2022-10-18 16:23:10 +08:00

266 lines
8.3 KiB
TypeScript

import classNames from 'classnames';
import type { BaseSelectRef } from 'rc-select';
import type { TreeSelectProps as RcTreeSelectProps } from 'rc-tree-select';
import RcTreeSelect, { SHOW_ALL, SHOW_CHILD, SHOW_PARENT, TreeNode } from 'rc-tree-select';
import type { BaseOptionType, DefaultOptionType } from 'rc-tree-select/lib/TreeSelect';
import omit from 'rc-util/lib/omit';
import * as React from 'react';
import { useContext } from 'react';
import { ConfigContext } from '../config-provider';
import defaultRenderEmpty from '../config-provider/defaultRenderEmpty';
import DisabledContext from '../config-provider/DisabledContext';
import type { SizeType } from '../config-provider/SizeContext';
import SizeContext from '../config-provider/SizeContext';
import { FormItemInputContext } from '../form/context';
import getIcons from '../select/utils/iconUtil';
import type { AntTreeNodeProps, TreeProps } from '../tree';
import type { SwitcherIcon } from '../tree/Tree';
import renderSwitcherIcon from '../tree/utils/iconUtil';
import type { SelectCommonPlacement } from '../_util/motion';
import { getTransitionDirection, getTransitionName } from '../_util/motion';
import type { InputStatus } from '../_util/statusUtils';
import { getMergedStatus, getStatusClassNames } from '../_util/statusUtils';
import { useCompactItemContext } from '../space/Compact';
import warning from '../_util/warning';
type RawValue = string | number;
export interface LabeledValue {
key?: string;
value: RawValue;
label: React.ReactNode;
}
export type SelectValue = RawValue | RawValue[] | LabeledValue | LabeledValue[];
export interface TreeSelectProps<
ValueType = any,
OptionType extends BaseOptionType | DefaultOptionType = DefaultOptionType,
> extends Omit<
RcTreeSelectProps<ValueType, OptionType>,
| 'showTreeIcon'
| 'treeMotion'
| 'inputIcon'
| 'mode'
| 'getInputElement'
| 'backfill'
| 'treeLine'
| 'switcherIcon'
> {
suffixIcon?: React.ReactNode;
size?: SizeType;
disabled?: boolean;
placement?: SelectCommonPlacement;
/**
* @deprecated `dropdownClassName` is deprecated which will be removed in next major
* version.Please use `popupClassName` instead.
*/
dropdownClassName?: string;
popupClassName?: string;
bordered?: boolean;
treeLine?: TreeProps['showLine'];
status?: InputStatus;
switcherIcon?: SwitcherIcon | RcTreeSelectProps<ValueType, OptionType>['switcherIcon'];
}
const InternalTreeSelect = <OptionType extends BaseOptionType | DefaultOptionType = BaseOptionType>(
{
prefixCls: customizePrefixCls,
size: customizeSize,
disabled: customDisabled,
bordered = true,
className,
treeCheckable,
multiple,
listHeight = 256,
listItemHeight = 26,
placement,
notFoundContent,
switcherIcon,
treeLine,
getPopupContainer,
dropdownClassName,
popupClassName,
treeIcon = false,
transitionName,
choiceTransitionName = '',
status: customStatus,
showArrow,
treeExpandAction,
...props
}: TreeSelectProps<OptionType>,
ref: React.Ref<BaseSelectRef>,
) => {
const {
getPopupContainer: getContextPopupContainer,
getPrefixCls,
renderEmpty,
direction,
virtual,
dropdownMatchSelectWidth,
} = React.useContext(ConfigContext);
const size = React.useContext(SizeContext);
warning(
multiple !== false || !treeCheckable,
'TreeSelect',
'`multiple` will always be `true` when `treeCheckable` is true',
);
warning(
!dropdownClassName,
'TreeSelect',
'`dropdownClassName` is deprecated which will be removed in next major version. Please use `popupClassName` instead.',
);
const prefixCls = getPrefixCls('select', customizePrefixCls);
const treePrefixCls = getPrefixCls('select-tree', customizePrefixCls);
const treeSelectPrefixCls = getPrefixCls('tree-select', customizePrefixCls);
const { compactSize, compactItemClassnames } = useCompactItemContext(prefixCls, direction);
const mergedDropdownClassName = classNames(
popupClassName || dropdownClassName,
`${treeSelectPrefixCls}-dropdown`,
{
[`${treeSelectPrefixCls}-dropdown-rtl`]: direction === 'rtl',
},
);
const isMultiple = !!(treeCheckable || multiple);
const mergedShowArrow = showArrow !== undefined ? showArrow : props.loading || !isMultiple;
// ===================== Form =====================
const {
status: contextStatus,
hasFeedback,
isFormItemInput,
feedbackIcon,
} = useContext(FormItemInputContext);
const mergedStatus = getMergedStatus(contextStatus, customStatus);
// ===================== Icons =====================
const { suffixIcon, removeIcon, clearIcon } = getIcons({
...props,
multiple: isMultiple,
showArrow: mergedShowArrow,
hasFeedback,
feedbackIcon,
prefixCls,
});
// ===================== Empty =====================
let mergedNotFound: React.ReactNode;
if (notFoundContent !== undefined) {
mergedNotFound = notFoundContent;
} else {
mergedNotFound = (renderEmpty || defaultRenderEmpty)('Select');
}
// ==================== Render =====================
const selectProps = omit(props as typeof props & { itemIcon: any; switcherIcon: any }, [
'suffixIcon',
'itemIcon',
'removeIcon',
'clearIcon',
'switcherIcon',
]);
// ===================== Placement =====================
const getPlacement = () => {
if (placement !== undefined) {
return placement;
}
return direction === 'rtl'
? ('bottomRight' as SelectCommonPlacement)
: ('bottomLeft' as SelectCommonPlacement);
};
const mergedSize = compactSize || customizeSize || size;
// ===================== Disabled =====================
const disabled = React.useContext(DisabledContext);
const mergedDisabled = customDisabled ?? disabled;
const mergedClassName = classNames(
!customizePrefixCls && treeSelectPrefixCls,
{
[`${prefixCls}-lg`]: mergedSize === 'large',
[`${prefixCls}-sm`]: mergedSize === 'small',
[`${prefixCls}-rtl`]: direction === 'rtl',
[`${prefixCls}-borderless`]: !bordered,
[`${prefixCls}-in-form-item`]: isFormItemInput,
},
getStatusClassNames(prefixCls, mergedStatus, hasFeedback),
compactItemClassnames,
className,
);
const rootPrefixCls = getPrefixCls();
return (
<RcTreeSelect
virtual={virtual}
dropdownMatchSelectWidth={dropdownMatchSelectWidth}
disabled={mergedDisabled}
{...selectProps}
ref={ref as any}
prefixCls={prefixCls}
className={mergedClassName}
listHeight={listHeight}
listItemHeight={listItemHeight}
treeCheckable={
treeCheckable ? <span className={`${prefixCls}-tree-checkbox-inner`} /> : treeCheckable
}
treeLine={!!treeLine}
inputIcon={suffixIcon}
multiple={multiple}
placement={getPlacement()}
removeIcon={removeIcon}
clearIcon={clearIcon}
switcherIcon={(nodeProps: AntTreeNodeProps) =>
renderSwitcherIcon(treePrefixCls, switcherIcon, treeLine, nodeProps)
}
showTreeIcon={treeIcon as any}
notFoundContent={mergedNotFound}
getPopupContainer={getPopupContainer || getContextPopupContainer}
treeMotion={null}
dropdownClassName={mergedDropdownClassName}
choiceTransitionName={getTransitionName(rootPrefixCls, '', choiceTransitionName)}
transitionName={getTransitionName(
rootPrefixCls,
getTransitionDirection(placement),
transitionName,
)}
showArrow={hasFeedback || showArrow}
treeExpandAction={treeExpandAction}
/>
);
};
const TreeSelectRef = React.forwardRef(InternalTreeSelect) as <
ValueType = any,
OptionType extends BaseOptionType | DefaultOptionType = DefaultOptionType,
>(
props: React.PropsWithChildren<TreeSelectProps<ValueType, OptionType>> & {
ref?: React.Ref<BaseSelectRef>;
},
) => React.ReactElement;
type InternalTreeSelectType = typeof TreeSelectRef;
interface TreeSelectInterface extends InternalTreeSelectType {
TreeNode: typeof TreeNode;
SHOW_ALL: typeof SHOW_ALL;
SHOW_PARENT: typeof SHOW_PARENT;
SHOW_CHILD: typeof SHOW_CHILD;
}
const TreeSelect = TreeSelectRef as TreeSelectInterface;
TreeSelect.TreeNode = TreeNode;
TreeSelect.SHOW_ALL = SHOW_ALL;
TreeSelect.SHOW_PARENT = SHOW_PARENT;
TreeSelect.SHOW_CHILD = SHOW_CHILD;
export { TreeNode };
export default TreeSelect;