antd
Version:
An enterprise-class UI design language and React components implementation
85 lines (84 loc) • 3.48 kB
JavaScript
"use client";
import * as React from 'react';
import CheckOutlined from "@ant-design/icons/es/icons/CheckOutlined";
import CloseCircleFilled from "@ant-design/icons/es/icons/CloseCircleFilled";
import CloseOutlined from "@ant-design/icons/es/icons/CloseOutlined";
import DownOutlined from "@ant-design/icons/es/icons/DownOutlined";
import LoadingOutlined from "@ant-design/icons/es/icons/LoadingOutlined";
import SearchOutlined from "@ant-design/icons/es/icons/SearchOutlined";
import { isReactRenderable } from '@rc-component/util';
import fallbackProp from '../_util/fallbackProp';
import { isFunction } from '../_util/is';
import { devUseWarning } from '../_util/warning';
export default function useIcons({
suffix,
contextSuffix,
clearIcon,
contextClearIcon,
menuItemSelectedIcon,
contextMenuItemSelectedIcon,
removeIcon,
contextRemoveIcon,
loading,
loadingIcon,
contextLoadingIcon,
searchIcon,
contextSearchIcon,
multiple,
hasFeedback,
showSuffix,
feedbackIcon,
showArrow,
componentName
}) {
if (process.env.NODE_ENV !== 'production') {
const warning = devUseWarning(componentName);
warning.deprecated(!clearIcon, 'clearIcon', 'allowClear={{ clearIcon: React.ReactNode }}');
}
return React.useMemo(() => {
// Clear Icon
const mergedClearIcon = fallbackProp(clearIcon, contextClearIcon, /*#__PURE__*/React.createElement(CloseCircleFilled, null));
// Validation Feedback Icon
const getSuffixNode = suffixNode => {
if (!isReactRenderable(suffixNode) && !hasFeedback && !showArrow) {
return null;
}
return /*#__PURE__*/React.createElement(React.Fragment, null, showSuffix !== false && suffixNode, hasFeedback && feedbackIcon);
};
// Suffix
let mergedSuffix = null;
if (suffix !== undefined) {
mergedSuffix = isFunction(suffix) ? props => getSuffixNode(suffix(props)) : getSuffixNode(suffix);
} else if (loading) {
mergedSuffix = getSuffixNode(fallbackProp(loadingIcon, contextLoadingIcon, /*#__PURE__*/React.createElement(LoadingOutlined, {
spin: true,
"aria-hidden": "true"
})));
} else {
mergedSuffix = props => {
const {
open,
showSearch
} = props;
if (open && showSearch) {
return getSuffixNode(fallbackProp(searchIcon, contextSearchIcon, /*#__PURE__*/React.createElement(SearchOutlined, {
"aria-hidden": "true"
})));
}
const fallbackSuffix = fallbackProp(contextSuffix, /*#__PURE__*/React.createElement(DownOutlined, {
"aria-hidden": "true"
}));
return getSuffixNode(isFunction(fallbackSuffix) ? fallbackSuffix(props) : fallbackSuffix);
};
}
// Checked item icon
const mergedItemIcon = fallbackProp(menuItemSelectedIcon, contextMenuItemSelectedIcon, multiple ? /*#__PURE__*/React.createElement(CheckOutlined, null) : null);
const mergedRemoveIcon = fallbackProp(removeIcon, contextRemoveIcon, /*#__PURE__*/React.createElement(CloseOutlined, null));
return {
clearIcon: mergedClearIcon,
suffix: mergedSuffix,
itemIcon: mergedItemIcon,
removeIcon: mergedRemoveIcon
};
}, [suffix, contextSuffix, clearIcon, contextClearIcon, menuItemSelectedIcon, contextMenuItemSelectedIcon, removeIcon, contextRemoveIcon, loading, loadingIcon, contextLoadingIcon, searchIcon, contextSearchIcon, multiple, hasFeedback, showSuffix, feedbackIcon, showArrow]);
}